Android Soundpool - 循环播放异常

发布于 2025-01-02 03:05:25 字数 839 浏览 1 评论 0原文

我在使用 SoundPool 和 .OGG 文件播放循环声音时遇到问题。我设置了这个 HashMap,用于查找与名称关联的声音并播放/停止它。

    public void playLoopSound(String soundName){

        currentSound = (Integer) soundMap.get(soundName);

        if(currentSound != -1){
            try{
                Logger.log("Playing Loop Sound: " + currentSound);
                loopingSound = soundPool.play(currentSound, 1, 1, 0, -1, 1);
            } catch (Exception e) {
                Logger.log("Sound Playing Error: " + e.getMessage());
            }
        } else {
            Logger.log("Sound Not Found");
        }
    }

    public void stopLoopSound(){
        soundPool.stop(loopingSound);
        loopingSound = 0;
    }

这个设置工作正常,我在角色开始行走时启动循环,并在角色停止行走时停止循环。

然而,声音会随机停止播放,通常是在使用(打开和关闭)后一分钟左右......

还有其他人遇到过 SoundPool 和循环声音的类似问题吗?

I am encountering problems with playing looping sounds using SoundPool and .OGG files. I have this HashMap set up for finding a sound associated to a name and playing it/stopping it

    public void playLoopSound(String soundName){

        currentSound = (Integer) soundMap.get(soundName);

        if(currentSound != -1){
            try{
                Logger.log("Playing Loop Sound: " + currentSound);
                loopingSound = soundPool.play(currentSound, 1, 1, 0, -1, 1);
            } catch (Exception e) {
                Logger.log("Sound Playing Error: " + e.getMessage());
            }
        } else {
            Logger.log("Sound Not Found");
        }
    }

    public void stopLoopSound(){
        soundPool.stop(loopingSound);
        loopingSound = 0;
    }

This set up works fine, i start the loop when the character starts walking and stop it when it stops walking.

The sound would however stop playing randomly, usually a minute or so after having been used (being turned on and off)...

Has anyone else encountered similar problems with SoundPool and looped sounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2025-01-09 03:05:25

阅读 Soundpool 上的文档可以清楚很多:

- 播放声音时,您传递一个引用加载声音的 int 。此方法(以及其他开始播放声音的方法) soundpool.play(int) 返回一个 int 值,引用当前播放声音的 threadID。

-当你想停止声音(或循环声音)时,你必须使用你开始播放声音时刚刚返回的threadID的int,而不是声音本身的int!

然后,在 soundpool 类中,设置一个 private int threadIDInt = mSoundPool.play/setloop/whatever(int soundtobeplayed, int/floatvolumeleft, int/floatvolumeright, float speed, int Loopornot); [注:论点有点编造;查找它们]

然后要停止声音(或暂停,或将其设置为循环或不循环),您可以传递 mSoundPool.stop(threadIDINT);

TLDR:表示您的声音的 int 和用于表示当前播放声音的流的内部 int soundpool 之间存在差异。

Reading the documentation on Soundpool clears up a lot:

-playing a sound you pass an int referring to the loaded sound. This method (and others starting a sound playing), soundpool.play(int) returns an int referring to the threadID in which the sound now plays.

-when you want to stop the sound (or the looping sound) you have to use the int of the threadID you just got back when you started playing the sound, NOT the int of the sound itself!

Within the soundpool class then, you set a private int threadIDInt = mSoundPool.play/setloop/whatever(int soundtobeplayed, int/float volumeleft, int/float volumeright, float speed, int loopornot); [note: the arguments are a bit made up; look 'em up]

To then stop the sound (or pause, or set it to loop or not) you pass mSoundPool.stop(threadIDINT);

TLDR: there's a differnce between the int which denotes your sound and the internal int soundpool uses to denote the stream in which your current sound is playing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文