官术网_书友最值得收藏!

4.3 使用AudioEngine

SimpleAudioEngine一般情況下已經足以滿足需求了,除非需要實現一些更加精確、深入的控制,或者策劃提出了更加復雜的需求(如在Win32下控制每個音效的音量),才需要用到更強大的AudioEngine。在學習AudioEngine之前,先來看一下它的命名空間——experimental,這是試驗中的意思,所以在其穩定之前,建議盡量不要使用。

雖然還在試驗中,但并不妨礙我們先了解一下其新功能。從接口上來看,AudioEngine并沒有音樂和音效的區別,可以使用AudioProfile來配置每一種聲音最多同時播放的實例數、聲音播放的最小時間間隔。相比SimpleAudioEngine,我們并不能控制每個音效的頻率和聲道,但一般也不需要使用這兩個特性。另外在AudioEngine中,可以動態控制每個聲音的循環、音量、播放進度等有用的屬性。AudioEngine還提供了異步加載的接口,還可以設置聲音加載完成和播放結束的回調。接下來看一下AudioEngine的相關接口。

        //初始化AudioEngine
        static bool lazyInit();
        //釋放AudioEngine
        static void end();

        //獲取默認的AudioProfile指針
        //AudioProfile是一個結構體,用于描述一個聲音的播放規則
        //包含了名字、最大實例、兩次播放的最小間隔
        static AudioProfile* getDefaultProfile();
        //播放2D音效(難道以后會增加3D音效?),參數的意義如下
            filePath聲音文件名
            loop是否循環
            volume音量
        //profile AudioProfile指針,傳入空會自動使用DefaultProfile
        //該方法會返回一個聲音ID,通過該聲音ID可以動態控制這個聲音
        static int play2d(const std::string& filePath, bool loop = false, float
        volume = 1.0f, const AudioProfile *profile = nullptr);
        //設置指定聲音實例的循環屬性
        static void setLoop(int audioID, bool loop);
        //判斷一個聲音實例是否循環
        static bool isLoop(int audioID);
        //設置指定聲音實例的音量屬性,音量范圍為0.0~1.0
        static void setVolume(int audioID, float volume);
        //獲取一個聲音實例的音量
        static float getVolume(int audioID);
        //暫停一個聲音實例的播放
        static void pause(int audioID);
        //暫停所有的聲音
        static void pauseAll();
        //恢復一個被暫停的聲音
        static void resume(int audioID);
        //恢復所有被暫停的聲音
        static void resumeAll();
        //停止一個聲音實例的播放
        static void stop(int audioID);
        //停止播放所有聲音
        static void stopAll();
        //設置指定聲音實例當前播放的進度,sec的單位為秒
        static bool setCurrentTime(int audioID, float sec);
        //獲取指定聲音實例當前播放了多少秒
        static float getCurrentTime(int audioID);
        //獲取指定聲音實例的總時長
        static float getDuration(int audioID);
        //獲取指定聲音實例當前的狀態,有以下4種狀態
            ERROR錯誤
            INITIALIZING加載中
            PLAYING播放中
            PAUSED暫停中
        static AudioState getState(int audioID);
        //設置當指定聲音實例播放完的回調函數,回調函數會傳入聲音ID和聲音文件名作為參數,沒有
        返回值
        static void setFinishCallback(int audioID, const std::function<void
        (int, const std::string&)>& callback);
        //獲取最大的聲音實例數(限制)
        static int getMaxAudioInstance() {return _maxInstances; }
        //設置最大的聲音實例數
        static bool setMaxAudioInstance(int maxInstances);
        //卸載指定聲音文件的緩存
        static void uncache(const std::string& filePath);
        //卸載所有聲音文件的緩存
        static void uncacheAll();
        //獲取指定聲音實例的AudioProfile配置指針
        static AudioProfile* getProfile(int audioID);
        //根據AudioProfile的名字來獲取AudioProfile配置指針
        static AudioProfile* getProfile(const std::string &profileName);
        //傳入聲音文件名,異步加載該聲音文件
        static  void  preload(const  std::string&  filePath)  {  preload(filePath,
        nullptr); }
        //傳入聲音文件名和加載完成的回調,異步加載該聲音文件
        //回調函數會傳入文件加載是否成功作為參數
        static void preload(const std::string& filePath, std::function<void(bool
        isSuccess)> callback);
主站蜘蛛池模板: 沾化县| 左贡县| 韶关市| 浮梁县| 武清区| 龙江县| 图片| 和顺县| 丰原市| 拜城县| 深水埗区| 西吉县| 阳山县| 石景山区| 聂荣县| 柘城县| 铁岭县| 定西市| 丰城市| 微博| 上思县| 大安市| 洛浦县| 孙吴县| 富宁县| 赞皇县| 北宁市| 许昌市| 麻栗坡县| 通海县| 门源| 扬中市| 巩留县| 周至县| 安塞县| 灵石县| 房山区| 五峰| 镇巴县| 塘沽区| 屯门区|