Using MediaPlayer to play MP3 file : MediaPlayer « Media « Android






Using MediaPlayer to play MP3 file

   
package app.Test;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;

public class appTest extends Activity {
     private MediaPlayer up;

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

        up = MediaPlayer.create(this, R.raw.up);
     }
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
        MediaPlayer mp;
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_UP:
           mp = up;
           break;
        default:
           return super.onKeyDown(keyCode, event);
        }
        mp.seekTo(0); 
        mp.start();
        return true;
     }
}

   
    
    
  








Related examples in the same category

1.MediaPlayer and Text to Speech
2.Load mp3 file with MediaPlayer and play
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