我在使用AudioFocusChangeListener时遇到问题,但仍然无法收听音频焦点更改。有人可以帮我找到原因吗?这是我的工作:

  • 在我的FM应用程序中,当FM在startFM中启动时,我将requestAudioFocus()放入以获得资源:
    int result = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_FM,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        Log.d(LOGTAG, "Successfull to request audioFocus listener");
    } else {
        Log.d(LOGTAG, "Failure to request focus listener");
    }
    
  • onDestroy()退出应用程序时,我使用abandonAudioFocus()释放资源。
  • 成员变量mAudioFocusListener如下:
    private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS:
                Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS, turning FM off");
    
                if (isFmOn()) {
                    fmOff();
                }
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
    
                if (isFmOn()) {
                    Log.d(LOGTAG, "AudioFocus: FM is on, turning off");
                    mute();
                    stopFM();
                }
                break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
    
                if (isFmOn()) {
                    Log.d(LOGTAG, "AudioFocus: FM is on, turning off");
                    mute();
                    stopFM();
                }
                break;
            case AudioManager.AUDIOFOCUS_GAIN:
                Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_GAIN");
    
                if (isFmOn()) {
                    Log.d(LOGTAG, "AudioFocus: FM is off, turning back on");
                    unMute();
                    startFM();
                }
                break;
            default:
                Log.e(LOGTAG, "Unknown audio focus change code " + focusChange);
        }
    }
    };
    
  • 根据该日志,它会显示“请求AudioFocus监听器已成功完成”,但是在警报打开或其他音频更改事件发生时,不打印任何AudioForcus更改日志。谁能弄清楚这是怎么回事?
  • 另一方面,我将日志放置在AudioService.java和AudioManger.java中,其中在AudioService.java中,requestAudioFocus():
    如果(!mFocusStack.empty()&&(mFocusStack.peek()。mFocusDispatcher!= null)){
           try {
    
                mFocusStack.peek().mFocusDispatcher.dispatchAudioFocusChange(
    
                         -1 * focusChangeHint, // loss and gain codes are inverse of each other
    
                         mFocusStack.peek().mClientId);  // This doesn't excute
    
            } catch (RemoteException e) {
    
                Log.e(TAG, " Failure to signal loss of focus due to "+ e);
                e.printStackTrace();
    
             }
    
         }
    
  • AudioManager.java中的
  • ,这里也不执行:
    私有(private)IAudioFocusDispatcher mAudioFocusDispatcher = new IAudioFocusDispatcher.Stub(){
    public void dispatchAudioFocusChange(int focusChange, String id) {
    
        Message m = mAudioFocusEventHandlerDelegate.getHandler().obtainMessage(focusChange, id);
    
         mAudioFocusEventHandlerDelegate.getHandler().sendMessage(m);
    
     }
    

  • 这就是我的全部问题。谁能告诉我为什么这个收听者不能收听警报事件和其他音频焦点改变的事件?顺便说一句,我如何才能成功收听事件?也许我错过了一些步骤或使用了错误的方法?非常感谢。

    最佳答案

    试试这个:

    private class MyService extends Service implements AudioManager.OnAudioFocusChangeListener {
        public void onAudioFocusChange(int focusChange) {
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS, turning FM off");
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    Log.e(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                    Log.v(TAG, "AudioFocus: received AUDIOFOCUS_GAIN");
                    break;
                default:
                    Log.e(TAG, "Unknown audio focus change code " + focusChange);
            }
        }
    
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
    

    和...
    mAudioManager.requestAudioFocus(new MyService(), AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    

    它对我有用。

    09-12 03:54