In my app the user can select some videos from the gallery to edit them. After the user selects videos, I create a list of Uri and I want to play in exoplayer. I can play videos using their file path such as /storage/emulated/0/Android/data/com.vitor.videoeditor/files/Movies/VideoEditor/VID_20200722_133503.mp4, but I don't know how to do the same with a uri like content: // media / external / file / 305166.I tried to pass the uri directly to DataSpec () but it didn't work. What is the correct way to play the videos?
private fun playVideos(pathsList: List<String>) {
val concatenatingMediaSource = ConcatenatingMediaSource()
pathsList.forEach { filePath ->
val dataSpec = DataSpec(Uri.fromFile(File(filePath)))
val fileDataSource = FileDataSource()
try {
fileDataSource.open(dataSpec)
} catch (e: FileDataSource.FileDataSourceException) {
e.printStackTrace()
}
val dataSourceFactory = DataSource.Factory { fileDataSource }
val mediaSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.fromFile(File(filePath)))
concatenatingMediaSource.addMediaSource(mediaSource)
}
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(applicationContext)
with(simpleExoPlayer) {
prepare(concatenatingMediaSource)
playerView.player = simpleExoPlayer
playWhenReady = true
repeatMode = Player.REPEAT_MODE_ALL
}
}
content uri will expires soon So only during the file opening by intent you can play a video then it will expire. to avoid that
val cr = applicationContext.contentResolver
val flg: Int = Intent.FLAG_GRANT_WRITE_URI_PERMISSION or
Intent.FLAG_GRANT_READ_URI_PERMISSION
cr.takePersistableUriPermission(uri!!, flg)
put above code in onActivity result. So that the uri will not expire.
and use same content uri later on.
Related
How can I set and show subtitles from url with ExoPlayer2 in Android? Currently, I write in Kotlin, I'm using following code for setting up ExoPlayer with subtitles:
exoPlayer = SimpleExoPlayer.Builder(this).build()
val subtitle = MediaItem.Subtitle(Uri.parse(SUBTITLES_URL), MimeTypes.TEXT_VTT, "en")
val subtitles = arrayListOf(subtitle)
val mediaItem = MediaItem.Builder()
.setUri(movieSrc)
.setSubtitles(subtitles)
.build()
exoPlayer.setMediaItem(mediaItem)
exoPlayer.addListener(this)
exoPlayer.prepare()
And following code to display them in ExoPlayer SubtitleView:
exoPlayer.addTextOutput {
binding.exoSubtitles.onCues(it)
}
I don't get any exception, it just does not show anything idk...
Nothing really works... Really need some help, Thank You in Advance!
Here is the code I used (I got these from the Internet somewhere I forgot)
// create the simple cache
val leastRecentlyUsedCacheEvictor = LeastRecentlyUsedCacheEvictor(MAX_CACHE_SIZE)
val databaseProvider: DatabaseProvider = ExoDatabaseProvider(this)
val simpleCache = SimpleCache(cacheDir, leastRecentlyUsedCacheEvictor, databaseProvider)
// create http datasource
val defaultBandwidthMeter = DefaultBandwidthMeter.Builder(context).build()
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
context,
DefaultHttpDataSourceFactory(
System.getProperty("http.agent"),
defaultBandwidthMeter
)
)
// create the player
val simplePlayer = ExoPlayerFactory.newSimpleInstance(context)
simplePlayer.addListener(playerCallback)
cacheDataSourceFactory = CacheDataSourceFactory(
simpleCache,
DefaultHttpDataSourceFactory(
Util.getUserAgent(
context,
"exo"
)
)
)
// create subtitle text format
val textFormat = Format.createTextSampleFormat(
null,
MimeTypes.TEXT_VTT,
C.SELECTION_FLAG_DEFAULT,
null
)
// create the subtitle source
val subtitleSource = SingleSampleMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(it), textFormat, C.TIME_UNSET)
// create the media source based from video/audio url
val mediaSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory).createMediaSource(uri)
// merge subtitle source and media source
val mediaSourceWithsubtitle = MergingMediaSource(mediaSource, subtitleSource)
// setup the player
simplePlayer.prepare(mediaSourceWithsubtitle, true, true)
I am right now using FFMPEG to stream mp4 file using HLS.
I am using this link to enable encryption: https://hlsbook.net/how-to-encrypt-hls-video-with-ffmpeg/
To play video in my android app, I am using exoplayer, below is my source code to play video:
Player player;
private MediaSource buildMediaSource(Uri uri) {
TrackSelection.Factory adaptiveTrackSelection = new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
player = ExoPlayerFactory.newSimpleInstance(
this,
new DefaultTrackSelector(adaptiveTrackSelection));
playerView.setPlayer(player);
// These factories are used to construct two media sources.
DefaultBandwidthMeter defaultBandwidthMeter = DefaultBandwidthMeter.getSingletonInstance(this);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext,
Util.getUserAgent(mContext, "cookvid"), defaultBandwidthMeter);
//DataSource.Factory dataSourceFactory =
// new DefaultDataSourceFactory(this, "exoplayer-codelab");
HlsMediaSource.Factory mediaSourceFactory = new HlsMediaSource.Factory(dataSourceFactory);
return mediaSourceFactory.createMediaSource(uri);
//return new ProgressiveMediaSource.Factory(dataSourceFactory)
// .createMediaSource(uri);
}
private void initializePlayer() {
Uri uri = Uri.parse(getString(R.string.media_url_hls));
MediaSource mediaSource = buildMediaSource(uri);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
player.addListener(playbackStateListener);
player.prepare(mediaSource, false, false);
}
But with this code, I can not play video in app, If I am not using this encryption ,then exoplayer can play video without any issue.
Please help me on this, I am newbie on exoplayer side.
I created for you an example in Kotlin to make it work:
class MainActivity : AppCompatActivity() {
private var exoPlayer: SimpleExoPlayer? = null
private var trackSelector: DefaultTrackSelector? = null
var drmSessionManager: DefaultDrmSessionManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
trackSelector = DefaultTrackSelector(this)
exoPlayer = SimpleExoPlayer.Builder(this)
.setTrackSelector(trackSelector!!)
.build()
player_view.player = exoPlayer
var uri = Uri.fromFile( File("//android_asset/legend_enc.mp4"))
playVideo(uri, "zX65/4jzTK6wYYWwACTkwg", "Y8tfcYTdS2iaXF/xHuajKA")
}
private fun playVideo(url: Uri, id: String, value: String){
try {
drmSessionManager =
Util.getDrmUuid(C.CLEARKEY_UUID.toString())?.let { buildDrmSessionManager(
it,
true,
id,
value
) }
} catch (e: UnsupportedDrmException) {
e.printStackTrace()
}
exoPlayer?.setMediaSource(buildDashMediaSource(url))
exoPlayer?.prepare()
exoPlayer?.playWhenReady = true
}
private fun buildDashMediaSource(uri: Uri): MediaSource {
val dashChunkSourceFactory = DefaultDataSourceFactory(this, "agent")
return ProgressiveMediaSource.Factory(dashChunkSourceFactory)
.setDrmSessionManager(drmSessionManager ?: DrmSessionManager.DUMMY)
.createMediaSource(uri)
}
#Throws(UnsupportedDrmException::class)
private fun buildDrmSessionManager(uuid: UUID, multiSession: Boolean, id: String, value: String): DefaultDrmSessionManager {
val drmCallback = LocalMediaDrmCallback("{\"keys\":[{\"kty\":\"oct\",\"k\":\"${value}\",\"kid\":\"${id}\"}],\"type\":\"temporary\"}".toByteArray())
val mediaDrm = FrameworkMediaDrm.newInstance(uuid)
return DefaultDrmSessionManager(uuid, mediaDrm, drmCallback, null, multiSession)
}
}
I am creating a music player using Exoplayer. If the user clicks on a specific track from recyclerview I want to play all tracks but switch to clicked index.
I am using ConcatenatingMediaSource to play the playlist.
val concatenatedSource = ConcatenatingMediaSource()
for (v in videos) {
when (v.type) {
"join" -> {
val mediaSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(v.src))
concatenatedSource.addMediaSource(mediaSource)
}
"separate" -> {
val videoSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(v.vSrc))
val audioSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(v.aSrc))
val mergedSource = MergingMediaSource(videoSource, audioSource)
concatenatedSource.addMediaSource(mergedSource)
}
"stream" -> {
val hlsSource = HlsMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(Uri.parse(v.src))
concatenatedSource.addMediaSource(hlsSource)
}
}
}
player.prepare(concatenatedSource)
I would like to know how to be able to play more than two songs back to back in ExoPlayer using MediaSources I have in an ArrayList.
I can use ConcatenatingMediaSource to be able to play two songs back to back, but I have to load them into this fundtion as separate paramters. I do not want to do this for a whole list of songs. I have tried to find an answer to this and seem to have some fundemental misunderstanding as I can't seem to replicate the efforts of others in other StackOverflow questions or blogs etc. (Many blogs show the simple two media source playlist as in the ExoPlayer docs).
This code is for context:
private fun prepareExoPlayer(songs: ListSongs) {
val uris = parseUris(songs)
val mediaSource = buildMediaSource(uris)
applyAudioAttributes()
simpleExoPlayer!!.prepare(mediaSource, false, false)
}
This code is where the issue is:
private fun buildMediaSource(uris: ArrayList<Uri>): MediaSource {
val userAgent = Util.getUserAgent(this, "MusicPlayer")
val defaultMediaSource = DefaultDataSourceFactory(this, userAgent)
val progressiveMediaSource = ProgressiveMediaSource.Factory(defaultMediaSource)
val mediaSources = ArrayList<MediaSource>()
for (uri in uris) {
mediaSources.add(progressiveMediaSource.createMediaSource(uri))
}
return if (mediaSources.size == 1) {
mediaSources[0]
} else {
val concatenatingMediaSource = ConcatenatingMediaSource()
concatenatingMediaSource.addMediaSources(mediaSources)
// ConcatenatingMediaSource(mediaSources[0], mediaSources[1])
}
}
In the else statement I get a failure as the return type is not a MediaSource, but a Unit. However, the commented code on the last line works fine. How do I modify the 2nd and 3rd last line to be able to play my list of songs?
Ok, so I just found this video: https://www.youtube.com/watch?v=svdq1BWl4r8
Turns out prepare for ExoPlayer does not have to have a MediaSource as a parameter, but can have a ConcatenatingMediaSource as a parameter as well. These aren't the same but are both accepted by the prepare function.
It's also worth noting that ConcatenatingMediaSource can recieve a single MediaSource. This means the if statement check on the size of the MediaSource ArrayList isn't necessary.
The solution is therefore to change the return type of buildMediaSource to ConcatenatingMediaSource and remove the if statement. Like this:
private fun buildMediaSource(uris: ArrayList<Uri>): ConcatenatingMediaSource {
val userAgent = Util.getUserAgent(this, "MusicPlayer")
val defaultMediaSource = DefaultDataSourceFactory(this, userAgent)
val progressiveMediaSource = ProgressiveMediaSource.Factory(defaultMediaSource)
val mediaSources = ArrayList<MediaSource>()
for (uri in uris) {
mediaSources.add(progressiveMediaSource.createMediaSource(uri))
}
val concatenatingMediaSource = ConcatenatingMediaSource()
concatenatingMediaSource.addMediaSources(mediaSources)
return concatenatingMediaSource
}
I implement exoplayer2 in my app for playing HLS Videos, but sometimes it plays just sound, and doesn't work correctly. What am I supposed to do? I couldn't find why this happens sometimes.
this is the code for initializing player :
fun initPalyer(){
val mainHandler = Handler()
val bandwidthMeter: BandwidthMeter = DefaultBandwidthMeter.Builder(context).build()
bandwidthMeter.addEventListener(mainHandler!!, this)
val trackSelectionFactory: TrackSelection.Factory = AdaptiveTrackSelection.Factory()
trackSelector = DefaultTrackSelector(context, trackSelectionFactory)
val builder = ParametersBuilder(context)
trackSelectorParameters = builder.build()
trackSelector!!.parameters = trackSelectorParameters
var rendersFactory: RenderersFactory = app.buildRenderersFactory(false)
player = SimpleExoPlayer.Builder(
context, renderersFactory
)
.setTrackSelector(trackSelector!!)
.setBandwidthMeter(bandwidthMeter)
//.setLoadControl(loadControl)
.build()
player!!.addListener(this)
loadState()
playerView.player = player!!
}
the code for preparing player :
private fun preparePlayer(uri: Uri) {
val mediaSource = MediaSourceBuilder().build(uri)
durationSet = false
player?.prepare(mediaSource, true, false)
}
and the code for creating mediaSources :
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)
.setAllowChunklessPreparation(true)
.createMediaSource(uri)
}else{
val dashChunkSourceFactory = DefaultDashChunkSource.Factory(defaultHttpDataSourceFactory)
return DashMediaSource.Factory(dashChunkSourceFactory, defaultHttpDataSourceFactory)
.createMediaSource(uri)
}
}
You might wanna change your DataSourceFactory, in case your URL are in HTTPS you might end up with an error, try using DefaultDataSourceFactory instead of DefaultHttpDataSourceFactory