【问题标题】:How to use ExoPlayer如何使用 ExoPlayer
【发布时间】:2016-01-26 15:16:55
【问题描述】:

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

【问题讨论】:

  • 请参考link 寻求解决方案,希望对您有所帮助
  • 最简单的方法是将 jcenter 添加到您的构建 gradle 存储库中,然后添加 com.google.android.exoplayer:exoplayer 作为依赖项。
  • 嗯...我的意思是样本中有很多文件,我不知道应该使用什么。

标签: android exoplayer


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢,我正在使用 compile 'com.google.android.exoplayer:exoplayer:r1.5.1',但我不知道如何将 exoplayer 添加到我的应用程序中并在其上播放视频。
  • 你将不得不在github.com/google/ExoPlayer/tree/master/demo中挖掘自己
  • 是的,我的应用现在正在使用 exoplayer 播放视频。无论如何,非常感谢!
  • 如果您认为这是一个正确的答案@NaPro,请随时接受我的回答
  • 是的,我想为你的答案投票,但我没有足够的声望,抱歉。
【解决方案2】:

老问题,但由于简单的 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 秒音频的问题。

【讨论】:

猜你喜欢
  • 2015-12-25
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
相关资源
最近更新 更多