Not able to find information about androidx.media2.VideoView
.
I want to stream a video from an url which is working with VideoView1
, however I can't achieve it withVideoView2
.
In the documentation they describe this method:
setVideoUri
open fun setVideoUri(uri: Uri!, headers: MutableMap<String!, String!>?): Unit
Sets video URI using specific headers.
However this method seems to be no longer available (I am using mediaWidgetVersion 1.0.0-alpha06
)
For future references androidx.media2.widget.VideoView's simple usage is:
- Create MediaMetadata if required. Here I'm simply setting media title
val mediaMetaData = MediaMetadata.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, "media title")
.build()
- Create MediaItem from any sources. Here I'm using url as a source
val mediaItem = UriMediaItem.Builder(videoUrl.toUri())
.setMetadata(mediaMetaData) // optional
.build()
- Create SessionPlayer and set it to VideoView by calling
setPlayer
val mediaPlayer = MediaPlayer(this)
with(mediaPlayer) {
videoView.setPlayer(this)
setMediaItem(mediaItem)
// play when ready
prepare().addListener(
Runnable { play() },
Executors.BACKGROUND_EXECUTOR
)
}
You should use MediaItem internal builder like this to create a UriMediaItem
UriMediaItem yourUriMediaItemHere = new UriMediaItem.Builder(context, uri).build();
and then use videoViews setMediaItem method like this
videoView.setMediaItem(yourUriMediaItemHere);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With