使用ExoPlayer播放HLS

时间:2015-09-01 18:55:01

标签: android exoplayer

我试图通过扩展SurfaceView并将ExoPlayer视频渲染到其表面来创建一个非常简单的视图。我想支持HLS和仅HLS。我似乎始终保持音频,但我从未看到过呈现在屏幕上的视频。我很好奇我可能做错了什么。

我在Logcat中看到的唯一错误如下:

E/OMXMaster﹕ A component of name 'OMX.qcom.audio.decoder.aac' already exists, ignoring this one.

以下是我的代码。

private static final int BUFFER_SEGMENT_SIZE = 256 * 1024;
private static final int BUFFER_SEGMENTS = 64;

private ExoPlayer mExoPlayer;
private Handler mHandler;
private AudioCapabilitiesReceiver mAudioCapabilitiesReceiver;
private AudioCapabilities mAudioCapabilities;
private ManifestFetcher<HlsPlaylist> playlistFetcher;
private String mUserAgent;
String url = "http://solutions.brightcove.com/bcls/assets/videos/Great-Blue-Heron.m3u8";

public ExoPlayerView(Context context) {
    super(context);
}

public ExoPlayerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ExoPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void init() {
    mHandler = new Handler();
    mUserAgent = Util.getUserAgent(getContext(), "CX Video Player");
    HlsPlaylistParser parser = new HlsPlaylistParser();
    playlistFetcher = new ManifestFetcher<>(url, new DefaultUriDataSource(getContext(), mUserAgent),
            parser);
    mAudioCapabilitiesReceiver = new AudioCapabilitiesReceiver(getContext(), this);
    mAudioCapabilitiesReceiver.register();
}

@Override
public void play() {
    mExoPlayer.setPlayWhenReady(true);
}

@Override
public void stop() {
    mExoPlayer.stop();
    release();
}

@Override
public void pause() {
    mExoPlayer.setPlayWhenReady(false);
}

@Override
public void seekTo(long timeMillis) {
    mExoPlayer.seekTo(timeMillis);
}

@Override
public long getCurrentPosition() {
    return mExoPlayer.getCurrentPosition();
}

@Override
public boolean isPlaying() {
    return false;
}

@Override
public void playNext() {

}

@Override
public boolean isPlayingLastVideo() {
    return false;
}

@Override
public int getDuration() {
    return (int)mExoPlayer.getDuration();
}

@Override
public void addVideo(Uri uri) {

}

@Override
public void addVideos(List<Uri> uris) {

}

@Override
public void release() {
    mAudioCapabilitiesReceiver.unregister();
    mExoPlayer.release();
}

@Override
public void onSingleManifest(HlsPlaylist hlsPlaylist) {
    final int numRenderers = 2;

    LoadControl loadControl = new DefaultLoadControl(new DefaultAllocator(BUFFER_SEGMENT_SIZE));
    DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

    int[] variantIndices = null;
    if (hlsPlaylist instanceof HlsMasterPlaylist) {
        HlsMasterPlaylist masterPlaylist = (HlsMasterPlaylist) hlsPlaylist;
        try {
            variantIndices = VideoFormatSelectorUtil.selectVideoFormatsForDefaultDisplay(
                    getContext(), masterPlaylist.variants, null, false);
        } catch (MediaCodecUtil.DecoderQueryException e) {
            e.printStackTrace();
            return;
        }
        if (variantIndices.length == 0) {
            return;
        }
    }

    DataSource dataSource = new DefaultUriDataSource(getContext(), bandwidthMeter, mUserAgent);
    HlsChunkSource hlsChunkSource = new HlsChunkSource(dataSource, url, hlsPlaylist, bandwidthMeter,
            variantIndices, HlsChunkSource.ADAPTIVE_MODE_SPLICE, mAudioCapabilities);
    HlsSampleSource hlsSampleSource = new HlsSampleSource(hlsChunkSource, loadControl, BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE);

    // Build the track renderers
    TrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(hlsSampleSource, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    TrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(hlsSampleSource);

    // Build the ExoPlayer and start playback
    mExoPlayer = ExoPlayer.Factory.newInstance(numRenderers);
    mExoPlayer.prepare(videoRenderer, audioRenderer);

    // Pass the surface to the video renderer.
    mExoPlayer.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, getHolder().getSurface());

    mExoPlayer.setPlayWhenReady(true);
}

@Override
public void onSingleManifestError(IOException e) {

}

@Override
public void onAudioCapabilitiesChanged(AudioCapabilities audioCapabilities) {
    mAudioCapabilities = audioCapabilities;
    playlistFetcher.singleLoad(mHandler.getLooper(), this);
}
}

2 个答案:

答案 0 :(得分:1)

So I found my issue. After going back through the demo code, I noticed that the demo SurfaceView was wrapped in com.google.android.exoplayer.AspectRatioFrameLayout. After wrapping my SurfaceView in this same layout, the video magically started playing.

答案 1 :(得分:0)

我有类似的情况,我可以听到音频,但没有视频,只是一个黑屏。结果我试图发送设置表面mesagge:

player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface);

在视频渲染实际初始化并准备就绪之前,这就是为什么我只能听到音频