android 音頻處理mediaplay介紹
// is likely to resume
本文引用地址:http://www.ex-cimer.com/article/201609/305045.htmif (mMediaPlayer.isPlaying()) mMediaPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Lost focus for a short time, but it's ok to keep playing
// at an attenuated level
if (mMediaPlayer.isPlaying()) mMediaPlayer.setVolume(0.1f, 0.1f);
break;
}
}
記住音頻焦點API僅在APIlevel 8 (Android2.2)及更高版本上可以,所以如果你想支持更早的Android版本,你必須在可能時采取兼容性的策略使用特性,如果不可能,you should adopt a backward compatibility strategy that allows you touse this feature if available, and fall back seamlessly if not.
你可以用反射的方式調(diào)用音頻焦點方法或自己在一個單獨的類(叫做AudioFocusHelper)中實現(xiàn)所有的音頻焦點功能來達到向前兼容.下面是一個這樣的類:
[java]
public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {
AudioManager mAudioManager;
// 這里是其它的字段,你可能要保存一個接口的引用,這個接口
// 被用于與你的service通訊以報告焦點的變化.
public AudioFocusHelper(Context ctx, ) {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
// ...
}
public boolean requestFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAudioManager.requestAudioFocus(mContext, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
public boolean abandonFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAudioManager.abandonAudioFocus(this);
}
@Override
public void onAudioFocusChange(int focusChange) {
// 讓你的service知道焦點變化了
}
}
你可以僅在檢測到系統(tǒng)運行的是API level 8 或更早的版本時才創(chuàng)建AudioFocusHelper類的實例.例如:
if (android.os.Build.VERSION.SDK_INT >= 8) {
mAudioFocusHelper = new AudioFocusHelper(getApplicationContext(), this);
} else {
mAudioFocusHelper = null;
}
public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {
AudioManager mAudioManager;
// 這里是其它的字段,你可能要保存一個接口的引用,這個接口
// 被用于與你的service通訊以報告焦點的變化.
public AudioFocusHelper(Context ctx, ) {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
// ...
}
public boolean requestFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAudioManager.requestAudioFocus(mContext, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
public boolean abandonFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAudioManager.abandonAudioFocus(this);
}
@Override
public void onAudioFocusChange(int focusChange) {
// 讓你的service知道焦點變化了
}
}
你可以僅在檢測到系統(tǒng)運行的是API level 8 或更早的版本時才創(chuàng)建AudioFocusHelper類的實例.例如:
if (android.os.Build.VERSION.SDK_INT >= 8) {
mAudioFocusHelper = new AudioFocusHelper(getApplicationContext(), this);
} else {
mAudioFocusHelper = null;
}
清理
前面提到過,一個MediaPlayer對象可以消耗掉大量的系統(tǒng)資源,所以你應該僅在需要它時保持它并在用完時立即釋放.明確的調(diào)用清理方法而不是依靠系統(tǒng)的垃圾收集機制是很重要的,因為在被收集之前MediaPlayer可能會存在很長時間,雖然此時它只是占用內(nèi)存而不影響其它的媒體相關(guān)的資源.所以,當你使用一個service時,你應該總四重寫onDestroy()方法來保證釋放MediaPlayer:
[java]
public class MyService extends Service {
MediaPlayer mMediaPlayer;
// ...
@Override
public void onDestroy() {
if (mMediaPlayer != null) mMediaPlayer.release();
}
}
public class MyService extends Service {
MediaPlayer mMediaPlayer;
// ...
@Override
public void onDestroy() {
if (mMediaPlayer != null) mMediaPlayer.release();
}www.2cto.com
}
你也應該尋找其它需要釋放你的MediaPlayer的時機.例如,如果你預料到長時間不能播放媒體(比如丟掉音頻焦點以后),你應該明確地釋放你的MediaPlayer,然后在后面重新創(chuàng)建它.反過來,如果你預測到只會短時間停止播放,你應該保持你的MediaPlayer來避免過多的創(chuàng)建,而不是重新準備它.
評論