2016-09-16 105 views
2

下面是我們用於使用ExoPlayer1從URL播放mp4視頻的代碼片段,我是在升級到ExoPlayer2之後。 我不確定升級後需要更新哪些內容才能使其工作,因爲我對ExoPlayer本身並不熟悉,也沒有太多有用的文檔。 讚賞任何幫助,如果任何一個可以遮陽一些輕..謝謝滿口ExoPlayer 2遷移

private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; 
private static final int BUFFER_SEGMENT_COUNT = 256; 
private ExoPlayer videoPlayer; 
private Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE); 

private void playVideo(Uri uri) { 
    Handler handler = new Handler(); 
    DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(handler, null); 
    DataSource dataSource = new DefaultUriDataSource(getContext(), bandwidthMeter, "Android-ExoPlayer"); 

    SampleSource sampleSource = new ExtractorSampleSource(
      uri, 
      dataSource, 
      allocator, 
      BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE); 

    TrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(
      sampleSource, 
      MediaCodecSelector.DEFAULT); 

    // 2. Construct renderers. 
    TrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(
      getContext(), 
      sampleSource, 
      MediaCodecSelector.DEFAULT, 
      MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT); 

    // 1. Instantiate the player. 
    if (videoPlayer == null) { 
     videoPlayer = ExoPlayer.Factory.newInstance(4); 
    } 


     // 3. Inject the renderers through prepare. 
     videoPlayer.prepare(videoRenderer, audioRenderer); 

     // 4. Pass the surface to the video renderer. 
     SurfaceTexture surfaceTexture = textureView.getSurfaceTexture(); 
     Surface surface = new Surface(surfaceTexture); 
     videoPlayer.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface); 


    videoPlayer.addListener(new ExoPlayer.Listener() { 
     @Override 
     public void onPlayerStateChanged(boolean b, int i) { 
      if (i == ExoPlayer.STATE_READY) { 
       mediaControl.setMedia(new MediaControl.Media() { 
        @Override 
        public void setCurrentPosition(long positionMs) { 
         if (videoPlayer != null) { 
          videoPlayer.seekTo(positionMs); 
         } 
        } 

        @Override 
        public long getCurrentPosition() { 
         if (videoPlayer != null) { 
          return videoPlayer.getCurrentPosition(); 
         } 
         return 0; 
        } 

        @Override 
        public long getDuration() { 
         if (videoPlayer != null) { 
          return videoPlayer.getDuration(); 
         } 
         return 0; 
        } 

        @Override 
        public void start() { 
         if (videoPlayer != null) { 
          videoPlayer.setPlayWhenReady(true); 
         } 
        } 

        @Override 
        public void pause() { 
         if (videoPlayer != null) { 
          videoPlayer.setPlayWhenReady(false); 
         } 
        } 

        @Override 
        public int getBufferedPercentage() { 
         if (videoPlayer != null) { 
          return videoPlayer.getBufferedPercentage(); 
         } 
         return 0; 
        } 
       }); 

       mediaControl.start(); 

       videoPlayer.removeListener(this); 
      } 
     } 

     @Override 
     public void onPlayWhenReadyCommitted() { 

     } 

     @Override 
     public void onPlayerError(ExoPlaybackException e) { 
      mediaControl.pause(); 
     } 
    }); 

    // 5. Start playback. 
    mediaControl.start(); 
} 

回答

1
Handler handler = new Handler(); 
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(handler, null); 
MediaSource sampleSource = new ExtractorMediaSource(
    uri, 
    new DefaultDataSourceFactory(getContext(), "Android-ExoPlayer", bandwidthMeter), 
    new DefaultExtractorsFactory(), null, null); 

if (videoPlayer == null) { 
    Handler mainHandler = new Handler(); 
    TrackSelection.Factory videoTrackSelectionFactory = 
     new AdaptiveVideoTrackSelection.Factory(bandwidthMeter); 
    TrackSelector trackSelector = 
     new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory); 
    LoadControl loadControl = new DefaultLoadControl(); 
    videoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl); 
} 

videoPlayer.prepare(sampleSource); 
videoPlayer.setVideoTextureView(textureView);