我在eclipse中创建了一个应用来播放和停止mp3文件。一切都很好,除了当我播放音频文件并停止播放并且我想重播它时,播放btn并没有麦汁!我想知道是否有人可以帮助我?提前致谢。 代码如下:
package ir.polyglotcenter.childrenmostfrequentwords;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Related to the media player
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.stop();
}
});
}
}
答案 0 :(得分:1)
如果要在按钮上启动/停止声音,请使用MediaPlayer.start()
和MediaPlayer.pause()
暂停当前正在播放的声音,该声音会在开始按钮单击时再次开始。并覆盖活动onPause
以最终停止并释放MediaPlayer。在代码中进行更改:
onStop按钮点击:
myButtonTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.pause(); //>>pause current sound
myMediaPlayer.seekTo(0); //>> seek to start it again
}
});
并覆盖Activity的onPause方法:
@Override
protected void onPause{
super.onPause();
myMediaPlayer.stop(); //>>> stop myMediaPlayer
myMediaPlayer.release(); //>>> free myMediaPlayer
}
答案 1 :(得分:0)
试试这个(只需在公共区域定义Media Player)
public class Main extends Activity {
MediaPlayer myMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Related to the media player
myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
}
}
答案 2 :(得分:0)
Point在if或任何循环的循环中实现MediaPlayer
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.buttonPlay:
//mp.reset();
mp=MediaPlayer.create(Ur Context.this, R.raw."ursong.mp3");
mp.start();
break;
case R.id.buttonStop:
mp.stop();
break;
}
}
答案 3 :(得分:0)
您必须移动此代码:
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
in:
myButtonOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this,R.raw.audio);
myMediaPlayer.start();
}
});