android MediaController audio example 1

android MediaController audio example

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;

import java.io.IOException;

public class AudioPlayer extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{
  private static final String TAG = "AudioPlayer";

  public static final String AUDIO_FILE_NAME = "audioFileName";

  private MediaPlayer mediaPlayer;
  private MediaController mediaController;
  private String audioFile;

  private Handler handler = new Handler();

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audio_player);

    audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
    ((TextView)findViewById(R.id.now_playing_text)).setText(audioFile);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);

    mediaController = new MediaController(this);

    try {
      mediaPlayer.setDataSource(audioFile);
      mediaPlayer.prepare();
      mediaPlayer.start();
    } catch (IOException e) {
      Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
    }

  }

  @Override
  protected void onStop() {
    super.onStop();
    mediaController.hide();
    mediaPlayer.stop();
    mediaPlayer.release();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    mediaController.show();
    return false;
  }

  //--MediaPlayerControl methods----------------------------------------------------
  public void start() {
    mediaPlayer.start();
  }

  public void pause() {
    mediaPlayer.pause();
  }

  public int getDuration() {
    return mediaPlayer.getDuration();
  }

  public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
  }

  public void seekTo(int i) {
    mediaPlayer.seekTo(i);
  }

  public boolean isPlaying() {
    return mediaPlayer.isPlaying();
  }

  public int getBufferPercentage() {
    return 0;
  }

  public boolean canPause() {
    return true;
  }

  public boolean canSeekBackward() {
    return true;
  }

  public boolean canSeekForward() {
    return true;
  }
  //--------------------------------------------------------------------------------

  public void onPrepared(MediaPlayer mediaPlayer) {
    Log.d(TAG, "onPrepared");
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(findViewById(R.id.main_audio_view));

    handler.post(new Runnable() {
      public void run() {
        mediaController.setEnabled(true);
        mediaController.show();
      }
    });
  }
}

Here is what the above code is Doing:
1. The onCreate() method is called when the activity is created. It sets the content view to the layout file audio_player.xml. It then gets the name of the audio file to play from the intent that started the activity. It creates a MediaPlayer object and sets the OnPreparedListener to this activity. It then creates a MediaController object and sets the media player to this activity. It then sets the data source of the media player to the audio file and calls prepare() and start().
2. The onStop() method is called when the activity is stopped. It hides the media controller, stops the media player, and releases the media player.
3. The onTouchEvent() method is called when the screen is touched. It shows the media controller.
4. The start(), pause(), getDuration(), getCurrentPosition(), seekTo(), isPlaying(), getBufferPercentage(), canPause(), canSeekBackward(), and canSeekForward() methods are all part of the MediaPlayerControl interface. They are called by the media controller to control the media player.
5. The onPrepared() method is called when the media player is prepared. It sets the media player of the media controller to this activity. It then sets the anchor view of the media controller to the main audio view. It then shows the media controller.

Here’s the audio_player.xml layout file:





Here’s the AndroidManifest.xml file:










Here’s the main.xml layout file:


Similar Posts