Android在使用AudioFocus后,当短暂失去Focus要怎么重新获得

求教,关于音频处理问题,我的背景音乐播放的时候,使用的AudioFocus的方法来处理,当我有其他音效要播放的时候,我就把背景音乐改小,但是这就出来了问题,改小了就不会变回原本的音量了,经过@四方城 的指点,说要在音效播放完后再让背景音乐重新获取焦点(应该是再requestAudioFocus一次?).但是现在这个我想问一下,我音效跟背景音乐都不是放在同一个java文件里面的,那我要怎么去让背景重新获取焦点?把背景音乐的mediaplayer对象设成公开的来修改?还是设置成静态来修改?还是有其他什么方法吗?

好吧,还是我来抽时间给你回答下,先给你贴一段Sample Code吧,你把下面的代码跑起来之后,打电话进来试试看,然后不接,把电话挂了。这是为了模拟AudioFocus的获取,丢失与重新获取。

【1】Sample Code

public class MainActivity extends Activity{
    
    public AudioManager mAudioManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        
        Button button1 = (Button)this.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mAudioManager.requestAudioFocus(mAudioFocusListener,
                        AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
                Log.d("Test", "requestAudioFocus: onClick");
            }
        });
        
        Button button2 = (Button)this.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mAudioManager.abandonAudioFocus(mAudioFocusListener);
                Log.d("Test", "abandonAudioFocus: onClick");
            }
        });
        
        
    }
    
    public AudioManager.OnAudioFocusChangeListener mAudioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
        public void onAudioFocusChange(int focusChange) {
            // AudioFocus is a new feature: focus updates are made verbose on purpose
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS:
                    Log.d("Test", "AudioFocus: received AUDIOFOCUS_LOSS");
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    Log.d("Test", "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    Log.d("Test", "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                    break;
                case AudioManager.AUDIOFOCUS_GAIN:
                    Log.d("Test", "AudioFocus: received AUDIOFOCUS_GAIN");
                    break;
                default:
                    Log.d("Test", "Unknown audio focus change code");
            }
        }
    };
}

【2】那么你看的Log应该是:
电话打进来的时候出现: AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT
电话挂掉的时候出现:AudioFocus: received AUDIOFOCUS_GAIN

【3】所以,你现在应该理解了。至于具体它是如何在另外一个App释放的时候,自己重新获取的,需要去看AudioManger的原代码。在你不了解它为何会这样的时候,写个小的程序验证一下,就知道了。

【4】Addition : 其实Android没有强制每个App必须去遵守AudioFocus的使用规范,可能会出现有几个App同时发出声音的情况。但是大多数App都需要自动去那样做,需要的时候请求,用完的时候释放,这样才能保证有良好的使用体验。

你可能感兴趣的:(Java/Android)