2

我是 android 应用程序开发的新手,我认为这可能是一个非常简单的问题,但我无法弄清楚如何做我想做的事......

我有一个简单的应用程序,可以在按下按钮时播放声音。播放声音后,我希望应用程序将音频返回到应用程序启动之前正在播放的任何内容(例如,如果音乐应用程序正在播放,声音应该在播放时中断音乐,然后返回控制并恢复音乐播放。我尝试使用 setOnCompletionListener 方法,但没有任何乐趣。

这是代码:

public class MainActivity extends Activity implements OnClickListener{

public MediaPlayer mp = null;
public ImageButton playm = null;
public AudioManager audioManager = null;

/** called when activity first created */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    //Sigh Button
    playm = (ImageButton) findViewById(R.id.sighButton);
    playm.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v==playm){
        OnAudioFocus onAudioFocus=new OnAudioFocus(this);
        audioManager.requestAudioFocus(onAudioFocus, AudioManager.STREAM_MUSIC, 
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
        playSighSound();
    }
}

public void playSighSound(){
        mp = MediaPlayer.create(this, R.raw.ohboy); 
        try {
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener(){

            @Override
            public void onCompletion(MediaPlayer mp){
            mp.release();
            }
        });
        } catch (IllegalArgumentException e){
            e.printStackTrace();
        } catch (IllegalStateException e){
            e.printStackTrace();
        }
    }
}

class OnAudioFocus implements AudioManager.OnAudioFocusChangeListener{
    MainActivity onAudioFocus;

    public OnAudioFocus(MainActivity onAudioFocus){
        this.onAudioFocus = onAudioFocus;
    }


public void onAudioFocusChange(int focusChange) {
    switch(focusChange){
    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
        onAudioFocus.playSighSound();
        break;

    }
}
}
4

1 回答 1

1

我知道我迟到了,但这里是关于如何在需要时获得音频焦点并在完成播放音频时释放它的实现。

正在播放音频的后台应用程序将在您获得 requestAudioFocus()应用程序的音频焦点时停止,并在您释放音频焦点后继续它们停止的位置releaseAudioFocus()

此外,实现API 26及更高版本。

public class AudioFocusManager {

    Context context;
    private AudioFocusRequest audioFocusRequest;

    public AudioFocusManager(Context context) {
        this.context = context;
    }

    public void requestAudioFocus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            audioFocusRequest =
                    new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
                            .build();
            getAudioManager().requestAudioFocus(audioFocusRequest);
        } else {
            getAudioManager().requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        }
    }

    public void releaseAudioFocus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (audioFocusRequest != null) {
                getAudioManager().abandonAudioFocusRequest(audioFocusRequest);
                audioFocusRequest = null;
            }
        } else {
            getAudioManager().abandonAudioFocus(null);
        }
    }

    private AudioManager getAudioManager() {
        return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
    }
}
于 2020-10-19T12:55:12.630 回答