我如何知道 Soundpool 已准备好使用低于 2.2 的 SDK 目标?

发布于 2024-09-10 03:41:10 字数 6434 浏览 10 评论 0原文

此问题与此问题。

我设法修改代码以使用 SoundPool 而不是 AudioManager。知道它或多或少有效。

public class Sound {

    private static boolean sound = true;

    private static final int EAT_SOUND = 1;
    private static final int SHORT_EAT_SOUND = 2;
    private static final int EAT_CHERRY_SOUND = 3;
    private static final int EAT_GHOST_SOUND = 4;
    private static final int EXTRA_LIVE_SOUND = 5;
    private static final int INTERMISSION_SOUND = 6;
    private static final int OPENING_SOUND = 7;
    private static final int PACMAN_DIES_SOUND = 8;
    private static final int SIREN_SOUND = 9;

    private static Context context;
    private static SoundPool soundPool;
    private static HashMap<Integer, Integer> soundPoolMap;

    public static void initializeOpenSound(Context con) {
        context = con;
        soundPool = new SoundPool(2, AudioManager.STREAM_RING, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(OPENING_SOUND, soundPool.load(context, R.raw.opening_song, 1));
    }

    public static void initializeSounds() {
        soundPoolMap.put(EAT_SOUND, soundPool.load(context, R.raw.eating, 1));
        soundPoolMap.put(SHORT_EAT_SOUND, soundPool.load(context, R.raw.eating_short, 1));
        soundPoolMap.put(EAT_CHERRY_SOUND, soundPool.load(context, R.raw.eating_cherry, 1));
        soundPoolMap.put(EAT_GHOST_SOUND, soundPool.load(context, R.raw.eating_ghoasts, 1));
        soundPoolMap.put(EXTRA_LIVE_SOUND, soundPool.load(context, R.raw.extra_lives, 1));
        soundPoolMap.put(INTERMISSION_SOUND, soundPool.load(context, R.raw.intermission, 1));
        soundPoolMap.put(PACMAN_DIES_SOUND, soundPool.load(context, R.raw.pac_man_dies, 1));
        soundPoolMap.put(SIREN_SOUND, soundPool.load(context, R.raw.siren, 1));
    }

    private static void playSound(int sound) {
        int streamID = soundPool.play(soundPoolMap.get(sound), 0.5f, 0.5f, 1, 0, 1f);
        if (sound == OPENING_SOUND) {
            soundPool.setLoop(streamID, 2);
        }
    }

    public static void playSirenSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(SIREN_SOUND)) {
                System.out.println("Play Siren sound");
                playSound(SIREN_SOUND);
            } else {
                throw new SoundInitializationError("Siren Sound not initialized!");
            }
        }
    }

    public static void playPacmanDiesSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(PACMAN_DIES_SOUND)) {
                System.out.println("Play Pacman Dies sound");
                playSound(PACMAN_DIES_SOUND);
            } else {
                throw new SoundInitializationError("Pacman Dies Sound not initialized!");
            }
        }
    }

    public static void playOpeningSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(OPENING_SOUND)) {
                System.out.println("Play Opening sound");
                playSound(OPENING_SOUND);
            } else {
                throw new SoundInitializationError("Opening Sound not initialized!");
            }
        }
    }

    public static void playIntermissionSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(INTERMISSION_SOUND)) {
                System.out.println("Play Intermission sound");
                playSound(INTERMISSION_SOUND);
            } else {
                throw new SoundInitializationError("Intermission Sound not initialized!");
            }
        }
    }

    public static void playExtraLiveSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EXTRA_LIVE_SOUND)) {
                System.out.println("Play Extra Live sound");
                playSound(EXTRA_LIVE_SOUND);
            } else {
                throw new SoundInitializationError("Extra Live Sound not initialized!");
            }
        }
    }

    public static void playEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_SOUND)) {
                System.out.println("Play Eat Sound");
                playSound(EAT_SOUND);
            } else {
                throw new SoundInitializationError("Eat Sound not initialized!");
            }
        }
    }

    public static void playShortEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(SHORT_EAT_SOUND)) {
                System.out.println("Play Short Eat sound");
                playSound(SHORT_EAT_SOUND);
            } else {
                throw new SoundInitializationError("Short Eat Sound not initialized!");
            }
        }
    }

    public static void playEatCherrySound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_CHERRY_SOUND)) {
                System.out.println("Play Eat Cherry sound");
                playSound(EAT_CHERRY_SOUND);
            }  else {
                throw new SoundInitializationError("Eat Cherry Sound not initialized!");
            }
        }
    }

    public static void playEatGhostSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_GHOST_SOUND)) {
                System.out.println("Play Eat Ghost sound");
                playSound(EAT_GHOST_SOUND);
            }  else {
                throw new SoundInitializationError("Eat Ghost Sound not initialized!");
            }
        }
    }

    public static boolean isSoundOn() {
        return sound;
    }

    public static void setSoundOn(boolean b) {
        sound = b;
    }

}

但是,我有一个或多或少较大的音频文件,我想在启动时播放它。但是,如果我调用initializeOpenSound(),然后调用playOpening Sound(),则不会播放声音并且logcat输出:

07-15 09:11:02.121: WARN/SoundPool(14273):   sample 1 not READY

在SDK 2.2中,我将使用 SoundPool.OnLoadCompleteListener 但如何使用 SDK 2.1 实现相同的效果?

编辑:我也很高兴提供一个不包含侦听器或类似内容的解决方案,但要么尝试播放声音文件,直到成功。

This question is related to this one.

I managed to modify my code to use SoundPool instead of AudioManager. Know it works more or less.

public class Sound {

    private static boolean sound = true;

    private static final int EAT_SOUND = 1;
    private static final int SHORT_EAT_SOUND = 2;
    private static final int EAT_CHERRY_SOUND = 3;
    private static final int EAT_GHOST_SOUND = 4;
    private static final int EXTRA_LIVE_SOUND = 5;
    private static final int INTERMISSION_SOUND = 6;
    private static final int OPENING_SOUND = 7;
    private static final int PACMAN_DIES_SOUND = 8;
    private static final int SIREN_SOUND = 9;

    private static Context context;
    private static SoundPool soundPool;
    private static HashMap<Integer, Integer> soundPoolMap;

    public static void initializeOpenSound(Context con) {
        context = con;
        soundPool = new SoundPool(2, AudioManager.STREAM_RING, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(OPENING_SOUND, soundPool.load(context, R.raw.opening_song, 1));
    }

    public static void initializeSounds() {
        soundPoolMap.put(EAT_SOUND, soundPool.load(context, R.raw.eating, 1));
        soundPoolMap.put(SHORT_EAT_SOUND, soundPool.load(context, R.raw.eating_short, 1));
        soundPoolMap.put(EAT_CHERRY_SOUND, soundPool.load(context, R.raw.eating_cherry, 1));
        soundPoolMap.put(EAT_GHOST_SOUND, soundPool.load(context, R.raw.eating_ghoasts, 1));
        soundPoolMap.put(EXTRA_LIVE_SOUND, soundPool.load(context, R.raw.extra_lives, 1));
        soundPoolMap.put(INTERMISSION_SOUND, soundPool.load(context, R.raw.intermission, 1));
        soundPoolMap.put(PACMAN_DIES_SOUND, soundPool.load(context, R.raw.pac_man_dies, 1));
        soundPoolMap.put(SIREN_SOUND, soundPool.load(context, R.raw.siren, 1));
    }

    private static void playSound(int sound) {
        int streamID = soundPool.play(soundPoolMap.get(sound), 0.5f, 0.5f, 1, 0, 1f);
        if (sound == OPENING_SOUND) {
            soundPool.setLoop(streamID, 2);
        }
    }

    public static void playSirenSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(SIREN_SOUND)) {
                System.out.println("Play Siren sound");
                playSound(SIREN_SOUND);
            } else {
                throw new SoundInitializationError("Siren Sound not initialized!");
            }
        }
    }

    public static void playPacmanDiesSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(PACMAN_DIES_SOUND)) {
                System.out.println("Play Pacman Dies sound");
                playSound(PACMAN_DIES_SOUND);
            } else {
                throw new SoundInitializationError("Pacman Dies Sound not initialized!");
            }
        }
    }

    public static void playOpeningSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(OPENING_SOUND)) {
                System.out.println("Play Opening sound");
                playSound(OPENING_SOUND);
            } else {
                throw new SoundInitializationError("Opening Sound not initialized!");
            }
        }
    }

    public static void playIntermissionSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(INTERMISSION_SOUND)) {
                System.out.println("Play Intermission sound");
                playSound(INTERMISSION_SOUND);
            } else {
                throw new SoundInitializationError("Intermission Sound not initialized!");
            }
        }
    }

    public static void playExtraLiveSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EXTRA_LIVE_SOUND)) {
                System.out.println("Play Extra Live sound");
                playSound(EXTRA_LIVE_SOUND);
            } else {
                throw new SoundInitializationError("Extra Live Sound not initialized!");
            }
        }
    }

    public static void playEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_SOUND)) {
                System.out.println("Play Eat Sound");
                playSound(EAT_SOUND);
            } else {
                throw new SoundInitializationError("Eat Sound not initialized!");
            }
        }
    }

    public static void playShortEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(SHORT_EAT_SOUND)) {
                System.out.println("Play Short Eat sound");
                playSound(SHORT_EAT_SOUND);
            } else {
                throw new SoundInitializationError("Short Eat Sound not initialized!");
            }
        }
    }

    public static void playEatCherrySound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_CHERRY_SOUND)) {
                System.out.println("Play Eat Cherry sound");
                playSound(EAT_CHERRY_SOUND);
            }  else {
                throw new SoundInitializationError("Eat Cherry Sound not initialized!");
            }
        }
    }

    public static void playEatGhostSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (soundPoolMap.containsKey(EAT_GHOST_SOUND)) {
                System.out.println("Play Eat Ghost sound");
                playSound(EAT_GHOST_SOUND);
            }  else {
                throw new SoundInitializationError("Eat Ghost Sound not initialized!");
            }
        }
    }

    public static boolean isSoundOn() {
        return sound;
    }

    public static void setSoundOn(boolean b) {
        sound = b;
    }

}

However, I have a more or less big audio file which I want to play during startup. However if i call initializeOpenSound() and then playOpening Sound(), then the sound isn't played and logcat outputs:

07-15 09:11:02.121: WARN/SoundPool(14273):   sample 1 not READY

In SDK 2.2 I would use SoundPool.OnLoadCompleteListener but how can I achieve the same using SDK 2.1?

Edit: I would also be happy width a solution that doesn't include a listener or something like that, but either try to play the sound file until it was successful.

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

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

发布评论

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

评论(2

第七度阳光i 2024-09-17 03:41:10

根据我所读到的内容,如果您尝试在初始化后立即播放声音,它不会准备好,您将收到此错误。您可以创建一个加载页面,然后等待几秒钟,直到确定声音已加载。

还要确保您的文件大小不超过 1048576 字节,这是 Soundpool 的限制。如果你想播放背景音乐或其他东西,我认为你必须使用媒体播放器。

From what I have read if you try to play the sound right after initializing it, it won't be ready and you will get this error. You could make a loading page and just wait for a few seconds until you are sure the sound has loaded.

Also make sure your filesize isn't over 1048576 bytes, thats the limitation for Soundpool. If you want to play background music or something i think you have to use media player.

拥醉 2024-09-17 03:41:10

我为 OnLoadCompleteListener 实现了一个后备实现,我发布了 此处

I implemented a fallback implementation for OnLoadCompleteListener which I posted here.

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