在EXOPlayer中播放视频

时间:2016-02-08 06:44:20

标签: android exoplayer

我使用下面的代码使用EXOPlayer播放视频,但它不会播放。

视频格式为mp4。我哪里出错了? 它显示错误 -

com.google.android.exoplayer.ExoPlaybackException: java.io.IOException: Failed to instantiate extractor.

代码 -

loadvideo("res:/" + R.raw.samplevider);


private void loadvideo(String path) {
    player = ExoPlayer.Factory.newInstance(2, 1000, 5000);
    Uri uri = Uri.parse(path);
    sampleSource = new FrameworkSampleSource(getActivity().getApplicationContext(), uri, null);
    // 1. Instantiate the player.
    // 2. Construct renderers.
    videoRenderer = new MediaCodecVideoTrackRenderer(getActivity().getApplicationContext(),sampleSource, MediaCodecSelector.DEFAULT, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,MediaCodecSelector.DEFAULT);
    // 3. Inject the renderers through prepare.
    player.prepare(videoRenderer, audioRenderer);
    // 4. Pass the surface to the video renderer.
    player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surfaceView.getHolder().getSurface());
    // 5. Start playback.
    player.setPlayWhenReady(true);
}

1 个答案:

答案 0 :(得分:0)

试试这个:

        private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
        private static final int BUFFER_SEGMENT_COUNT = 256;
        private static final int RENDERER_COUNT = 3;

        Uri uri = Uri.parse(getAVideoUri());

        Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);

        DataSource dataSource = new DefaultUriDataSource(this, null, getUserAgent(this, "ExoPlayerExample"));

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

        MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(
                    this, sampleSource, MediaCodecSelector.DEFAULT, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);
        MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(
                    sampleSource, MediaCodecSelector.DEFAULT);
        player = ExoPlayer.Factory.newInstance(RENDERER_COUNT);

        player.prepare(videoRenderer, audioRenderer, secondAudioRenderer);
        player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surfaceView.getHolder().getSurface());
        player.setPlayWhenReady(true);