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

时间:2019-01-23 07:42:54

标签: java android android-mediaplayer touchscreen

我正在制作一个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;
}
}

3 个答案:

答案 0 :(得分:0)

  1. 您必须创建两个媒体播放器
  2. 您必须指定正在按键的按钮

使用此代码

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

答案 1 :(得分:0)

创建两个MediaPlayer实例

第一个声音

MediaPlayer mpFirst = MediaPlayer.create(this, R.raw.sound); 

第二声

MediaPlayer mpSecond = MediaPlayer.create(this, R.raw.asound); 

适当使用这两个实例

答案 2 :(得分:0)

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
}

您应该根据单击的按钮来设置声音。 我以前没有使用过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;
}
}