android 3d遊戲研究(一)(邊學邊寫,多謝高手指正,鞠躬) :聲音

來源:互聯網
上載者:User

很久以前就想學習3d的,但一直沒有時間,最近能閑點,所以開始學習3d的;
今天先寫聲音:
遊戲中的聲音可以分為兩種,背景音樂和動作音樂;
他們有什麼區別:背景音樂,相對與動作音樂它的存在時間要長,所以按這樣的方式我將其引用分為兩種:一般音樂播放(背景音樂),池播放(動作音樂)

(1)池播放:
代碼實現:

SoundPool sp;                            //得到一個聲音池引用
    HashMap<Integer,Integer> spMap;            //得到一個map的引用

//初始化
 sp=new SoundPool(
                5,                 //maxStreams參數,該參數為設定同時能夠播放多少音效
                AudioManager.STREAM_MUSIC,    //streamType參數,該參數設定音訊類型,在遊戲中通常設定為:STREAM_MUSIC
                0                //srcQuality參數,該參數設定音頻檔案的品質,目前還沒有效果,設定為0為預設值。
        );
        spMap=new HashMap<Integer,Integer>();
        spMap.put(1, sp.load(this, R.raw.attack02, 1));
        spMap.put(2, sp.load(this, R.raw.attack14, 1));


//音樂播放
public void playSound(int sound,int number){    //播放聲音,參數sound是播放音效的id,參數number是播放音效的次數
        AudioManager am=(AudioManager)this.getSystemService(this.AUDIO_SERVICE);//執行個體化AudioManager對象
        float audioMaxVolumn=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    //返回當前AudioManager對象的最大音量值
        float audioCurrentVolumn=am.getStreamVolume(AudioManager.STREAM_MUSIC);//返回當前AudioManager對象的音量值
        float volumnRatio=audioCurrentVolumn/audioMaxVolumn;
        sp.play(
                spMap.get(sound),                     //播放的音樂id
                volumnRatio,                         //左聲道音量
                volumnRatio,                         //右聲道音量
                1,                                     //優先順序,0為最低
                number,                             //迴圈次數,0無不迴圈,-1無永遠迴圈
                1                                    //回放速度 ,該值在0.5-2.0之間,1為正常速度
        );
    }


//事件的引用:
playSound(1,1);        //播放第一首音效,迴圈一遍
sp.pause(spMap.get(1)); // 暫停第一首音效
sp.stop(spMap.get(1));// 停止第一首音效

好了,以上為池播放的簡單使用;

(2) 一般播放
MediaPlayer mp;                            //MediaPlayer引用
AudioManager am;                        //AudioManager引用

//初始化音頻
mp=new MediaPlayer();

//在第一次播放前,必須
try{
    mp.setDataSource("/sdcard/dl.mid");        //載入音頻,進入Initialized狀態。
    }catch(Exception e){e.printStackTrace();}
 try{
    mp.prepare();                //進入prepared狀態。
    }catch(Exception e){e.printStackTrace();}

//事件的引用: www.2cto.com
mp.start();    //播放音樂
mp.pause();    //暫停音樂
mp.stop();       //停止音樂

//mp.isPlaying();//判斷是否為播放狀態

//初始化音量
am=(AudioManager) this.getSystemService(this.AUDIO_SERVICE);
am.adjustVolume(AudioManager.ADJUST_RAISE, 0);        //增大音量
am.adjustVolume(AudioManager.ADJUST_LOWER, 0);        //減小音量


好的,以上為聲音的簡單調度;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.