Last active
September 4, 2020 10:53
-
-
Save navi25/7ab41931eb52bbcb693b5599e6955245 to your computer and use it in GitHub Desktop.
Media Source Builder for ExoPlayer Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.navendra.player | |
class MediaSourceBuilder { | |
//Build various MediaSource depending upon the type of Media for a given video/audio uri | |
fun build(uri: Uri): MediaSource { | |
val userAgent = PlayerConstants.USER_AGENT | |
val lastPath = uri.lastPathSegment?:"" | |
val defaultHttpDataSourceFactory = DefaultHttpDataSourceFactory(userAgent) | |
if(lastPath.contains(PlayerConstants.FORMAT_MP3) || lastPath.contains(PlayerConstants.FORMAT_MP4)){ | |
return ExtractorMediaSource.Factory(defaultHttpDataSourceFactory) | |
.createMediaSource(uri) | |
}else if(lastPath.contains(PlayerConstants.FORMAT_M3U8)){ | |
return HlsMediaSource.Factory(defaultHttpDataSourceFactory) | |
.createMediaSource(uri) | |
}else{ | |
val dashChunkSourceFactory = DefaultDashChunkSource.Factory(defaultHttpDataSourceFactory) | |
return DashMediaSource.Factory(dashChunkSourceFactory, defaultHttpDataSourceFactory) | |
.createMediaSource(uri) | |
} | |
} | |
//Overloaded function to Build various MediaSource for whole playlist of video/audio uri | |
fun build(uriList: Array<Uri>) : MediaSource{ | |
val playlistMediaSource = DynamicConcatenatingMediaSource() | |
uriList.forEach { playlistMediaSource.addMediaSource(build(it)) } | |
return playlistMediaSource | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment