Introduction

The android media viewer audio focus tutorial series describes how to support & use android’s AudioFocus for media playback

Also discussed will be supporting the becoming noisy event where a BroadcastReceiver will be setup.

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/media-thumbnail-viewer.git Tag

media-viewer-audiofocus

or you can run this command

git clone https://github.com/mobapptuts/media-thumbnail-viewer.git –branch

media-viewer-audiofocus

This video describes how to import the code from github using android studio and also how to use git tags

Steps

Adding a listener for audio focus change

public class VideoPlayActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnCompletionListener,
    AudioManager.OnAudioFocusChangeListener {

And inside the overridden  method implement the calls for when the audio focus changes

@Override
  public void onAudioFocusChange(int audioFocusChanged) {
      switch (audioFocusChanged) {
          case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
              mediaPause();
              break;
          case AudioManager.AUDIOFOCUS_GAIN:
              mediaPlay();
              break;
          case AudioManager.AUDIOFOCUS_LOSS:
              mediaPause();
              break;
      }
  }

 

Creating activity members

These members will support the AudioManager and becoming noisy events

private AudioManager mAudioManager;
 private IntentFilter mNoisyIntentFilter;
 private AudioBecommingNoisy mAudioBecommingNoisy;

Created a BroadcastReceiver for the becoming noisy event

private class AudioBecommingNoisy extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent intent) {
           mediaPause();
       }
   }

 

Setup the AudioManager and becoming noisy members

@Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_video_play);

       mPlayPauseButton = (ImageButton) findViewById(R.id.videoPlayPauseButton);
       mSurfaceView = (SurfaceView) findViewById(R.id.videoSurfaceView);

       Intent callingIntent = this.getIntent();
       if(callingIntent != null) {
           mVideoUri = callingIntent.getData();
       }

       mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
       mAudioBecommingNoisy = new AudioBecommingNoisy();
       mNoisyIntentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
   }

 Playing the media

Before playing the media register the BroadcastReceiver for becoming noisy events.

Then make a request for audio focus. If granted play the media.

private void mediaPlay() {
        registerReceiver(mAudioBecommingNoisy, mNoisyIntentFilter);
        int requestAudioFocusResult = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        if(requestAudioFocusResult == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mMediaPlayer.start();
            mPlayPauseButton.setImageResource(R.mipmap.ic_media_pause);
        }
    }

 Pausing the media

In the paused state we release the audio focus and unregister the BroadcastReceiver

private void mediaPause() {
      mMediaPlayer.pause();
      mPlayPauseButton.setImageResource(R.mipmap.ic_media_play);
      mAudioManager.abandonAudioFocus(this);
      unregisterReceiver(mAudioBecommingNoisy);
  }

 

Android media viewer audio focus summary

In the android media viewer audio focus development tutorial we discussed to correct procedure in using audio in the android environment.

We also introduced the action_audio_becoming_noisy event for handling unforeseen circumstances such as the head jack being accidentally removed.

Android media viewer audio focus

| Media Viewer, tutorials |
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>