Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use new androidx.media2.widget.VideoView

Problem

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.

My Research

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)

like image 778
Hacker129042 Avatar asked Dec 21 '18 18:12

Hacker129042


2 Answers

For future references androidx.media2.widget.VideoView's simple usage is:

  1. Create MediaMetadata if required. Here I'm simply setting media title
  val mediaMetaData = MediaMetadata.Builder()
      .putString(MediaMetadata.METADATA_KEY_TITLE, "media title")
      .build()
  1. Create MediaItem from any sources. Here I'm using url as a source
  val mediaItem = UriMediaItem.Builder(videoUrl.toUri())
      .setMetadata(mediaMetaData) // optional
      .build()
  1. 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
    )
  }
like image 62
Fatih Santalu Avatar answered Sep 18 '22 19:09

Fatih Santalu


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);
like image 22
Paul Avatar answered Sep 20 '22 19:09

Paul