Load mp3 file with MediaPlayer and play : MediaPlayer « Media « Android






Load mp3 file with MediaPlayer and play

   
package app.test;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;

public class Test extends Activity implements OnCompletionListener {

  MediaPlayer mediaPlayer;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  public void onCompletion(MediaPlayer mp) {
    mediaPlayer.start();
  }

  public void onStart() {
    super.onStart();
    mediaPlayer = MediaPlayer.create(this, R.raw.s);//raw/s.mp3
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.start();
  }

  public void onStop() {
    super.onStop();
    // Log.v("CustomAudioPlayer","onStop Called");
    mediaPlayer.stop();
    mediaPlayer.release();
  }

}

   
    
    
  








Related examples in the same category

1.Using MediaPlayer to play MP3 file
2.MediaPlayer and Text to Speech
3.Call prepare for MediaPlayer before starting
4.Set URL data source for MediaPlayer
5.Audio MediaPlayer Demo
6.Using MediaPlayer to play Video and Audio
7.extends MediaPlayer
8.MediaPlayer create and start
9.Multi PlayMixer
10.Play movie.m4v file
11.Using Service to play media file