Reputation: 21
I'm new to android app development and have what I think is probably a pretty simple question, but I'm having trouble sussing out how to do what I want...
I have a simple application that plays a sound when a button is pressed. After the sound is played I'd like for the application to return the audio to whatever was playing before the application started (for example, if the music app is playing, the sound should interrupt music while it plays, and then return control and resume music playing. I tried using the setOnCompletionListener method, but with no joy.
Here's the code:
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;
}
}
}
Upvotes: 2
Views: 4176
Reputation: 1717
I know Im little late, but here is implementation on how to get audio focus when needed and release it when you are done with playing your audio.
The background apps, that are playing audio, will stop when you gain audio focus requestAudioFocus()
for your application, and will continue where they have stopped, after you release the audio focus releaseAudioFocus()
.
Also, have implementation for API 26 and above.
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));
}
}
Upvotes: 4