如何使用ExoPlayer

How to use ExoPlayer

我想在我的应用中使用 ExoPlayer。你能告诉我哪个是最简单的例子吗?我尝试过可能 https://github.com/google/ExoPlayer/ 但这对我来说并不容易。我尝试将 library 作为模块导入,然后收到 bintray-release 错误。

如主要 Readme.md 中所述,您可以像导入任何其他依赖项一样导入 ExoPlayer:

在您的应用中 build.gradle > dependencies 添加:

compile 'com.google.android.exoplayer:exoplayer:rX.X.X'

截至 2015 年 10 月 27 日,当前版本为 r1.5.1。请参阅 here

老问题,但由于简单的 ExoPlayer 教程太少,所以我写了这个。我最近将一个应用程序从使用 Android 的默认媒体播放器转换为 ExoPlayer。性能提升是惊人的,它适用于更广泛的设备。然而,它有点复杂。

此示例专为播放 http 音频流而设计,但通过试验,您可能可以轻松地将其调整为适用于任何其他内容。本例使用ExoPlayer最新v1.xx,目前v1.5.11:

首先,将其放入您的 build.gradle(模块:app)文件中,在 "dependencies":

compile 'com.google.android.exoplayer:exoplayer:r1.5.11'

您的 class 也应该实施 ExoPlayer.Listener:

...implements ExoPlayer.Listener

下面是播放 http 音频流的相关代码:

    private static final int RENDERER_COUNT = 1; //since we want to render simple audio
    private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; // for http mp3 audio stream use these values
    private static final int BUFFER_SEGMENT_COUNT = 256; // for http mp3 audio steam use these values
    private ExoPlayer exoPlayer;

// for http mp3 audio stream, use these values
    int minBufferMs = 1000;
    int minRebufferMs = 5000;

    // Prepare ExoPlayer
    exoPlayer = ExoPlayer.Factory.newInstance(RENDERER_COUNT, minBufferMs, minRebufferMs);

    // String with the url of the stream to play
    String stream_location = "http://audio_stream_url";        
    // Convert String URL to Uri
    Uri streamUri = Uri.parse(stream_location);

    // Settings for ExoPlayer
    Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
    String userAgent = Util.getUserAgent(ChicagoPoliceRadioService.this, "ExoPlayer_Test");
    DataSource dataSource = new DefaultUriDataSource(ChicagoPoliceRadioService.this, null, userAgent);
    ExtractorSampleSource sampleSource = new ExtractorSampleSource(
            streamUri, dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT);
    MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource, MediaCodecSelector.DEFAULT);

    // Attach listener we implemented in this class to this ExoPlayer instance
    exoPlayer.addListener(this);

    // Prepare ExoPlayer
    exoPlayer.prepare(audioRenderer);

    // Set full volume
    exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 1f);

    // Play!
    exoPlayer.setPlayWhenReady(true);

回调方式一共有三种:

 @Override
 public void onPlayWhenReadyCommitted() {

    // No idea what would go here, I left it empty

}

// Called when ExoPlayer state changes
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

    // If playbackState equals STATE_READY (4), that means ExoPlayer is set to
    // play and there are no errors
    if (playbackState == ExoPlayer.STATE_READY) {
        // ExoPlayer prepared and ready, no error
        // Put code here, same as "onPrepared()"
       }
}

// Called on ExoPlayer error
@Override
public void onPlayerError(ExoPlaybackException error) {
    // ExoPlayer error occurred
    // Put your error code here
}

当你玩完之后照常做:

if (exoPlayer != null) {
                exoPlayer.stop();
                exoPlayer.release();
            }

注意:我仍然不能 100% 确定所有 ExoPlayer 设置的细节。我从未尝试过播放视频。注意这是针对ExoPlayer 1.5.x版本的,2.0改了很多,我还没弄明白。我强烈推荐此代码给任何拥有从网络流式传输音频的应用程序的人,因为性能提升令人难以置信,并且对于我的应用程序,它解决了三星手机在停止前只能播放大约 30 秒音频的问题。