提问者:小点点

为什么我的声池在这 2 种情况下表现不同?(从不使用分配给流的 soundpool.play 值)


我想在用户触摸按钮时播放声音,并在抬起手指时停止声音。这是我的试用代码,可以按我想要的方式工作:

            public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            // -1 so it keeps looping
                            fStream = soundPool.play(F4, 1, 1,0,-1,1);
                            return true;
                        case MotionEvent.ACTION_UP:
                            soundPool.stop(fStream);
                    }
                return false;
            }

这是我在应用一些条件后要放入我的应用程序中的代码:

            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(up){
                            horizontal_method(A5, A5_sharp, aStream);
                        } else if(down){
                            horizontal_method(A4, A4_sharp, aStream);
                        }
                        return true;
                    case MotionEvent.ACTION_UP:
                        stop_audio_method(aStream);
                        //soundPool.stop(aStream);
                }
                return false;
            }
    public void horizontal_method(int note, int sharp, int stream){
        if(horizontal){ //if phone is moving
            loop_num = -1;
        } else { //if phone is not moving
            loop_num = 0;
        }
        rotate_method(note, sharp, stream, loop_num);
    }

    public void rotate_method(int note, int sharp, int stream, int loop_num){
        if(rotate){ //if it's sharp
            stream = soundPool.play(sharp, 1, 1,0,loop_num,1);
        } else { // if it's normal note
            stream = soundPool.play(note, 1, 1,0,loop_num,1);
        }
    }

    public void stop_audio_method(int stream){
        soundPool.stop(stream);
    }

问题:

在试用代码中,当我按住按钮时,声音循环播放。当我抬起手指时,它停止了。但是在我的应用程序中,当我抬起手指时,声音不会立即停止。它播放整个音频,不管它是否循环播放。 (现在当它循环时,它永远不会停止)。我尝试将soundpool.stop(aStream)直接放在MotionEvent.ACTION_UP中,但它仍然是一样的。

rotate_method中,变量有一个下划线,它说“分配给流的值 soundPool.play(sharp, 1, 1, 0, loop_num, 1) 永远不会使用。就像是的,它没有被使用,它只是为了播放音频。

试用代码没有此“免责声明”。

我做错了什么?为什么声音池在试用代码和我的应用程序中的行为不同?


共1个答案

匿名用户

SoundPool.play() 返回一个 streamID,如果要停止播放,则需要该 ID。

在试用代码中,将该值分配给某个字段 fStream

fStream = soundPool.play(F4, 1, 1,0,-1,1);

并在您想要停止播放时使用它来停止播放:

soundPool.stop(fStream);

在“生产”代码中,将该值分配给参数:

public void rotate_method(int note, int sharp, int stream, int loop_num){
    stream = soundPool.play(sharp, 1, 1,0,loop_num,1);
    // stream is a method parameter here and changes are lost!
}

您必须将返回的值存储在某个字段中 - 为什么不重用上一个示例中的字段:

public void rotate_method(int note, int sharp, int stream, int loop_num){
    fStream = soundPool.play(sharp, 1, 1,0,loop_num,1);
}

由于您现在不再需要该参数,因此应将其从方法中删除:

public void rotate_method(int note, int sharp, int loop_num){
    fStream = soundPool.play(sharp, 1, 1,0,loop_num,1);
}

此更改将传播回调用方法。