I am a complete beginner at android studio.
and I am working on a simple application in which I have to stream a video from a URL. Just a simple Exoplayer I tried the below code but it doesn't work. If anybody knows please help.
XML
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/exoplayer"
android:layout_width="match_parent"
android:layout_height="500dp"
android:foregroundGravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
JAVA
PlayerView playerView;
SimpleExoPlayer simpleExoPlayer;
simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();
playerView = findViewById(R.id.exoPlayerView);
playerView.setPlayer(simpleExoPlayer);
MediaItem mediaItem = MediaItem.fromUri(my video url);
simpleExoPlayer.addMediaItem(mediaItem);
simpleExoPlayer.prepare();
simpleExoPlayer.play();
First make sure that you have the internet permission added to the manifest <uses-permission android:name="android.permission.INTERNET" />
. After that you can try to add android:usesCleartextTraffic="true"
to the manifest in the application tag.
Here is an example of the manifest file:
<manifest ...>
...
<uses-permission android:name="android.permission.INTERNET"/>
...
<application
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
In your *.java
class add this
simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();
playerView = findViewById(R.id.exoPlayerView);
playerView.setPlayer(simpleExoPlayer);
MediaItem mediaItem = MediaItem.fromUri("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
simpleExoPlayer.addMediaItem(mediaItem);
simpleExoPlayer.prepare();
simpleExoPlayer.setPlayWhenReady(true);
The setPlayWhenReady(true)
is required when you are playing a video from URL.
...
represents empty spaces - or your data/content