android MediaController audio example 1

android MediaController audio example

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;


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) {

    audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);

    mediaPlayer = new MediaPlayer();

    mediaController = new MediaController(this);

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


  protected void onStop() {

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

  //--MediaPlayerControl methods----------------------------------------------------
  public void start() {

  public void pause() {

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

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

  public void seekTo(int 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.setAnchorView(findViewById(; Runnable() {
      public void run() {

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