我正在尝试制作一个简单的 Android MP3 播放器,它将播放由 mediaplayer=mediaplayer.create 代码行声明的 MP3!
XML 代码用于一个简单的按钮,上面写着播放!
我希望它在按下按钮时播放 MP3...
任何人都可以帮忙吗?
import android.R; //This automatically generates R file (references resources)
import android.app.Activity;
import android.media.MediaPlayer; - //This allows for predefined MP3 start/stop classes to be used
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class Mp3player extends Activity {
private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private final Handler handler = new Handler();
@Override
public void onCreate(Bundle icicle) { //Place holder for ‘OnFreeze’ parameter
super.onCreate(icicle);
setContentView(R.layout.activity_main); //Referencing XML file
initViews();
}
// setOnClickListener method (buttonClick())
private void initViews() {
buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
buttonPlayStop.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {buttonClick();}});
mediaPlayer = MediaPlayer.create(this, R.raw.music); // Sound file location
// event handler for buttonClick event
private void buttonClick(){ //When button is clicked do…
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str)); //
try{
mediaPlayer.start(); //Play MP3
}
}else {
buttonPlayStop.setText(getString(R.string.play_str));
mediaPlayer.pause(); //Pause MP3
}
}
}