public class SoundtestActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.gun_shot);
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp.isPlaying())
mp.reset();
mp.start();
}
}));
}
}
答案 0 :(得分:1)
试试此代码
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp.start();
}
});
或者替换mp.start();通过mp.reset();在此代码中
答案 1 :(得分:1)
最好的方式和最简单的方法是= mp.setLooping(真); 是的,它确实有效,只需在mp.start()之后添加此行,并且需要nothinfg
答案 2 :(得分:0)
你需要在按钮的监听器方法中comment
以下行和shift your Media player instance
,与多个媒体播放器并行播放完成你的操作
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
mp = MediaPlayer.create(this, R.raw.gun_shot); // ADD THIS LINE HERE
if(mp.isPlaying()){
mp.stop(); //ADDED TO STOP FIRST
mp.release(); //ADDED TO RELEASE RESOURCES
}
mp.start();
}
}));