声音池仅播放一次声音

时间:2018-08-05 09:31:06

标签: android soundpool

我有一个Soundpool,一旦查看动画开始,它就会播放短声音效果。问题在于,它只会第一次播放声音,而不会连续播放。

我已经检查了sound.play函数的返回结果,它始终是streamID,并且永远不会为零。它在需要时创建流,但是声音只是第一次播放。声音仅加载一次,因此自首次播放以来就不会成为加载问题。我将粘贴以下代码:

我正在调用createSoundPool()来构建声音池

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,并且永远不会为零,因此我认为它应该正确触发。

更新: 这个问题仅发生在Lollipop上,而我已经在下面的Lollipop模拟器上对其进行了测试,并且声音工作正常。棒棒糖及更高版本上是否存在一些错误?我已经在三星S7上对其进行了测试

1 个答案:

答案 0 :(得分:4)

发现了问题!因此,显然SoundPool.Play()的左右音量不能取大于1.0的值。对于低于Lollipop的API来说这很好用,但是对于大于Lollipop的API来说效果不佳。我将值降低到1.0,现在可以正常使用了