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