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

Why is my soundpool behaving differently in these 2 situations? (The value soundpool.play assigned to stream is never used)

提问人:motty_here 提问时间:12/18/2022 最后编辑:motty_here 更新时间:12/19/2022 访问量:35

问:

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

            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;
            }

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

    int A4, A5, aStream; //global variable in MainActivity
            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

在 中,该变量有一个下划线,它表示“从未使用分配给流的值 soundPool.play(sharp, 1, 1, 0, loop_num, 1)。就像是的,它没有被使用,它只是用于播放音频。rotate_methodstream

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

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

java android ontouchlistener 声音池

评论


答:

0赞 Thomas Kläger 12/19/2022 #1

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

在试用代码中,将该值分配给某个字段: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);
}

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

评论

0赞 motty_here 12/19/2022
(对于生产代码)我以为我从一开始就传递了 streamID(),并且方法只能使用该 streamID 来播放/停止声音。我声明为全局 var,所以我认为它可以在任何地方访问。我尝试将流变量转换为本地变量并在其他参数中删除它,但我不确定这如何用于 .它仍然播放整个音频,而不是在我抬起手指时停止。aStreamaStreamrotate_methodACTION_UP
0赞 Thomas Kläger 12/19/2022
@motty_here不能将 streamID 的值传递到 - 创建该值,并且必须将其存储在字段或全局变量中,以便可以将创建的值传递给 。soundPool.play(sharp, 1, 1,0,loop_num,1)soundPool.play(sharp, 1, 1,0,loop_num,1)soundPool.stop()