Cocos2dx audio module Analysis (2): background music, cocos2dx Module

Source: Internet
Author: User

Cocos2dx audio module Analysis (2): background music, cocos2dx Module

Cocos2dx audio module Analysis (2): Background Music Section

I have analyzed some things in (1). Next I will analyze the background music file playback Based on the android platform: 1. This is only the pre-loading of background music. Why is pre-loading required? It is time-consuming to load music files. If we do not pre-load the file, we can play it directly, but there will be a certain delay, because if there is no pre-load, we can play it directly, it will first load the music file and then play it. Void SimpleAudioEngine: preloadBackgroundMusic (const char * pszFilePath) {std: string fullPath = getFullPathWithoutAssetsPrefix (pszFilePath); preloadBackgroundMusicJNI (fullPath. c_str ();} loads background music and calls the android end: 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 // if we play a new background music file, then we need to release the old player and then create a new // Releases resources associated with this MediaPlayer object. if (this. mBackgroundMediaPlayer! = Null) {this. mBackgroundMediaPlayer. release ();} // create a media player instance called the MediaPlayer class this. mBackgroundMediaPlayer = this. createMediaplayer (pPath); // record the path // record the currently played background music file, because the next time you play the same music // file, then we can play the video directly without re-creating the MediaPlayer instance this. mCurrentPath = pPath ;}----- >>>/*** create mediaplayer for music ** @ param pPath * the pPath relative to assets * @ return */private MediaPlayer createMediaplayer (fina L String pPath) {MediaPlayer mediaPlayer = new MediaPlayer (); try {// differentiate the absolute path from the path in the package. When the final goal is to set the playback source if (pPath. startsWith ("/") {final FileInputStream FD = new FileInputStream (pPath); mediaPlayer. setDataSource (FCM. getFD (); FCM. close ();} else {final AssetFileDescriptor assetFileDescritor = this. mContext. getAssets (). openFd (pPath); mediaPlayer. setDataSource (assetFileDescritor. getFileDescriptor (), asset FileDescritor. getStartOffset (), assetFileDescritor. getLength ();} // prepare for the player. This is only an android api. If you do not understand it, check the document. /*** 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 (); // sets the sound volume mediaPlayer. setVolume (this. mLeftVol Ume, this. mRightVolume);} catch (final Exception e) {mediaPlayer = null; Log. e (Cocos2dxMusic. TAG, "error:" + e. getMessage (), e) ;}return mediaPlayer;} 2. music playing function // pszFilePath: music file name // bLoop: whether to play the video cyclically. We usually set the music file to play cyclically, void SimpleAudioEngine: playBackgroundMusic (const char * pszFilePath, bool bLoop) {std: string fullPath = callback (pszFilePath); playBackgroundMusicJNI (fullPath. c_st R (), bLoop) ;}--- >>> in the end, the playBackgroundMusic function on the android end is called, and the file path and whether to cyclically play in 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 // if no music file has been played before, re-create one. The above English note clearly shows mBackgroundMediaPlayer = createMediaplayer (path); mCurrentPath = path;} else {if (! MCurrentPath. equals (path) {// play new background music // if the music file played this time is different from the previous one, it is a new music file, // release the old one and create a new one. // 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 // For playing or paused, if (mPaused) {// if it is paused, set the playback progress to 0 and start. // This means that if we call pause and then call play, the // music will be played from the beginning, rather than from the paused place. /*** 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 (start) or resumes playback (resume ). if playback had previusly 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. * The start function has two functions: one is to start playing and the other is to resume playing. * 1. If stopped or never started before (the first time ), start playing from scratch. * 2. If paused is paused, it will be played from where it is paused. */MBackgroundMediaPlayer. start ();} else if (mBackgroundMediaPlayer. isPlaying () {// if it is in the playing status, it will return to the start and play mBackgroundMediaPlayer from the beginning. seekTo (0);} else {// if it is in the stop State, it will be played again. The preceding two functions of the start function are described in mBackgroundMediaPlayer. start () ;}/ * conclusion: in fact, the above three cases are handled separately, and the final effect is the same, that is, playing the background music file from the beginning. * /// Set whether to play mBackgroundMediaPlayer cyclically. setLooping (isLoop); // mPaused indicates that the value is set to false; mPaused = false; // whether the loop playback record is mIsLoop = isLoop;} catch (final Exception e) {Log. e (Cocos2dxMusic. TAG, "playBackgroundMusic: error state") ;}}3. Conclusion: From the analysis above, we can know that if you load the file in advance, you can create a MediaPlayer first, we can play the video directly during playback. If we do not pre-load the video in advance, we can call the playBackgroundMusic function directly or play the video, but there will be a MediaPlayer creation process, there will be some time delay.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.