猿问

如何在 OnTouchListner Android 上的两个不同按钮上播放两种不同的声音?

我正在制作一个安卓应用程序。我添加了两个按钮并为它们分配了两种不同的声音。现在 Button1 应该播放 sound.mp3 和 Button2 应该在按下时播放 asound.mp3 并且声音应该在按下时停止。问题是两个按钮都只播放一种声音。


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);

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);

}


@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {


case MotionEvent.ACTION_DOWN: {

mp.setLooping(true);

mp.start();

}


break;

case MotionEvent.ACTION_UP: {

mp.pause();

}

break;

}

return true;

}

}


慕尼黑的夜晚无繁华
浏览 162回答 3
3回答

ITMISS

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);}@Overridepublic 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;}}

动漫人物

您必须创建两个媒体播放器您必须指定正在调整的按钮使用此代码private MediaPlayer mp1;private MediaPlayer mp2;public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; ImageButton btn = (ImageButton) this.findViewById(R.id.button1);&nbsp; &nbsp; btn.setOnTouchListener(this);&nbsp; &nbsp; mp1 = MediaPlayer.create(this, R.raw.sound);&nbsp; &nbsp; ImageButton btn2 = (ImageButton) this.findViewById(R.id.button2);&nbsp; &nbsp; btn2.setOnTouchListener(this);&nbsp; &nbsp; mp2 = MediaPlayer.create(this, R.raw.asound);}@Overridepublic boolean onTouch(View v, MotionEvent event) {&nbsp; &nbsp; switch (event.getAction()) {&nbsp; &nbsp; case MotionEvent.ACTION_DOWN: {&nbsp; &nbsp; &nbsp; &nbsp; if(v.getId() == R.id.button1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp1.setLooping(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp1.start();&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp2.setLooping(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp2.start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; break;&nbsp; &nbsp; case MotionEvent.ACTION_UP: {&nbsp; &nbsp; &nbsp; &nbsp; if(v.getId() == R.id.button1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp1.pause();&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mp2.pause();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; 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) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; //others codes&nbsp; &nbsp; for (int btn : btns) {&nbsp; &nbsp; &nbsp; &nbsp; (ImageButton) this.findViewById(btn).setOnTouchListener(this);&nbsp; &nbsp; }}@Overridepublic boolean onTouch(View v, MotionEvent event) {&nbsp; &nbsp; switch (event.getAction()) {&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0 ; i<btns.length ; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (v.getId() == btns[i]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mp = MediaPlayer.create(this, sounds[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mp.start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int btn : btns) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (v.getId() == btn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mp.pause();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; return true;}

芜湖不芜

创建两个 MediaPlayer 实例对于第一个声音MediaPlayer&nbsp;mpFirst&nbsp;=&nbsp;MediaPlayer.create(this,&nbsp;R.raw.sound);对于第二声MediaPlayer&nbsp;mpSecond&nbsp;=&nbsp;MediaPlayer.create(this,&nbsp;R.raw.asound);
随时随地看视频慕课网APP

相关分类

Java
我要回答