How to use android exoplayer

后端 未结 5 2017
被撕碎了的回忆
被撕碎了的回忆 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: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 
    
    
      
      
    
        ...
    
      
    
    
    //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

提交回复
热议问题