作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
int SC5;
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
SC5 = sp.load(this, R.raw.c5, 1);
C5.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
sp.play(SC5, 1, 1, 0, 0, 1);
return true;
case MotionEvent.ACTION_UP:
// RELEASED
sp.pause(SC5);
return true;
}
return false;
}
});
我希望当我释放按钮时我的声音池停止播放,这段代码可以工作,但只能工作一次。当我第二次按下按钮时,我无法停止声音我无法使用 autoPause();
因为我在这个声音池中有其他文件
请帮忙
最佳答案
解决方案:
int streamId = -1;
C5.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
streamId = sp.play(SC5, 1, 1, 0, 0, 1);
return true;
case MotionEvent.ACTION_UP:
// RELEASED
sp.pause(streamId);
return true;
}
return false;
}
});
说明
您无法使用 SC5 来暂停声音,因为 SC5 原来是一个 soundId。
更多信息,您应该查看SoundPool的文档,并注意您用于播放(soundId)或暂停(streamId)声音的ID。
/**
* Play a sound from a sound ID.
*
* Play the sound specified by the soundID. This is the value
* returned by the load() function. Returns a non-zero streamID
* if successful, zero if it fails. The streamID can be used to
* further control playback. Note that calling play() may cause
* another sound to stop playing if the maximum number of active
* streams is exceeded. A loop value of -1 means loop forever,
* a value of 0 means don't loop, other values indicate the
* number of repeats, e.g. a value of 1 plays the audio twice.
* The playback rate allows the application to vary the playback
* rate (pitch) of the sound. A value of 1.0 means play back at
* the original frequency. A value of 2.0 means play back twice
* as fast, and a value of 0.5 means playback at half speed.
*
* @param soundID a soundID returned by the load() function
* @param leftVolume left volume value (range = 0.0 to 1.0)
* @param rightVolume right volume value (range = 0.0 to 1.0)
* @param priority stream priority (0 = lowest priority)
* @param loop loop mode (0 = no loop, -1 = loop forever)
* @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
* @return non-zero streamID if successful, zero if failed
*/
public final int play(int soundID, float leftVolume, float rightVolume,
int priority, int loop, float rate);
/**
* Pause a playback stream.
*
* Pause the stream specified by the streamID. This is the
* value returned by the play() function. If the stream is
* playing, it will be paused. If the stream is not playing
* (e.g. is stopped or was previously paused), calling this
* function will have no effect.
*
* @param streamID a streamID returned by the play() function
*/
public final void pause(int streamID);
关于java - SoundPool puse 只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39747308/
有一条(相对)众所周知的 Perl 公理:“只有 Perl 可以解析 Perl”。我想知道 Perl 6 是否仍然如此? 扩大讨论...考虑到 PyPy 最近的更新,我想到了这个问题。 Perl 独特
这是设置。在上一个问题中,我发现我可以通过子组件中的状态传递对象属性,然后使用 componentDidUpdate 获取该对象属性。在这种情况下,状态和属性都称为到达。 这是基本代码... expo
我运行的是 10.5.2 社区版。我已经标记了 源/主要/资源 作为源目录。我可以右键单击并“编译”某些文件,据我所知,这意味着 IDE 将文件复制到与发送类文件的“com.mydomain.pack
我是一名优秀的程序员,十分优秀!