5

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

4

2 回答 2

4

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

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

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

当前版本r1.5.1截至 2015 年 10 月 27 日。请参见此处

于 2015-10-27T09:35:54.150 回答
3

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

这个例子是专门为播放一个 http 音频流而定制的,但是通过实验你可以很容易地将它适应其他任何东西。本例使用 ExoPlayer 最新的 v1.xx,目前为 v1.5.11:

首先,把它放在你的 build.gradle (Module: app) 文件中,在“dependencies”下:

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

你的班级也应该实现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 秒音频的问题。

于 2016-09-28T01:23:08.073 回答