Ankit meena
Ankit meena

Reputation: 71

Create a simple exoplayer to stream video from url

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();

Upvotes: 3

Views: 19448

Answers (2)

Ganesh MB
Ganesh MB

Reputation: 1179

Its works for me

exo_player_view add this ExoplayerView in your activity_main.xml

  <com.google.android.exoplayer2.ui.PlayerView
        app:use_controller="true"
        android:id="@+id/exo_player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:controller_layout_id="@layout/exoplayer_controller"
        app:fastforward_increment="10000"
        app:hide_on_touch="true"
        app:player_layout_id="@layout/exo_player_view"
        app:resize_mode="fixed_width"
        app:rewind_increment="10000"
        app:show_timeout="2000"/>

exoplayer_controller

create a new layout for exoplayer_controller.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/exo_controller"
android:background="#80000000"
android:orientation="horizontal">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="end">
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_video_settings_white_18dp"
        android:id="@+id/exo_track_selection_view"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1x"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:clickable="true"
        android:id="@+id/exo_playback_speed"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:id="@+id/exo_subtitle"
        android:src="@drawable/exo_ic_subtitle_off"/>
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_sound"
        android:src="@drawable/ic_surround_sound_white"/>
    <ImageView
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_bookmark"
        android:src="@drawable/ic_bookmark"/>

    <ImageView
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_settings"
        android:src="@drawable/ic_more_vert"/>

</LinearLayout>

<LinearLayout
    android:weightSum="4"
    android:id="@+id/control_Set"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/exo_rew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="44dp"
        android:src="@drawable/ic_10sec_back" />

    <ImageView
        android:layout_weight="1"
        android:id="@+id/exo_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_play_circle"/>

    <ImageView
        android:layout_weight="1"
        android:id="@+id/exo_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_pause_circle"/>

    <ImageView
        android:id="@+id/exo_ffwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:src="@drawable/ic_10sec_forward" />
</LinearLayout>

<LinearLayout
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layoutDirection="rtl">
    <ImageView
        android:layout_gravity="end"
        android:layout_marginBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exo_fullscreen"/>
    <TextView
        android:text="duration"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:id="@+id/exo_duration"/>
    <com.google.android.exoplayer2.ui.DefaultTimeBar
        android:layout_width="0dp"
        android:layout_height="26dp"
        android:layout_weight="1"
        android:id="@+id/exo_progress"
        app:played_color="#FF0000"
        app:buffered_color="#FFFFFF"
        app:unplayed_color="#707070"/>
    <TextView
        android:text="position"
        android:layout_marginRight="4dp"
        android:layout_marginLeft="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:id="@+id/exo_position"/>
</LinearLayout>

In-class


And finally play the video with URL

SimpleExoPlayer simpleExoPlayer;
PlayerView playerView;

MediaItem mediaItem = MediaItem.fromUri(link);
playerView.setPlayer(simpleExoPlayer);
simpleExoPlayer.setMediaItem(mediaItem);
simpleExoPlayer.prepare();
simpleExoPlayer.pause();

simpleExoPlayer.addListener(new Player.Listener() {
        @Override
        public void onPlaybackStateChanged(int state) {
            if (state == simpleExoPlayer.STATE_BUFFERING){
                // hide controls
                // show progress
            }else {
                // hide progress and controls
            }
        }
    });

Upvotes: 0

Teodor G.
Teodor G.

Reputation: 176

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.

  • the ... represents empty spaces - or your data/content

Upvotes: 11

Related Questions