ExoPlayer:如何根据带宽设置视频质量

时间:2018-10-28 12:27:02

标签: java android hls exoplayer

我真的很固执于此,我正在构建一个直播电视应用程序, 播放器根据带宽自动选择流的质量。 我在stackoverflow中关注了与此问题相关的答案 但它似乎没有用,我不知道自己缺少什么或缺少理解。

请在下面查看我的代码... 这是正确的方法吗,还是我应该喜欢具有不同规则的URL列表,但是玩家如何选择我不知道

提前谢谢。

       private String hlsVideoUri = "http://stream/1080p.m3u8";
     TrackSelection.Factory trackSelectionFactory;

        trackSelectionFactory = new 
      AdaptiveTrackSelection.Factory(bandwidthMeter);


    TrackSelector trackSelector = new 
    DefaultTrackSelector(trackSelectionFactory);
    DefaultDataSourceFactory defaultDataSourceFactory = new 
    DefaultDataSourceFactory(this, Util.getUserAgent(this, "Tv"), 
    bandwidthMeter);
    // 1. Create a default TrackSelector
    Handler mainHandler = new Handler();

    Log.d("rashed"," "+bandwidthMeter.getBitrateEstimate());
    // 2. Create a default LoadControl
    LoadControl loadControl = new DefaultLoadControl();


    // 3. Create the player
    player =
            ExoPlayerFactory.newSimpleInstance(
                    this, trackSelector);

    simpleExoPlayerView = (PlayerView) findViewById(R.id.playerview);
    simpleExoPlayerView.setPlayer(player);

//

    // Measures bandwidth during playback. Can be null if not required.

    // This is the MediaSource representing the media to be played.
  HlsMediaSource hlsMediaSource =new   



   HlsMediaSource.Factory(defaultDataSourceFactory).
    createMediaSource(Uri.parse(hls 
  VideoUri),mainHandler, new MediaSourceEventListener() {
  @Override
  public void onLoadStarted(DataSpec dataSpec, int dataType, int trackType, 
  Format trackFormat, int trackSelectionReason, Object trackSelectionData, 
  long 
   mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs) {

  }

  @Override
  public void onLoadCompleted(DataSpec dataSpec, int dataType, int 
  trackType, 
  Format trackFormat, int trackSelectionReason, Object trackSelectionData, 
  long 
  mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long 
  loadDurationMs, long bytesLoaded) {

  }

  @Override
  public void onLoadCanceled(DataSpec dataSpec, int dataType, int trackType, 
  Format trackFormat, int trackSelectionReason, Object trackSelectionData, 
 long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long 
 loadDurationMs, long bytesLoaded) {

}

  @Override
   public void onLoadError(DataSpec dataSpec, int dataType, int trackType, 
   Format trackFormat, int trackSelectionReason, Object trackSelectionData, 
   long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long 
   loadDurationMs, long bytesLoaded, IOException error, boolean wasCanceled) 
   {

   }

@Override
public void onUpstreamDiscarded(int trackType, long mediaStartTimeMs, long 
 mediaEndTimeMs) {

}

@Override
public void onDownstreamFormatChanged(int trackType, Format trackFormat, int 
 trackSelectionReason, Object trackSelectionData, long mediaTimeMs) {

   }
  });



    player.prepare(hlsMediaSource);
    simpleExoPlayerView.requestFocus();
    player.setPlayWhenReady(true);

}

0 个答案:

没有答案