手機後臺播放音樂時,設置手機鈴聲,後臺音樂不會暫停,此現象的爲設置手機鈴聲界面,並沒要加入播放的foucs機制,java
此修改在packages\providers\MediaProvider\src\com\android\providers\media\RingtonePickerActivity.java中android
import android.media.AudioManager;
import android.content.Context;
import android.media.AudioManager.OnAudioFocusChangeListener;ide
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;
}
}
};ui
@Override
protected void onCreate(Bundle savedInstanceState) {this
........spa
mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);.net
........ci
}get
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();
}
}it
@Override
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;
}
加入以上紅色的代碼便可解決此問題