1
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、摩托罗拉 Xoom。

4

3 回答 3

6

case AudioManager.AUDIOFOCUS_GAINinonAudioFocusChange()更多的是 AudioFocus 的重新增益。它仅在丢失事件后触发。

case AudioManager.AUDIOFOCUS_REQUEST_GRANTED是您应该添加另一个GlobalMusicPlayer.onFocusGained().

于 2013-09-17T07:23:39.887 回答
1

实际上,我看不出您的代码有什么问题,因此我创建了小型测试活动。

要测试音频焦点侦听器,您必须启动此活动,使用“主页”按钮将其最小化,然后启动一些音乐播放器(在模拟器上使用股票进行测试)——一旦音乐开始,您应该能够看到“焦点丢失”吐司玩。当您停止音乐时,您应该会看到“Focus GAINED”消息。

另外,是一篇关于处理音频焦点的好文章(只是留在这里以备将来参考)

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;
        }
    }
}
}
于 2013-09-16T20:11:13.543 回答
1

我发现这种行为在某些设备上没有实现。我的第一部 HTC 手机尊重侦听器 API,但我的华硕手机却没有。我不知道为什么谷歌会让这种碎片化存在。

您可以在https://groups.google.com/forum/#!topic/android-developers/qWFHOOkgWT8上看到有人抱怨

于 2016-02-03T17:52:39.617 回答