How to use android exoplayer

51,293

Solution 1

The ExoMedia library wraps exoplayer in simpler api and provides a video view for use in layouts. See usage examples on github: https://github.com/brianwernick/ExoMedia/

Solution 2

Exoplayer is a very advanced library. Even writing a bare minimum would take 40-50 lines of code. So if you really wanna use a sword to chop onions, here is a direct copy pasta :

//manifest.xml 

<manifest ...>
  <uses-permission android:name="android.permission.INTERNET"/>
  <application
    android:usesCleartextTraffic="true"
    ...>

    ...

  </application>
</manifest>
//app/build.gradle
apply plugin: 'com.android.application'

android {
    ...
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    ...
    implementation 'com.google.android.exoplayer:exoplayer:2.10.4'
}



    protected void onCreate(Bundle savedInstanceState) {
        ...

        Context ctx =this;
        String CONTENT_URL = "https://www.radiantmediaplayer.com/media/bbb-360p.mp4";
        int playerID=R.id.pv_main;
        int appNameStringRes = R.string.app_name;
        startPlayingVideo(this,CONTENT_URL,playerID,appNameStringRes);


    }

    //
    private void startPlayingVideo(Context ctx , String CONTENT_URL, int playerID, String appNameRes) {

        PlayerView pvMain = ctx.findViewById(playerID);

        //BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        //TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);        
        //TrackSelector trackSelectorDef = new DefaultTrackSelector(videoTrackSelectionFactory);
        TrackSelector trackSelectorDef = new DefaultTrackSelector();

        SimpleExoPlayer absPlayerInternal = ExoPlayerFactory.newSimpleInstance(ctx, trackSelectorDef);

        String userAgent = Util.getUserAgent(ctx, ctx.getString(appNameRes));

        DefaultDataSourceFactory defdataSourceFactory = new DefaultDataSourceFactory(ctx,userAgent);
        Uri uriOfContentUrl = Uri.parse(CONTENT_URL);
        MediaSource mediaSource = new ProgressiveMediaSource.Factory(defdataSourceFactory).createMediaSource(uriOfContentUrl);

        absPlayerInternal.prepare(mediaSource);
        absPlayerInternal.setPlayWhenReady(true);

        pvMain.setPlayer(absPlayerInternal);

    }

    private void stopPlayer(PlayerView pv,SimpleExoPlayer absPlayer){
        pv.setPlayer(null);
        absPlayer.release();
        absPlayer = null;
    }

simply add the player view in your activity layout, call the startPlayingVideo(...) in onCreate() and stopPlayer() in the onStop() . I am not an expert but i can try explaining this if you want, but you asked for no complicated stuff, so here is just the code

Solution 3

//Add dependency in manifest file 
    implementation 'com.google.android.exoplayer:exoplayer:2.7.3'


// Add exoplayer in your layout(xml) file 
 <RelativeLayout
        android:id="@+id/rl_video"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/videoFullScreenPlayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A6000000"
        app:controller_layout_id="@layout/exo_playback_control_view"
        app:player_layout_id="@layout/exo_simple_player_view"
        app:repeat_toggle_modes="none"
        app:show_timeout="45000"
        app:surface_type="texture_view"
        />
        <ProgressBar
            android:id="@+id/spinnerVideoDetails"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:indeterminate="true"/>
    </RelativeLayout>




// open video with below code

// initialise varible 
 PlayerView videoFullScreenPlayer;
    SimpleExoPlayer player;
    ProgressBar spinnerVideoDetails;


// find Id 
 videoFullScreenPlayer = findViewById(R.id.videoFullScreenPlayer);
        spinnerVideoDetails = findViewById(R.id.spinnerVideoDetails);

// open video method 
private void setUp() {
        initializePlayer();
        if (videoUrl == null) {
            return;
        }
        buildMediaSource(Uri.parse(videoUrl ));
    }
    private void initializePlayer() {
        if (player == null) {

            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
            TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
            TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
            // 1. Create a default TrackSelector
            DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(32*1024, 64*1024, 1024, 1024).createDefaultLoadControl();
            // 2. Create the player
            player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
            videoFullScreenPlayer.setPlayer(player);
        }
    }

    private void buildMediaSource(Uri mUri) {
        // Measures bandwidth during playback. Can be null if not required.
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this, getString(R.string.app_name)), bandwidthMeter);
        // This is the MediaSource representing the media to be played.
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(mUri);
        // Prepare the player with the source.
        player.prepare(videoSource);
        player.setPlayWhenReady(true);
        player.addListener(this);
    }

    private void releasePlayer() {
        if (player != null) {
            player.release();
            player = null;
        }
    }

    private void pausePlayer() {
        if (player != null) {
            player.setPlayWhenReady(false);
            player.getPlaybackState();
        }
    }

    private void resumePlayer() {
        if (player != null) {
            player.setPlayWhenReady(true);
            player.getPlaybackState();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        pausePlayer();
       /* if (mRunnable != null) {
            mHandler.removeCallbacks(mRunnable);
        }*/
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        resumePlayer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releasePlayer();
    }

    @Override
    public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
    }

    @Override
    public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
    }

    @Override
    public void onLoadingChanged(boolean isLoading) {
    }

    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        switch (playbackState) {
            case Player.STATE_BUFFERING:
                spinnerVideoDetails.setVisibility(View.VISIBLE);
                break;
            case Player.STATE_ENDED:
                // Activate the force enable
                break;
            case Player.STATE_IDLE:
                break;
            case Player.STATE_READY:
                spinnerVideoDetails.setVisibility(View.GONE);
                break;
            default:
                // status = PlaybackStatus.IDLE;
                break;
        }
    }

    @Override
    public void onRepeatModeChanged(int repeatMode) {
    }

    @Override
    public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
    }

    @Override
    public void onPlayerError(ExoPlaybackException error) {
    }

    @Override
    public void onPositionDiscontinuity(int reason) {
    }

    @Override
    public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
    }

    @Override
    public void onSeekProcessed() {
    }

Solution 4

A VideoView would be a better idea in case you wish to display only a Video URL. ExoPlayer requires some developmental effort, even for invoking its simple instance. However, there is an advantage of faster and more efficient playback, backed up by an active open-source community. This link provides a good walk through the implementation giving ample reasons to switch to ExoPlayer. Ofcourse, do checkout the official developer guide, the updated version has split modules for simpler implementation.

Share:
51,293

Related videos on Youtube

kimv
Author by

kimv

Updated on July 09, 2022

Comments

  • kimv
    kimv almost 2 years

    I am looking to implement Google's ExoPlayer in my app. Their documentation seems pretty vague to me, and all I am looking for is to play a video from an URL, no complicated custom settings or anything like that. Haven't found any tutorials on how to do it. The example they have on git is way too complicated for what I need and, since I am new to video streaming, I did not understand much. All I have managed to do so far is to display a com.google.android.exoplayer.AspectRatioFrameLayout.

    Basically, I have an URL. I need to play the video, and handle onConfigurationChanged when the user flips the screen.

    Can anyone help?

    • CommonsWare
      CommonsWare over 8 years
      Searching for exoplayer tutorial on a major search engine turns up davekb.com/… and youtube.com/watch?v=6VjF638VObA. The ExoPlayer developers recommend reviewing their demo app.
    • kimv
      kimv over 8 years
      I reviewed their demo app, and I did not understand anything. As I said, I am new to video streaming. I also looked over davekb's example, and the FrameworkSampleSource class is not only deprecated, but I do not have the same constructor he seems to have. He has FrameworkSampleSource(mContext, uri, /* headers */ null, numRenderers), and I have a constructor without the number of renderers
    • CommonsWare
      CommonsWare over 8 years
      OK, just making sure that you tried what's out there. I haven't played with ExoPlayer yet (though it's actually on my short list), and I don't get the sense that it is aimed at simple scenarios.
    • kimv
      kimv over 8 years
      I just have to customize the looks. I understand that the classic way does not offer a way to do this, but the exoplayer does. Hopefully, someone will post an example
    • Hugo Gresse
      Hugo Gresse over 8 years
      I'm creating a library that will allow you to do this. stay tunned ;)
    • Hugo Gresse
      Hugo Gresse over 8 years
      so here it is : github.com/HugoGresse/SimpleExoPlayer It's missing a lot of stuff including FullscreenActivity, the documentation, but you can start using VideoPlayerFactory and use the project as a git submodule for the moment, I will push it on jCenter once finish
  • BoD
    BoD about 8 years
    I can confirm that I successfully used this lib in my project to replace VideoView, and it works great.
  • Shajeel Afzal
    Shajeel Afzal over 7 years
    This library is easy to use but shows videos slightly tilted. github.com/brianwernick/ExoMedia/issues/318
  • Nikita Axyonov
    Nikita Axyonov over 7 years
    also this library doesn't support seamless looping as it should be, so if you need this feature you should try something else!
  • Amr El Aswar
    Amr El Aswar over 7 years
    Even though ExoMedia wrapper makes using exoplayer a lot easier it's a nightmare if you plan on resizing your videos dynamically and attaching or detaching your player since it doesnt handle surface resizing and doing it manually ends up releasing the surfaceview
  • RocketSpock
    RocketSpock over 7 years
    Just to make sure that no one is misguided; seamless looping has only really been supported by the ExoPlayer since V2 (there were still gaps in V1) which ExoMedia has a task to update to. As far as resizing goes, that bug was fixed a while ago.
  • Jay Patel
    Jay Patel almost 6 years
    This library is buggy! I won't recommend it!
  • Anurag Shukla
    Anurag Shukla almost 3 years
    The full-screen feature doesn't work in Kotlin. Heads up for Kotlin developers