安卓:MediaPlayer的AUDIOFOCUS_LOSS和放大器; setOnErrorListener()问题放大器、问题、MediaPlayer、AUDIOFOCUS_LOSS

2023-09-06 19:11:38 作者:逝去的爱

我要创建一个音乐播放器应用程序。

I'm creating a music player app.

在,我检查,如果我的应用程序失去AudioFocus则播放将停止。 但是,这是提高一个问题,当我播放曲目 - 然后停止它, - 然后重新播放曲目,开关的情况下AudioManager.AUDIOFOCUS_LOSS 获取调用中,我已经停止播放器,因为它失去焦点。

In that I am checking if my application loses AudioFocus then the playback will stop. But this is raising one issue that when I play a track - then stop it and - then play a track again, switch case AudioManager.AUDIOFOCUS_LOSS is getting called in which I've stopped the player as it is losing focus.

所以,我自己的应用程序失去焦点本身。 显然,这些不是我的意图。 我希望它被调用时,一些其他的音乐播放器所需的焦点。

So my own app is losing focus to itself. Obviously, these were not my intentions. I wanted it to get called when some other music player requested focus.

private OnAudioFocusChangeListener focusChangeListener = new OnAudioFocusChangeListener() {
    @Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
        case AudioManager.AUDIOFOCUS_LOSS:

            if (mPlayer.isPlaying()) {
                stopPlayback();
            }
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            if (mPlayer.isPlaying()) {
                mPlayer.pause();
            }
            break;
        case AudioManager.AUDIOFOCUS_GAIN:
            if (!mPlayer.isPlaying()) {
                initiatePlayback();
            }
            break;
        }
    }
};

还有一个问题,异常是我得到的 IllegalStateException异常

There is one more problem, Exception that I was getting is IllegalStateException.

这意味着当code里面的情况下AudioManager.AUDIOFOCUS_LOSS达到,我musicplayer在一些错误的状态。 因此,要检查,我注册了一个 OnErrorListener 使用:

This means when the code reaches inside case AudioManager.AUDIOFOCUS_LOSS, my musicplayer is in some erroneous state. So to check that, I registered an OnErrorListener using:

//Inside Service - onCreate() Method
mPlayer.setOnErrorListener(mErrorListener);

但这个监听器永远不会被调用。 我将AP preteate任何帮助。

But this listener is never gets called. I'll appreteate any help.

推荐答案

我有类似的问题,我强烈怀疑你的 focusChangeListener 的地方重新创建你的程序,所以你要求专注于 focusChangeListener 不同的实例。你是从一个服务或活动定义它?

I had similar issue, I strongly suspect that your focusChangeListener is recreated somewhere in your program so you request focus on different instance of focusChangeListener. Are you defining it from a service or activity?

这样做是为了保持你的 focusChangeListener 您传递给 requestAudioFocus()和 abandonAudioFocus();否则AudioManager将考虑从不同的源的请求,这可能会导致该应用可以失去焦点本身

The idea is to maintain a single copy of your focusChangeListener that you pass to requestAudioFocus() and abandonAudioFocus(); otherwise the AudioManager will consider the request from different source, which can cause the app can lose focus to itself.

您应该知道,叫 requestAudioFocus() 同一个侦听器对象很多时候并注册它的只有一次的并呼吁 abandonAudioFocus()一次就足以释放音频焦点。

You should know that calling requestAudioFocus() on the same listener object many times does register it only once and calling abandonAudioFocus() one time is sufficient to release the audio focus.

这能够解决您的问题提出几点建议:

Few suggestions that can solve your problem:

尝试定义 focusChangeListener 的静态对象,看看帮助。

Try to define focusChangeListener as static object and see if help.

如果您的侦听器已注册的活动,考虑到移动到一个服务,使听众可 存活更长的时间。

If your listener is registered in Activity, consider to move to a service so the listener can survive longer.

要调试这个问题,这是非常有帮助的记录下面** **时您请求或放弃的焦点,以确保你有,以确保您正在处理相同的实例:

To debug this issue, it is very helpful to log the following ** whenever ** you request or abandon focus to ensure that you have, to ensure that you are dealing with the same instance:

您focusChangeListener如 focusChangeListener.hash code() 在当前上下文对象如 context.hash code() 在当前线程ID例如 Thread.currentThread()的getId() Your focusChangeListener e.g. focusChangeListener.hashCode() Current context object e.g. context.hashCode() Current thread id e.g. Thread.currentThread().getId()

例如:

  Log.d(TAG, String.format("Request audio focus: thread id = %s, context = %s, listener = %s",Thread.currentThread().getId(),context.hashCode(),focusChangeListener hashCode())

编辑:

使用应用程序上下文,即 getApplicationContext()就可以解决问题为好。

Using application context i.e. getApplicationContext() can solve the issue as well.