代码之家  ›  专栏  ›  技术社区  ›  anon

声音在HTC Desire上工作,但在T-Mobile G1上不工作

  •  1
  • anon  · 技术社区  · 14 年前

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

    这对我的宏达电的愿望非常好,但在我的T-mobileg1,只有OpeningSound可以发挥。其他声音文件不会播放,但也不例外。两个设备都使用2.1。

    有什么提示吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   ognian    14 年前

    只需使用SoundPool的短声音