我已经尝试了我在本网站上可以找到的所有示例,但我似乎无法让它发挥作用。 我需要它,以便如果通过按下按钮启动mp3文件,如果按下另一个按钮,则前一个mp3停止并且新的mp3开始。
我还需要它在离开页面时停止。
任何人都可以看到我可能做错了吗?
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class buttontofirstaidaed extends Activity implements OnClickListener{
private MediaPlayer mp1;
private MediaPlayer mp2;
private MediaPlayer mp3;
private MediaPlayer mp4;
private MediaPlayer mp5;
private MediaPlayer mp6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstaidaed);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button buttonaed1=(Button)findViewById(R.id.button_aed1);
Button buttonaed2=(Button)findViewById(R.id.button_aed2);
Button buttonaed3=(Button)findViewById(R.id.button_aed3);
Button buttonaed4=(Button)findViewById(R.id.button_aed4);
Button buttonaed5=(Button)findViewById(R.id.button_aed5);
Button buttonaed6=(Button)findViewById(R.id.button_aed6);
buttonaed1.setOnClickListener(this);
buttonaed2.setOnClickListener(this);
buttonaed3.setOnClickListener(this);
buttonaed4.setOnClickListener(this);
buttonaed5.setOnClickListener(this);
buttonaed6.setOnClickListener(this);
}
public void onClick(View v) {
int resId1;
int resId2;
int resId3;
int resId4;
int resId5;
int resId6;
switch (v.getId()) {
case R.id.button_aed1:
resId1 = R.raw.aed1;
if(mp1!=null) { mp1.release(); } mp1 = MediaPlayer.create(this, resId1); mp1.start();
break;
case R.id.button_aed2:
resId2 = R.raw.aed2;
if(mp2!=null) { mp2.release(); } mp2 = MediaPlayer.create(this, resId2); mp2.start();
break;
case R.id.button_aed3:
resId3 = R.raw.aed3;
if(mp3!=null) { mp3.release(); } mp3 = MediaPlayer.create(this, resId3); mp3.start();
break;
case R.id.button_aed4:
resId4 = R.raw.aed4;
if(mp4!=null) { mp4.release(); } mp4 = MediaPlayer.create(this, resId4); mp4.start();
break;
case R.id.button_aed5:
resId5 = R.raw.aed5;
if(mp5!=null) { mp5.release(); } mp5 = MediaPlayer.create(this, resId5); mp5.start();
break;
case R.id.button_aed6:
resId6 = R.raw.aed6;
if(mp6!=null) { mp6.release(); } mp6 = MediaPlayer.create(this, resId6); mp6.start();
break;
}
}
}
答案 0 :(得分:0)
为什么不使用相同的MediaPlayer播放所有声音。
然后在每个按钮中单击停止它并使用相应的媒体再次初始化。
了解MediaPlayer的直播周期。第一次看起来很疯狂xD
http://developer.android.com/reference/android/media/MediaPlayer.html
尝试使用类似的东西
private MediaPlayer genericMediaPlayer = null;
public void onClick(View v) {
if(genericMediaPlayer == null) {
genericMediaPlayer = MediaPlayer.create(this);
} else {
genericMediaPlayer.stop();
}
FileDescriptor audio = null;
switch (v.getId()) {
case R.id.button_aed1:
audio = getResources().openRawResourceFd(R.raw.audio1).getFileDescriptor();
break;
case R.id.button_aed2:
audio =getResources().openRawResourceFd(R.raw.audio3).getFileDescriptor();
break;
case R.id.button_aed3:
audio = getResources().openRawResourceFd(R.raw.audio3).getFileDescriptor();
break;
case R.id.button_aed4:
audio = getResources().openRawResourceFd(R.raw.audio4).getFileDescriptor();
break;
case R.id.button_aed5:
audio = getResources().openRawResourceFd(R.raw.audio5).getFileDescriptor();
break;
case R.id.button_aed6:
audio = getResources().openRawResourceFd(R.raw.audio6).getFileDescriptor();
break;
}
if(audio != null){
genericMediaPlayer.setDataSource(audio);
genericMediaPlayer.prepare();
genericMediaPlayer.start();
}
}
答案 1 :(得分:0)
现在全部修复,其他问题的链接解决了这个问题。谢谢你的帮助。
答案 2 :(得分:0)
使用相同的媒体播放器,并在每次点击开始时使用:
if (player!=null) {
player.stop();
}
player = MediaPlayer.create(this, R.raw.recording);
start();