3 回答
TA贡献1871条经验 获得超8个赞
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = (ImageButton) this.findViewById(R.id.button1);
btn.setOnTouchListener(this);
mp = MediaPlayer.create(this, R.raw.sound);
ImageButton btn2 = (ImageButton) this.findViewById(R.id.button2);
btn2.setOnTouchListener(this);
mp = MediaPlayer.create(this, R.raw.asound); // this is your problem. you set the MediaPlayer fixed to this sound
}
您应该根据单击的 Button 设置声音。我以前没有使用过 android,所以我不能 100% 确定代码,但你需要这样的东西:
package com.example.sound1;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnTouchListener {
private MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = (ImageButton) this.findViewById(R.id.button1);
btn.setOnTouchListener(this);
// remove this -> mp = MediaPlayer.create(this, R.raw.sound);
ImageButton btn2 = (ImageButton) this.findViewById(R.id.button2);
btn2.setOnTouchListener(this);
// remove this -> mp = MediaPlayer.create(this, R.raw.asound);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if ( event.getActionSource() == R.id.button1) {
mp = MediaPlayer.create(this, R.raw.sound);
} else if ( event.getActionSource() == R.id.button2 ) {
mp = MediaPlayer.create(this, R.raw.asound);
} else { mp = null; }
if ( mp != null ) {
mp.setLooping(true);
mp.start();
}
}
break;
case MotionEvent.ACTION_UP: {
if (mp != null ) mp.pause();
}
break;
}
return true;
}
}
TA贡献1815条经验 获得超10个赞
您必须创建两个媒体播放器
您必须指定正在调整的按钮
使用此代码
private MediaPlayer mp1;
private MediaPlayer mp2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn = (ImageButton) this.findViewById(R.id.button1);
btn.setOnTouchListener(this);
mp1 = MediaPlayer.create(this, R.raw.sound);
ImageButton btn2 = (ImageButton) this.findViewById(R.id.button2);
btn2.setOnTouchListener(this);
mp2 = MediaPlayer.create(this, R.raw.asound);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if(v.getId() == R.id.button1){
mp1.setLooping(true);
mp1.start();
}else{
mp2.setLooping(true);
mp2.start();
}
}
break;
case MotionEvent.ACTION_UP: {
if(v.getId() == R.id.button1){
mp1.pause();
}else{
mp2.pause();
}
}
break;
}
return true;
}
对于 3 个或更多按钮,请使用此
private MediaPlayer mp;
private int[] sounds = {R.raw.sound1,R.raw.sound2,R.raw.sound3};
private int[] btns = {R.id.button1,R.id.button2,R.id.button3};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//others codes
for (int btn : btns) {
(ImageButton) this.findViewById(btn).setOnTouchListener(this);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
for (int i = 0 ; i<btns.length ; i++){
if (v.getId() == btns[i]){
mp = MediaPlayer.create(this, sounds[i]);
break;
}
}
mp.start();
}
break;
case MotionEvent.ACTION_UP: {
for (int btn : btns) {
if (v.getId() == btn) {
mp.pause();
break;
}
}
}
break;
}
return true;
}
TA贡献1796条经验 获得超7个赞
创建两个 MediaPlayer 实例
对于第一个声音
MediaPlayer mpFirst = MediaPlayer.create(this, R.raw.sound);
对于第二声
MediaPlayer mpSecond = MediaPlayer.create(this, R.raw.asound);
添加回答
举报