private static int changedFocus;
private static boolean focusGranted;
private static AudioManager audioManager;
public static int getChangedFocus() {
return changedFocus;
}
public static boolean isFocusGranted() {
return focusGranted;
}
public static void requestFocus(final Context context) {
if (audioManager == null) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
switch (audioManager.requestAudioFocus(OnFocusChangeListener.getInstance(),
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)) {
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
focusGranted = true;
break;
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
focusGranted = false;
break;
}
}
private static final class OnFocusChangeListener implements AudioManager.OnAudioFocusChangeListener {
private static OnFocusChangeListener instance;
protected static OnFocusChangeListener getInstance() {
if (instance == null) {
instance = new OnFocusChangeListener();
}
return instance;
}
@Override
public void onAudioFocusChange(final int focusChange) {
changedFocus = focusChange;
System.out.println("on focus change: " + focusChange);
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
GlobalMusicPlayer.onFocusGained();
break;
case AudioManager.AUDIOFOCUS_LOSS:
GlobalMusicPlayer.onFocusLoss();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
GlobalMusicPlayer.onFocusLossTransient();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
GlobalMusicPlayer.onFocusLossTransientCanDuck();
break;
}
}
}
我调用requestFocus(context)并获取AudioManager.AUDIOFOCUS_REQUEST_GRANTED作为返回值。
之后, onAudioFocusChange 从未调用过,因此 changedFocus 始终为0。
isFocusGranted() // true
getChangedFocus() // 0
设备:Nexus 10,Motorola Xoom。
答案 0 :(得分:6)
case AudioManager.AUDIOFOCUS_GAIN
中的 onAudioFocusChange()
更多是AudioFocus的重新获得。它只会在丢失事件后触发。
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED
是您应该添加其他GlobalMusicPlayer.onFocusGained()
。
答案 1 :(得分:1)
实际上我看不出你的代码有什么问题所以我创建了一些小的测试活动。
要测试音频焦点监听器,您必须开始此活动,使用“主页”按钮将其最小化,然后启动一些音乐播放器(在模拟器上使用库存测试) - 您应该能够尽快看到'Focus LOST'吐司随着音乐开始播放。当您停止播放音乐时 - 您应该看到'Focus GAINED'消息。
此外,here是一篇关于处理音频焦点的好文章(只是将其保留在此处以备将来参考)
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private AudioManager mAudioManager;
private AudioFocusChangeListenerImpl mAudioFocusChangeListener;
private boolean mFocusGranted, mFocusChanged;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioFocusChangeListener = new AudioFocusChangeListenerImpl();
int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
switch (result) {
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
mFocusGranted = true;
break;
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
mFocusGranted = false;
break;
}
String message = "Focus request " + (mFocusGranted ? "granted" : "failed");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
Log.i(TAG, message);
}
@Override
protected void onDestroy() {
super.onDestroy();
mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class AudioFocusChangeListenerImpl implements AudioManager.OnAudioFocusChangeListener {
@Override
public void onAudioFocusChange(int focusChange) {
mFocusChanged = true;
Log.i(TAG, "Focus changed");
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
Log.i(TAG, "AUDIOFOCUS_GAIN");
Toast.makeText(MainActivity.this, "Focus GAINED", Toast.LENGTH_LONG).show();
break;
case AudioManager.AUDIOFOCUS_LOSS:
Log.i(TAG, "AUDIOFOCUS_LOSS");
Toast.makeText(MainActivity.this, "Focus LOST", Toast.LENGTH_LONG).show();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
Toast.makeText(MainActivity.this, "Focus LOST TRANSIENT", Toast.LENGTH_LONG).show();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.i(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
Toast.makeText(MainActivity.this, "Focus LOST TRANSIENT CAN DUCK", Toast.LENGTH_LONG).show();
break;
}
}
}
}
答案 2 :(得分:1)
我发现某些设备上没有实现此行为。我的第一个HTC手机尊重监听器API,但我的华硕不支持。我不知道谷歌为什么会让这种碎片存在。
您可以在[{3}}
上投诉某人