的Soundpool声停止只有一次? [英] SoundPool sound stops only once?

查看:1931
本文介绍了的Soundpool声停止只有一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Sound类:

I have a Sound class:

  package dubpad.brendan;

  import android.media.SoundPool;

    public class Sound {
   SoundPool soundPool;
    int soundID;

    public Sound(SoundPool soundPool, int soundID) {
        this.soundPool = soundPool;
        this.soundID = soundID;
    }

    public void play(float volume) {
        soundPool.play(soundID, volume, volume, 0, -1, 1);
    }

    public void stop(int soundID){
        soundPool.stop(soundID);
    }


    public void dispose() {
        soundPool.unload(soundID);
    }


    }

和我有一个扩展的按钮活动:

and I have an activity that extends a button :

package dubpad.brendan;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.AttributeSet;

import android.view.MotionEvent;
import android.widget.Button;

public class TestButton extends Button {
SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
int soundID = soundPool.load(getContext(), R.raw.dub1, 0);
Sound sound = new Sound(soundPool, soundID);


public TestButton(final Context context, final AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}



@Override
public boolean onTouchEvent(final MotionEvent event) {
  if(event.getAction()==MotionEvent.ACTION_DOWN){
sound.play(1);


 } 




if(event.getAction()==MotionEvent.ACTION_UP){
sound.stop(soundID);
}


    return super.onTouchEvent(event);
}

 }

在第一个动作下来的声音播放,并在第一个行动起来的声音停顿。但是第一个接的每一个行动起来不会暂停的声音。在简单的话,停顿只能使用一次。

On the first action down the sound plays, and on first action up the sound pauses. But every single action up after the 1st one doesn't pause the sound. In simple words, the pause only works once.

推荐答案

===编辑===(我觉得我原来的答复是错,更改它)

===EDIT=== (I think my original answer was wrong, changing it)

您需要更改Sound类从发挥返回流ID:

You need to change Sound class to return the streamId from play:

public int play(float volume) {
  return soundPool.play(soundID, volume, volume, 0, -1, 1);
}

然后你就可以存储这个值内TestButton的:

And then you can store this value inside of TestButton:

if(event.getAction()==MotionEvent.ACTION_DOWN){
  mStreamId = sound.play(1);
} 

然后使用mStreamId停止声音:

Then use mStreamId to stop the sound:

if(event.getAction()==MotionEvent.ACTION_UP){
   sound.stop(mStreamId);
}

这里的关键是,你要调用停止流ID ,而不是 soundId 。一个soundId指特定声音的资源,但在流ID是指声音播放的单个实例。所以,如果你玩同样的声音3次,你有一个soundId和三个streamIds。

The key here is that you want to call stop on the streamId, not the soundId. A soundId refers to the particular sound resource, but the streamId refers to a single instance of the sound playing. So if you play the same sound 3 times you have one soundId and three streamIds.

这篇关于的Soundpool声停止只有一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆