Android中MediaPlayer和SoundPool的聲音管理,直接看代碼
- import java.util.HashMap;
-
- import yoyo.activity.CrazyActivity;
- import yoyo.activity.R;
- import android.content.Context;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.SoundPool;
-
- /***
- * @time 2011-10-16 12:03:15
- * @author YangWenwei
- * @聲音播放管理器
- */
- public class MediaManager {
-
- /**
- * 聲音開關 背景音樂
- */
- private boolean blnOpenBgSound;
-
- /**
- * 聲音開關 特效音樂
- */
- private boolean blnOpenEffectSound;
-
- /**
- * media 背景音樂
- */
- public static final int STATIC_MEDIA_TYPE_BGSOUND = 0;
-
- public static final int STATIC_MEDIA_TYPE_COUNT = STATIC_MEDIA_TYPE_BGSOUND + 1;
-
- private int[] mediaListID = {
- R.raw.backsound
- };
-
- /**
- * sound
- */
- public static final int STATIC_SOUND_TYPE_DINGDONG = 0;
-
- public static final int STATIC_SOUND_TYPE_COUNT = STATIC_SOUND_TYPE_DINGDONG + 1;
-
- private int[] soundListID = {
- R.raw.dingdong
- };
-
-
- private final int maxStreams = 10; //streamType音頻池的最大音頻流數目為10
- private final int srcQuality = 100;
- private final int soundPriority = 1;
- private final float soundSpeed = 1f;//播放速度 0.5 -2 之間
-
- /**
- * 游戲音效
- */
- private SoundPool soundPool;
- private HashMap <Integer, Integer> soundPoolMap;
- private HashMap <Integer, MediaPlayer> mediaMap;
-
- private static MediaManager mediaManager;
-
- private MediaManager(){
- initMediaPlayer();
- initSoundPool();
- }
-
- /***
- * 實例MediaManager
- * @return
- */
- public static MediaManager getInstance(){
-
-
-
- if(mediaManager == null){
- mediaManager = new MediaManager();
- }
- return mediaManager;
- }
-
- /***
- * 是否開啟背景音樂
- */
- public void setOpenBgState(boolean bgSound){
- blnOpenBgSound = bgSound;
- if(!bgSound && mediaMap != null){
- for (int i = 0; i < mediaMap.size(); i++) {
- mediaMap.get(i).pause();
- }
- }
- }
- /***
- * 是否開啟特效音樂
- */
- public void setOpenEffectState(boolean effectSound){
- blnOpenEffectSound = effectSound;
- if(!effectSound && soundPoolMap != null){
- for (int i = 0; i < soundPoolMap.size(); i++) {
- soundPool.pause(soundPoolMap.get(i));
- }
- }
- }
-
-
- private void initMediaPlayer(){
- mediaMap = new HashMap<Integer, MediaPlayer>();
- for (int i = 0; i < STATIC_SOUND_TYPE_COUNT; i++) {
- MediaPlayer mediaPlayer = MediaPlayer.create(CrazyActivity.getInstence(), mediaListID[i]);
- mediaMap.put(i, mediaPlayer);
- }
- }
-
- private void initSoundPool(){
- soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, srcQuality);
- soundPoolMap = new HashMap<Integer, Integer>();
- for (int i = 0; i < STATIC_SOUND_TYPE_COUNT; i++) {
- soundPoolMap.put(i, soundPool.load(CrazyActivity.getInstence(), soundListID[i], soundPriority));
- }
- }
-
- /**
- * 播放MediaPlayer音樂
- */
- public void playMedia(int mediaType){
- if(!blnOpenBgSound){
- return;
- }
- MediaPlayer mediaPlayer = mediaMap.get(mediaType);
- if(!mediaPlayer.isPlaying()){
- mediaPlayer.start();
- }
- }
-
- /**
- * 暫停MediaPlayer音樂
- */
- public void pauseMedia(int mediaType){
- MediaPlayer mediaPlayer = mediaMap.get(mediaType);
- if(mediaPlayer.isPlaying()){
- mediaPlayer.pause();
- }
- }
-
- /**
- * 播放soundPlayer音樂
- */
- public void playSound(int soundID, int loop){
- if(!blnOpenEffectSound){
- return;
- }
- AudioManager audioManager = (AudioManager)CrazyActivity.getInstence().getSystemService(Context.AUDIO_SERVICE);
- float streamVolumeCurrent = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
- float streamVolumeMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
- float volume = streamVolumeCurrent / streamVolumeMax;
- soundPool.play(soundPoolMap.get(soundID), volume, volume, soundPriority, loop, soundSpeed);
- }
-
- /**
- * 播放soundPlayer音樂
- */
- public void pauseSound(int soundID){
- soundPool.pause(soundPoolMap.get(soundID));
- }
- }
在使用過程中給出用法
- //初始化游戲聲音
- MediaManager.getInstance(); //可以做為初始化
-
- //播放MediaPlayer音頻
-
- MediaManager.getInstance().playMedia(MediaManager.STATIC_MEDIA_TYPE_BGSOUND);
-
- //播放soundPool音頻
-
- MediaManager.getInstance().playSound(MediaManager.STATIC_SOUND_TYPE_DINGDONG);