手机后台播放音乐时,设置手机铃声,后台音乐不会暂停,此现象的为设置手机铃声界面,并没要加入播放的foucs机制,
此修改在packages\providers\MediaProvider\src\com\android\providers\media\RingtonePickerActivity.java中
import android.media.AudioManager; import android.content.Context; import android.media.AudioManager.OnAudioFocusChangeListener;
private AudioManager mAudioManager = null; private boolean mGetFocus = false; private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { MtkLog.d(TAG, "onAudioFocusChange:" + focusChange ); switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: stopAnyPlayingRingtone(); break; } } };
protected void onCreate(Bundle savedInstanceState) {
........
mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
........
}
public void run() {
stopAnyPlayingRingtone(); if (mSampleRingtonePos == mSilentPos) { return; } /* * Stop the default ringtone, if it's playing (other ringtones will be * stopped by the RingtoneManager when we get another Ringtone from it. */ if (mDefaultRingtone != null && mDefaultRingtone.isPlaying()) { mDefaultRingtone.stop(); mDefaultRingtone = null; } Ringtone ringtone; if (mSampleRingtonePos == mDefaultRingtonePos) { /// M: if default URI is "", it means silent if default item selected. @{ /* if (mUriForDefaultItem.toString().isEmpty()) { return; } ///@} if (mDefaultRingtone == null) { mDefaultRingtone = RingtoneManager.getRingtone(this, mUriForDefaultItem); }*/ /* * Stream type of mDefaultRingtone is not set explicitly here. * It should be set in accordance with mRingtoneManager of this Activity. */ if (mDefaultRingtone != null) { mDefaultRingtone.setStreamType(mRingtoneManager.inferStreamType()); } ringtone = mDefaultRingtone; mCurrentRingtone = null; } else { ringtone = mRingtoneManager.getRingtone(getRingtoneManagerPosition(mSampleRingtonePos)); mCurrentRingtone = ringtone; } if (ringtone != null) { int result=mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { MtkLog.d(TAG, " request audio focus fail"); mGetFocus = false; } else { MtkLog.d(TAG, " request audio focus success"); mGetFocus = true; } if(mAudioManager.getMode() == AudioManager.MODE_RINGTONE){ return; } if (mAttributesFlags != 0) { ringtone.setAudioAttributes( new AudioAttributes.Builder(ringtone.getAudioAttributes()) .setFlags(mAttributesFlags) .build()); } ringtone.play(); } }
protected void onPause() { super.onPause();if (mGetFocus && (null != mAudioManager) ) { if (AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(mAudioFocusListener)) { MtkLog.d(TAG, "<abandonAudioFocus()> abandon audio focus success"); mGetFocus = false; } else { MtkLog.d(TAG, "<abandonAudioFocus()> abandon audio focus failed"); mGetFocus = true; } } if (!isChangingConfigurations()) { stopAnyPlayingRingtone(); } mNeedRefreshOnResume = true; }加入以上红色的代码即可解决此问题