Android 2.2引入了称作AudioFocus的机制来管理对Audio资源的竞争的管理与协调。 按照AudioFocus的机制,在使用AudioStream之前,需要申请AudioFocus,在获得AudioFocus之后才可以使用相应的AudioStream;如果有别的程序竞争你正在使用的AudioStream,你的程序需要在收到通知之后做停止播放或者降低声音的处理。该机制需要合作完成。
1.音频的播放
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
}
2.播放完成的释放
// Abandon audio focus when playback complete
am.abandonAudioFocus(afChangeListener);
3.Focus改变的监听
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT //临时失焦
// Pause playback
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { //获取焦点
// Resume playback
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
public static void init(Context context) {
mContext = context;
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int result = am.requestAudioFocus(, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
startPlay();
}
}
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {//临时失焦
// Pause playback
pausePlay();
}
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { //获取焦点
// Resume playback
resumePlay();
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
am.abandonAudioFocus(afChangeListener);
// Stop playback
stopPlay();
}
}
};
package com.utility.helper;
import com.utility.common.LogHelper;
import android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
public class AudioFocusHelper {
private static Context mContext;
private static final String TAG = "AudioFocusHelper";
private volatile static AudioFocusHelper instance; // 实例
private OnAudioFocusChangeListener mAudioFocusChangeListener;
private AudioManager mAudioManager;
private static final int AUDIO_NO_FOCUS_NO_DUCK = 0;
private static final int AUDIO_FOCUSED = 2;
private int mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
public static AudioFocusHelper getInstance(Context context) {
mContext = context;
if (instance == null) {
synchronized (AudioFocusHelper.class) {
if (instance == null) {
instance = new AudioFocusHelper();
}
}
}
return instance;
}
private AudioFocusHelper() {
if(null != mContext){
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
}
/**
* 设置 audiofocus changelistener,只调一次
*/
public void setAudioFocusChangeListener(OnAudioFocusChangeListener listener){
if(null == mAudioManager && null != mContext){
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
mAudioFocusChangeListener = listener;
LogHelper.releaseLog(TAG, "setAudioFocusChangeListener");
}
/**
* get AudioFocus,播放前调用,需先调用setAudioFocusChangeListener
* return true: focus success
*/
public boolean startFocus(){
LogHelper.releaseLog(TAG, "startFocus");
if(null != mAudioManager && mAudioFocus != AUDIO_FOCUSED){
int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mAudioFocus = AUDIO_FOCUSED;
}
}
if(mAudioFocus == AUDIO_FOCUSED){
return true;
}else{
return false;
}
}
/**
* give up AudioFocus,播放完成调用
* return true: stop success
*/
public boolean stopFocus(){
LogHelper.releaseLog(TAG, "stopFocus");
if (null != mAudioManager && mAudioFocus == AUDIO_FOCUSED) {
if (mAudioManager.abandonAudioFocus(mAudioFocusChangeListener) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
}
}
if(mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK){
return true;
}else{
return false;
}
}
/**
* 释放资源
*/
public void release(){
mAudioManager = null;
instance = null;
}
}
一、说明
Helios应用在播放音频时需要统一使用CommonSDK中的AudioFocusHelper获取AudioStream,并设置监听,实用完成后释放AudioStream.
权限:android.permission.MODIFY_AUDIO_SETTINGS
二、使用方法
1.设置OnAudioFocusChangeListener监听,方法如下:
AudioFocusHelper.getInstance(mContext).setAudioFocusChangeListener(OnAudioFocusChangeListener listener);
2.播放前调用startFocus方法,如下:
AudioFocusHelper.getInstance(getApplicationContext()).startFocus();
返回true:获取AudioStream成功,false:失败
3.播放完成后调用stopFocus方法,如下:
AudioFocusHelper.getInstance(getApplicationContext()).stopFocus();
返回true:释放AudioStream成功,false:失败
三、OnAudioFocusChangeListener监听说明
示例:
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_GAIN ) {
//获取焦点
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
) {
//失去焦点
}
}
四、注意
开始播放音频前,需要调用startFocus,返回成功即可播放,失败不播放。结束播放时,需要调用stopFocus。