實現遊戲音效的預載入
阿新 • • 發佈:2018-12-30
遊戲中有很多東西是需要預載入進來的,這樣可以使遊戲更流暢,雖然,遊戲中音效佔用的資源比較少,但是,還是有必要為音效做一個預載入的,這裡用安卓原生的方法實現了一個音效載入系統。
思路:
安卓中游戲的資源通常都是根據id來載入的(這個id可以在R.java檔案中找到),在系統中使用了兩個HashMap,一個儲存資源的ID,一個儲存音效載入後的ID,這兩個HashMap有共同的key值將兩個表聯絡起來。
系統中實現了資源的載入,根據資源ID播放音效,解除安裝資源。
下面是腦圖的地址,是系統中詳細函式的介紹。
http://naotu.baidu.com/file/079133fa68a2e51d5800f905e77ef67b?token=d52654a7a8883f62
Demo程式碼:
MainActivity.java
package com.example.soundloadsystem; import android.R.integer; import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { public static MainActivity _instance ; public SoundLoadSystem soundLoadSystem; public SoundPool m_soundPool ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); setContentView(R.layout.main); _instance = this; m_soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); soundLoadSystem = SoundLoadSystem.Instance(); addSoundRes(); initUICallback(); } public void addSoundRes() { soundLoadSystem.add(R.raw.sound_0); soundLoadSystem.add(R.raw.sound_1); soundLoadSystem.add(R.raw.sound_2); soundLoadSystem.add(R.raw.sound_3); soundLoadSystem.add(R.raw.sound_4); soundLoadSystem.add(R.raw.sound_5); } public void initUICallback() { Button btn1 = (Button)findViewById(R.id.Button01); Button btn2 = (Button)findViewById(R.id.Button02); Button btn3 = (Button)findViewById(R.id.Button04); btn1.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub soundLoadSystem.load(); } } ); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub\ AudioManager am = (AudioManager)_instance.getSystemService(Context.AUDIO_SERVICE); float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC); float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = streamVolumeCurrent / streamVolumeMax; m_soundPool.play(soundLoadSystem.getRandomID(), volume, volume, 1, 0, 1.0f); } }); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub soundLoadSystem.unload(); } }); } }
SoundLoadSystem.java
package com.example.soundloadsystem; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Random; import android.R.integer; import android.content.Context; import android.drm.DrmStore.RightsStatus; import android.media.AudioManager; import android.media.SoundPool; import android.util.Log; public class SoundLoadSystem { public static SoundLoadSystem _instance ; public HashMap<Integer, Integer> m_soundResIDMap ; public HashMap<Integer, Integer> m_soundIDMap; public int m_index = 0 ; public static SoundLoadSystem Instance() { if (_instance == null) { _instance = new SoundLoadSystem(); } return _instance ; } public static void Destory() { _instance = null ; System.gc(); } private SoundLoadSystem() { // TODO Auto-generated constructor stub m_soundResIDMap = new HashMap<Integer, Integer>(); m_soundIDMap = new HashMap<Integer, Integer>(); } public void add(Integer id) { if (isHave(id) == false) { m_soundResIDMap.put(m_index, id); m_index++ ; } } public void load() { Context cot = MainActivity._instance ; SoundPool spPool = MainActivity._instance.m_soundPool; int per = 0 ; Iterator iterator = m_soundResIDMap.entrySet().iterator() ; while (iterator.hasNext()) { Map.Entry<Integer,Integer> entry = (Map.Entry<Integer,Integer>)iterator.next(); Integer index = entry.getKey(); Integer id = entry.getValue(); Integer spID = spPool.load(cot, id,1); m_soundIDMap.put(index, spID); per++; } } public int getLength() { //Because m_index start from 0,so the length is m_index + 1. return m_soundIDMap.size(); } public boolean isHave(Integer id) { return m_soundResIDMap.containsValue(id) ; } public Integer getRandomID() { if (m_soundIDMap.size() == 0 || m_soundResIDMap.size() == 0 ) { Log.e("load_error","Doesn't load any res."); return -1; } Integer ret = -1; Random random = new Random(); Integer randInteger = random.nextInt(getLength()); ret = m_soundIDMap.get(randInteger); return ret; } public void unload() { SoundPool spPool = MainActivity._instance.m_soundPool; spPool.unload(1); Iterator iterator = m_soundIDMap.entrySet().iterator() ; while (iterator.hasNext()) { Map.Entry<Integer,Integer> entry = (Map.Entry)iterator.next(); Integer id = entry.getValue(); spPool.unload(id); } m_soundIDMap.clear(); m_soundIDMap = new HashMap<Integer, Integer>(); } public int getSoundIDByResID(Integer resID) { int soundID = -1; int index = -1; boolean isHave = m_soundResIDMap.containsKey(resID); if (isHave) { Iterator iterator = m_soundIDMap.entrySet().iterator() ; while (iterator.hasNext()) { Map.Entry<Integer,Integer> entry = (Map.Entry)iterator.next(); Integer idx = entry.getKey(); Integer id = entry.getValue(); if (id == resID) { index = idx ; } } soundID = m_soundIDMap.get(index); } else { Log.e("sound_res","can't find sound which id is" + resID); } return soundID ; } public void reset() { m_soundIDMap.clear(); m_soundResIDMap.clear(); m_soundIDMap = new HashMap<Integer, Integer>(); m_soundResIDMap = new HashMap<Integer, Integer>(); m_index = 0; } }
Demo下載地址:http://download.csdn.net/detail/c_boy_lu/9300661