Android 声音池只播放一次声音
我有一个声音池,一旦视图动画开始,它就会播放一个简短的声音效果。问题是它只播放第一次的声音,而不是连续播放 我已经检查了sound.play函数的返回结果,它总是streamID,从不为零。它在需要时创建流,但声音只是第一次播放。声音只加载一次,因此它不应该是加载问题,因为它是第一次播放的。我将粘贴以下代码: 我正在调用createSoundPool()来构建声音池Android 声音池只播放一次声音,android,soundpool,Android,Soundpool,我有一个声音池,一旦视图动画开始,它就会播放一个简短的声音效果。问题是它只播放第一次的声音,而不是连续播放 我已经检查了sound.play函数的返回结果,它总是streamID,从不为零。它在需要时创建流,但声音只是第一次播放。声音只加载一次,因此它不应该是加载问题,因为它是第一次播放的。我将粘贴以下代码: 我正在调用createSoundPool()来构建声音池 private void createSoundPool() { if (Build.VERSION.SDK_INT &g
private void createSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
createNewSoundPool();
} else {
createOldSoundPool();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void createNewSoundPool(){
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
sounds = new SoundPool.Builder().setMaxStreams(5)
.setAudioAttributes(attributes)
.build();
}
@SuppressWarnings("deprecation")
private void createOldSoundPool(){
sounds = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
}
这就是我使用它的方式
public MyTouchListener(final Context context, final RecyclerView recyclerView,
OnTouchActionListener onTouchActionListener){
mOnTouchActionListener = onTouchActionListener;
initAnim();
createSoundPool();
drawCardSound = sounds.load(context,R.raw.drawcard,1); //this is where the soundID is created. Only once the class is instantiated
mGestureDetector = new GestureDetectorCompat(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// Find the item view that was swiped based on the coordinates
final View child = recyclerView.findChildViewUnder(e1.getX(), e1.getY());
final int childPosition = recyclerView.getChildAdapterPosition(child);
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mOnTouchActionListener != null && child != null) {
if(!animation.hasStarted() || animation.hasEnded()) {
child.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
int res=sounds.play(drawCardSound,3,3,1,0,1f);// sound is being played here
Log.e("test", "onAnimationStart: "+res );
}
LogCat显示一个递增的streamID,并且从不为零,因此我认为它应该正确触发
更新:
这个问题只发生在棒棒糖上面,我已经在下面棒棒糖的模拟器上测试过了,声音很好。棒棒糖及以上食品的包装上有虫子吗?我已经在三星S7上进行了测试,发现了问题!显然,SoundPool.Play()不能为左右音量取大于1.0的值。这对于低于棒棒糖的API来说效果很好,但对于高于棒棒糖的API来说效果不好。我已将该值降低到1.0,现在工作正常谢谢!在无数次搜索失败后,这救了我!:)