java - 声音适用于 HTC Desire 但不适用于 T-Mobile G1

标签 java android audio

我定义了以下声音类来在我的 Pacman 游戏中播放声音:

public class Sound {

    private static boolean sound = true;

    private static MediaPlayer eatSound;
    private static MediaPlayer shortEatSound;
    private static MediaPlayer eatCherry;
    private static MediaPlayer eatGhost;
    private static MediaPlayer extraLive;
    private static MediaPlayer intermission;
    private static MediaPlayer openingSound;
    private static MediaPlayer pacmanDiesSound;
    private static MediaPlayer sirenSound;

    public static void initializeOpenSound(Context context) {
        openingSound = MediaPlayer.create(context, R.raw.opening_song);
    }

    public static void initializeSounds(Context context) {
        eatSound = MediaPlayer.create(context, R.raw.eating);
        shortEatSound = MediaPlayer.create(context, R.raw.eating_short);
        eatCherry = MediaPlayer.create(context, R.raw.eating_cherry);
        eatGhost = MediaPlayer.create(context, R.raw.eating_ghoasts);
        extraLive = MediaPlayer.create(context, R.raw.extra_lives);
        intermission = MediaPlayer.create(context, R.raw.intermission);
        pacmanDiesSound = MediaPlayer.create(context, R.raw.pac_man_dies);
        sirenSound = MediaPlayer.create(context, R.raw.siren);
    }

    public static int getOpeningSoundDuration() throws SoundInitializationError {
        if (openingSound != null) {
            return openingSound.getDuration();
        } else {
            throw new SoundInitializationError("Opening Sound not initialized!");
        }
    }

    public static void playSirenSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (sirenSound != null) {
                sirenSound.start();
            } else {
                throw new SoundInitializationError("Siren Sound not initialized!");
            }
        }
    }

    public static void playPacmanDiesSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (pacmanDiesSound != null) {
                pacmanDiesSound.start();
            } else {
                throw new SoundInitializationError("Pacman Dies Sound not initialized!");
            }
        }
    }

    public static void playOpeningSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (openingSound != null) {
                openingSound.setLooping(true);
                openingSound.start();
            } else {
                throw new SoundInitializationError("Opening Sound not initialized!");
            }
        }
    }

    public static void playIntermissionSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (intermission != null) {
                intermission.start();
            } else {
                throw new SoundInitializationError("Intermission Sound not initialized!");
            }
        }
    }

    public static void playExtraLiveSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (extraLive != null) {
                extraLive.start();
            } else {
                throw new SoundInitializationError("Extra Live Sound not initialized!");
            }
        }
    }

    public static void playEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (eatSound != null) {
                eatSound.start();
            } else {
                throw new SoundInitializationError("Eat Sound not initialized!");
            }
        }
    }

    public static void playShortEatSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (shortEatSound != null) {
                shortEatSound.start();
            } else {
                throw new SoundInitializationError("Short Eat Sound not initialized!");
            }
        }
    }

    public static void playEatCherrySound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (eatCherry != null) {
                eatCherry.start();
            } else {
                throw new SoundInitializationError("Eat Cherry Sound not initialized!");
            }
        }
    }

    public static void playEatGhostSound() throws SoundInitializationError {
        if (isSoundOn()) {
            if (eatGhost != null) {
                eatGhost.start();
            } else {
                throw new SoundInitializationError("Eat Ghost Sound not initialized!");
            }
        }
    }

    public static void stopOpeningSound() throws SoundInitializationError {
        if (openingSound != null) {
            openingSound.stop();
        } else {
            throw new SoundInitializationError("Opening Sound not initialized!");
        }
    }

    public static boolean isSoundOn() {
        return sound;
    }

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

}

这在我的 HTC Desire 上运行良好,但在我的 T-Mobile G1 上,只能播放 OpeningSound。其他声音文件不播放,但也不异常(exception)。两种设备都使用 2.1。

有什么提示吗?

最佳答案

短音只用SoundPool

关于java - 声音适用于 HTC Desire 但不适用于 T-Mobile G1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236070/

相关文章:

c - YIN-Frequency-Detection 和泛音(吉他弦)

java - 在第 1 行的 'update, stock_id) values (0, 0.0, 10018, 1, null, 0, null, 0)' 附近使用正确的语法

android - FFmpeg Android库压缩后旋转视频

android - 为什么我的应用程序没有出现在 chromecast.com/apps 中?

android - 读出网络通话设置(SIP 账号)

iphone - 在 iPhone 上传输音频时是否可以减少背景噪音?

ios - Xcode将许多音频文件预加载到一个AVAudioPLayer中?

java - 在循环中从 map 中删除值

java - Android 应用程序的后端建议

java - 基于 Java 的 Web 应用程序的 Windows 身份验证,如何?