正确的 Exoplayer Uri

Right Exoplayer Uri

我下载了一个简单的Exoplayer示例(这个link:github example

它与设置的 uri 完美配合,但是当我切换到想要播放的视频时,没有任何反应。我想我要设置的 Uri 是不对的。我怎样才能让这个视频正常播放?

这里是 class 和简单的 class。 Uri 行注释:

public class MainActivity extends AppCompatActivity implements VideoRendererEventListener {


private static final String TAG = "MainActivity";
private SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private TextView resolutionTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    resolutionTextView = new TextView(this);
    resolutionTextView = (TextView) findViewById(R.id.resolution_textView);

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

    LoadControl loadControl = new DefaultLoadControl();

    player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
    simpleExoPlayerView = new SimpleExoPlayerView(  this);
    simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);

    simpleExoPlayerView.setUseController(true);
    simpleExoPlayerView.requestFocus();

    simpleExoPlayerView.setPlayer(player);

    //THIS WORKS!
    Uri mp4VideoUri =Uri.parse("http://54.255.155.24:1935//Live/_definst_/amlst:sweetbcha1novD235L240P/playlist.m3u8");

    //THIS IS THE ONE I WANT TO PLAY!
    //Uri mp4VideoUri =Uri.parse("http://d17h27t6h515a5.cloudfront.net/topher/2017/April/58ffdcf9_9-final-product-brownies/9-final-product-brownies.mp4");


    DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();

    DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA);

    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

    MediaSource videoSource = new HlsMediaSource(mp4VideoUri, dataSourceFactory, 1, null, null);
    final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);

    player.prepare(loopingSource);

    player.addListener(new ExoPlayer.EventListener() {
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest) {
            Log.v(TAG, "Listener-onTimelineChanged...");
        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
            Log.v(TAG, "Listener-onTracksChanged...");
        }

        @Override
        public void onLoadingChanged(boolean isLoading) {
            Log.v(TAG, "Listener-onLoadingChanged...isLoading:"+isLoading);
        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            Log.v(TAG, "Listener-onPlayerStateChanged..." + playbackState);
        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            Log.v(TAG, "Listener-onRepeatModeChanged...");
        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {
            Log.v(TAG, "Listener-onPlayerError...");
            player.stop();
            player.prepare(loopingSource);
            player.setPlayWhenReady(true);
        }

        @Override
        public void onPositionDiscontinuity() {
            Log.v(TAG, "Listener-onPositionDiscontinuity...");
        }

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
            Log.v(TAG, "Listener-onPlaybackParametersChanged...");
        }
    });

    player.setPlayWhenReady(true);
    player.setVideoDebugListener(this);
}

@Override
public void onVideoEnabled(DecoderCounters counters) {

}

@Override
public void onVideoDecoderInitialized(String decoderName, long initializedTimestampMs, long initializationDurationMs) {

}

@Override
public void onVideoInputFormatChanged(Format format) {

}

@Override
public void onDroppedFrames(int count, long elapsedMs) {

}

@Override
public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
    Log.v(TAG, "onVideoSizeChanged ["  + " width: " + width + " height: " + height + "]");
    resolutionTextView.setText("RES:(WxH):"+width+"X"+height +"\n           "+height+"p");
}

@Override
public void onRenderedFirstFrame(Surface surface) {

}

@Override
public void onVideoDisabled(DecoderCounters counters) {

}


@Override
protected void onStop() {
    super.onStop();
    Log.v(TAG, "onStop()...");
}

@Override
protected void onStart() {
    super.onStart();
    Log.v(TAG, "onStart()...");
}

@Override
protected void onResume() {
    super.onResume();
    Log.v(TAG, "onResume()...");
}

@Override
protected void onPause() {
    super.onPause();
    Log.v(TAG, "onPause()...");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.v(TAG, "onDestroy()...");
    player.release();
}

}

您在此处使用 HlsMediaSourcenew HlsMediaSource(mp4VideoUri, dataSourceFactory, 1, null, null);。但是你真的想使用 ExtractorMediaSource.

可以找到更多信息 here

.m3u8 是一个 HLS 文件,.mp4.wav 等需要 ExtractorMediaSource.

HlsMediaSource 用于 Adaptive Streaming 视频。示例中的视频 link 用于直播。对于 mp4, ogg, 3gp, wav 等其他视频格式,请使用 ExtractorMediaSource.

这是代码示例,用于处理两者(如果需要)-

final LoopingMediaSource loopingSource;
Uri mp4VideoUri = Uri.parse(uri);
if (uri.contains("m3u8")) {
     MediaSource videoSource = new HlsMediaSource(mp4VideoUri, dataSourceFactory, 3, null, null);
     loopingSource = new LoopingMediaSource(videoSource);
}else {
     MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri, dataSourceFactory, extractorsFactory, null, null);
     loopingSource = new LoopingMediaSource(videoSource);
}

这里是ExoPlayer Developer guide了解更多信息。