cocos2dx 音頻模組分析(2):背景音樂,cocos2dx模組
cocos2dx 音頻模組分析(2): 背景音樂部分
我在(1)已經分析了一些東西,這裡接著分析,這一篇我們主要分析背景音樂檔案的播放,還是基於android平台:1、這裡只是背景音樂的預先載入,為什麼要進行預先載入呢?主要是載入音樂檔案是比較耗時的,如果我們沒有預先載入就直接播放也是可以的,但是會有一定的延時,因為如果沒有預先載入,就直接播放,也是會先進行載入音樂檔案,然後進行播放。void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath){ std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath); preloadBackgroundMusicJNI(fullPath.c_str());}其實載入背景音樂最終調用android端:public void preloadBackgroundMusic(final String pPath) {if ((this.mCurrentPath == null) || (!this.mCurrentPath.equals(pPath))) {// preload new background music// release old resource and create a new one// 如果我們播放的是一個新的背景音樂檔案,那麼我們需要先釋放舊的播放器,然後建立一個新的// Releases resources associated with this MediaPlayer object.if (this.mBackgroundMediaPlayer != null) {this.mBackgroundMediaPlayer.release();} //建立一個播放器即MediaPlayer類的執行個體this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);// record the path// 記錄當前播放的背景音樂檔案,因為下次如果播放的是同一個音樂// 檔案,那麼我們就可以直接進行播放了,不用再重新建立MediaPlayer類的執行個體this.mCurrentPath = pPath; }}----->>>/** * create mediaplayer for music * * @param pPath * the pPath relative to assets * @return */private MediaPlayer createMediaplayer(final String pPath) {MediaPlayer mediaPlayer = new MediaPlayer();try { //對絕對路徑和包裡的路徑進行區分處理,當最終的目的就是設定播放源if (pPath.startsWith("/")) {final FileInputStream fis = new FileInputStream(pPath);mediaPlayer.setDataSource(fis.getFD());fis.close();} else {final AssetFileDescriptor assetFileDescritor = this.mContext.getAssets().openFd(pPath);mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());} //播放器前需要做些準備工作,這個只是android的api,不明白的話,查下文檔。/** * Prepares the player for playback, synchronously. * * After setting the datasource and the display surface, you need to either * call prepare() or prepareAsync(). For files, it is OK to call prepare(), * which blocks until MediaPlayer is ready for playback. * * @throws IllegalStateException if it is called in an invalid state */mediaPlayer.prepare(); //設定聲音音量mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);} catch (final Exception e) {mediaPlayer = null;Log.e(Cocos2dxMusic.TAG, "error: " + e.getMessage(), e);}return mediaPlayer;}2、音樂播放函數//pszFilePath: 音樂檔案名稱//bLoop: 是否迴圈播放,音樂檔案我們一般設定為迴圈播放,看具體情況void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop){ std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath); playBackgroundMusicJNI(fullPath.c_str(), bLoop);}--->>> 最終都會調用到android端的playBackgroundMusic函數,並把檔案路徑,是否迴圈播放傳進來public void playBackgroundMusic(final String path, final boolean isLoop) {if (mCurrentPath == null) {// it is the first time to play background music or end() was called// 如果以前沒有播放過音樂檔案,那麼重新建立一個,上面的英文注釋很清楚mBackgroundMediaPlayer = createMediaplayer(path);mCurrentPath = path;} else {if (!mCurrentPath.equals(path)) {// play new background music //如果這次播放的音樂檔案和上次的不同,即是一個新的音樂檔案,//那麼就需要先釋放掉舊的,然後建立一個新的。// release old resource and create a new oneif (mBackgroundMediaPlayer != null) {mBackgroundMediaPlayer.release();}mBackgroundMediaPlayer = createMediaplayer(path);// record the pathmCurrentPath = path;}}if (mBackgroundMediaPlayer == null) {Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: background media player is null");} else {try {// if the music is playing or paused, stop it// 對playing or paused, stop三種情況進行分別處理if (mPaused) { //如果是暫停狀態,那麼就把播放進度設定到0,然後開始。//這就意味著,如果我們調用了暫停,然後又調用play,那麼//音樂將會從頭開始播放,而不是從暫停地方接著播放。 /** * Seeks to specified time position. * * @param msec the offset in milliseconds from the start to seek to * @throws IllegalStateException if the internal player engine has not been * initialized */mBackgroundMediaPlayer.seekTo(0);/** * Starts(開始) or resumes playback(恢複播放). If playback had previously been paused, * playback will continue from where it was paused. If playback had * been stopped, or never started before, playback will start at the * beginning. * start函數兩個功能,一個是開始播放,一個是恢複播放 * 1、如果stopped或者never started before(第一次開始),那麼就從頭開始播放 * 2、如果paused即暫停,那麼將會從暫停地方接著播放。 */mBackgroundMediaPlayer.start();} else if (mBackgroundMediaPlayer.isPlaying()) { //如果處於播放狀態,則回到開始,從頭播放mBackgroundMediaPlayer.seekTo(0);} else { //如果處於stop狀態,則從新播放,上面已經說明了start函數的兩個作用mBackgroundMediaPlayer.start();} /*總結:其實對上面三種情況分別處理,最終達到的效果都是一樣的,那就是從頭開始播放背景音樂檔案。*///設定是否迴圈播放mBackgroundMediaPlayer.setLooping(isLoop);//mPaused 表示設為false,表示不處於暫停狀態mPaused = false;//是否迴圈播放記錄mIsLoop = isLoop;} catch (final Exception e) {Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: error state");}}}3、總結:從上面的分析我們可以知道,如果預先進行載入即先建立一個MediaPlayer,那麼我們播放時可以直接進行播放,如果我們我們沒有提前進行預先載入,而是直接調用playBackgroundMusic函數,也可以進行播放,只不過會有一個建立MediaPlayer的過程,會有一些時間上的延時。