How to use android exoplayer

后端 未结 5 2008
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 08:28

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 c

相关标签:
5条回答
  • 2021-01-31 08:35

    Here is a new Github Library named MagicalExoPlayer, That based on ExoPlayer.

    Supports MP4, HLS and Dash.

    Supports custom Aspect Ratios

    Support FullScreen

    0 讨论(0)
  • 2021-01-31 08:45

    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.

    0 讨论(0)
  • 2021-01-31 08:51
    //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() {
        }
    
    0 讨论(0)
  • 2021-01-31 08:53

    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

    0 讨论(0)
  • 2021-01-31 08:57

    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/

    0 讨论(0)
提交回复
热议问题