安卓(Android)游戲開發(fā)音效代碼
游戲音效就是我們在玩游戲時出現(xiàn)的音樂,這個也是每個游戲必備的一部分,但有是你做游戲的背景音樂有間斷的感覺的話,我們可以用getCurrentPosition()這個方法來判斷一下聲音播放的偏移。其實這個也是非常簡單的。只要我們在代碼當中設(shè)置好(初始化聲音)和(加載音效資源)就可以了,別的就和音樂播放器的代碼差不多,像開始,停止。不多說了,我們還是先來看看代碼當中是怎么實現(xiàn)音效的吧:
1.音效的音量
int streamVolume;
//定義SoundPool 對象 private SoundPool soundPool; //定義HASH表 private HashMap soundPoolMap; /*Parameters: null * Returns: None. * Description: 初始化聲音系統(tǒng) * Notes: none. ***************************************************************/
2.初始化soundPool
public void initSounds() { //初始化soundPool 對象,第一個參數(shù)是允許有多少個聲音流同時播放,第2個參數(shù)是聲音類型,第三個參數(shù)是聲音的品質(zhì) soundPool = new SoundPool(100, AudioManager.STREAM_MUSIC, 100); //初始化HASH表 soundPoolMap = new HashMap(); //獲得聲音設(shè)備和設(shè)備音量 AudioManager mgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); } /*************************************************************** * Function: loadSfx(); * Parameters: null * Returns: None. * Description: 加載音效資源 * Notes: none. ***************************************************************/
3.加載
public void loadSfx(int raw, int ID) { //把資源中的音效加載到指定的ID(播放的時候就對應(yīng)到這個ID播放就行了) soundPoolMap.put(ID, soundPool.load(context, raw, ID)); } /*************************************************************** * Function: play(); * Parameters: sound:要播放的音效的ID, loop:循環(huán)次數(shù) * Returns: None. * Description: 播放聲音 * Notes: none. ***************************************************************/ public void play(int sound, int uLoop) { soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1, uLoop, 1f); }
- Android 重力傳感器在游戲開發(fā)中的應(yīng)用
- Android游戲開發(fā):實現(xiàn)手勢操作切換圖片的實例
- Android游戲開發(fā) 自定義手勢--輸入法手勢技術(shù)
- Android游戲開發(fā)之黑白棋
- Android游戲開發(fā)學(xué)習之引擎用法實例詳解
- Android游戲開發(fā)學(xué)習②焰火綻放效果實現(xiàn)方法
- Android游戲開發(fā)學(xué)習①彈跳小球?qū)崿F(xiàn)方法
- Android游戲開發(fā)實踐之人物移動地圖的平滑滾動處理
- Android 游戲開發(fā)之Canvas畫布的介紹及方法
- Android 游戲開發(fā)入門簡單示例
相關(guān)文章
Android中AutoCompleteTextView自動提示
這篇文章主要為大家詳細介紹了Android中AutoCompleteTextView自動提示的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12android JSON解析數(shù)據(jù) android解析天氣預(yù)報
這篇文章主要為大家詳細介紹了android JSON解析數(shù)據(jù),android天氣預(yù)報JSON數(shù)據(jù)解析,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android app應(yīng)用多語言切換功能實現(xiàn)
這篇文章主要為大家詳細介紹了Android app應(yīng)用多語言切換功能實現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-08-08Android仿微信activity滑動關(guān)閉效果
這篇文章主要為大家詳細介紹了Android仿微信activity滑動關(guān)閉的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01