Android-具有音频焦点的SimpleExoPlayer [英] Android - SimpleExoPlayer with audio focus

查看:138
本文介绍了Android-具有音频焦点的SimpleExoPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SimpleExoPlayer实现了一个测试应用程序,该应用程序运行良好.我现在想将audiofocus添加到我的应用中.

I implemented a test app using SimpleExoPlayer which works fine. I would like now to add audiofocus to my app.

在用户启动应用程序时开始播放音频焦点很容易.在onCreate()的末尾添加普通代码:

To start playing with audio focus when the user launches the app is easy. Adding the usual code at the end of onCreate() :

        if (AudioManager.AUDIOFOCUS_REQUEST_GRANTED == audioManager.requestAudioFocus(...

工作正常.

但是当用户按下播放按钮时,我有一个请求音频焦点的问题,该按钮由我记得的PlaybackControlView封装.

But I have problem to request the audiofocus when the user presses the play button, which is encapsulated by the PlaybackControlView I recall.

换句话说,当使用SimpleExoPlayer和SimpleExoPlayerView(PlaybackControlView)时,如何捕获播放按钮单击事件?

Is other words, how can I capture the play button click event when using SimpleExoPlayer and SimpleExoPlayerView (PlaybackControlView)?

重新定义simpleExoPlayer.setPlayWhenReady()似乎是不可能的,因为SimpleExoPlayer的实例是由工厂提供的,而不是通过使用new关键字实例化的.

Redifining simpleExoPlayer.setPlayWhenReady() does not seem possible because the instance of SimpleExoPlayer is given by a Factory, not from instantation with new keyword.

simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(...)

除了使用播放"按钮解决此问题外,还有其他想法吗?预先感谢.

Any other ideas than solving this problem using the play button? Thanks in advance.

更新(17/02/17):

UPDATE (17/02/17) :

此问题在github.com/google/ExoPlayer上被标记为ENHANCEMENT.此处

This issue on github.com/google/ExoPlayer is labelled as ENHANCEMENT. here

他们的第一个答案是:

您可以通过将侦听器添加到ExoPlayer实例(ExoPlayer.addListener)并实现侦听器的onPlayerStateChanged来获取或释放音频焦点(通过执行此操作)来实现.请注意,只要更改playWhenReady,就会调用onPlayerStateChanged.

You can do this by adding a listener to the ExoPlayer instance (ExoPlayer.addListener) and implementing the listener's onPlayerStateChanged to obtain or release audio focus as necessary. Note that onPlayerStateChanged will be called whenever playWhenReady is changed.

此解决方案的小问题是onPlayerStateChanged()是在音频/视频开始播放之后而不是在开始播放之前调用的.我实现了它,当两个音频同时播放时,很短的重叠当然不会从耳朵上注意到.因此,如果需要,可以将其视为一个很好的解决方案.如果有/否,ExoPlayer团队会提供更好的解决方案,我会告诉您.

The small problem with this solution is that onPlayerStateChanged() is called after the audio/video starts playing, not before. I implemented it and the very short overlapping when both audios play at the same time is not noticeable from the ear of course. So it could be considered as a good solution if you need. I'll let you know when/if the ExoPlayer team comes with a better solution.

推荐答案

ExoPlayer设计团队已在下面引用的补丁中解决了该问题:

ExoPlayer design team fixed the issue in the patch ref'd below :

修补程序以拦截播放/暂停通话

这篇关于Android-具有音频焦点的SimpleExoPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆