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;
-
-
-
-
-
-
- public class MediaManager {
-
-
-
-
- private boolean blnOpenBgSound;
-
-
-
-
- private boolean blnOpenEffectSound;
-
-
-
-
- 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
- };
-
-
-
-
- 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;
- private final int srcQuality = 100;
- private final int soundPriority = 1;
- private final float soundSpeed = 1f;
-
-
-
-
- private SoundPool soundPool;
- private HashMap
- private HashMap
-
- private static MediaManager mediaManager;
-
- private MediaManager(){
- initMediaPlayer();
- initSoundPool();
- }
-
-
-
-
-
- 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
- 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
- for (int i = 0; i < STATIC_SOUND_TYPE_COUNT; i++) {
- soundPoolMap.put(i, soundPool.load(CrazyActivity.getInstence(), soundListID[i], soundPriority));
- }
- }
-
-
-
-
- public void playMedia(int mediaType){
- if(!blnOpenBgSound){
- return;
- }
- MediaPlayer mediaPlayer = mediaMap.get(mediaType);
- if(!mediaPlayer.isPlaying()){
- mediaPlayer.start();
- }
- }
-
-
-
-
- public void pauseMedia(int mediaType){
- MediaPlayer mediaPlayer = mediaMap.get(mediaType);
- if(mediaPlayer.isPlaying()){
- mediaPlayer.pause();
- }
- }
-
-
-
-
- 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);
- }
-
-
-
-
- public void pauseSound(int soundID){
- soundPool.pause(soundPoolMap.get(soundID));
- }
- }
在使用過程中給出用法
-
- MediaManager.getInstance();
-
-
-
-
------分隔線----------------------------
- 上一篇:Android子線程在沒有ViewRoot的情況下能刷新UI嗎?
- 下一篇:Android基礎教程:讀寫SharedPreferences中的數據