paul_schaefer
paul_schaefer

Reputation: 431

Android stop sounds

I am writing an alert app. When an alert is received, I want to stop all current sounds (like music) and play my alert sound. After the user has done something and the activity will be closed, the previous sounds should continue.

Thats the same like someone calls you. How can I achieve this behaviour?

Upvotes: 0

Views: 521

Answers (2)

paul_schaefer
paul_schaefer

Reputation: 431

With the help of Julius Skripkauskas' answer I use the following code:

to start the alert sound:

private void playSound(){
try{
            Log.d(TAG, "request audio focus");
            this.am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
            int result=this.am.requestAudioFocus(afListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE);
            if(result==AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
                Log.d(TAG, "audio focus granted");
                this.player=new MediaPlayer();
                this.player.setOnPreparedListener(new OnPreparedListener(){

                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();                 
                    }});
                this.player.setOnCompletionListener(new OnCompletionListener(){

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        am.abandonAudioFocus(afListener);
                    }});
                this.player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                this.player.setDataSource(getApplicationContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound ));
                this.player.prepareAsync();
            }
        }catch(Exception e){
            Log.e(TAG, "error playing music." + e.getMessage(), e);
        }
}

The Listener: I simply stop the sound because I will only play the sound once focus was granted. If the sound will get interrupted by an other app, it doesn't matter...

private AudioManager.OnAudioFocusChangeListener afListener = new AudioManager.OnAudioFocusChangeListener(){

@Override
public void onAudioFocusChange(int focusChange) {
    Log.d(TAG, "audio focus changed" + Integer.toString(focusChange));
    if(player==null){
        // we don't play audio
        return;
    }           
    player.stop();
}};

It is important to consider to add the OnCompletionListener to the player where to call am.abandonAudioFocus(afListener) to release the audio focus immediately after the sound was played.

Upvotes: 2

JuliusScript
JuliusScript

Reputation: 3850

You can request audio focus. Once you are done with your own audio, abandon audio focus so other apps can start playing sound again. You will be notified of other apps requesting audio focus too, thus being able to pause and resume your own sounds.

Requesting audio focus:

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) {
    am.registerMediaButtonEventReceiver(RemoteControlReceiver);
    // Start playback.
}

Upvotes: 2

Related Questions