Playing background music in Cocos2d-x

Source: Internet
Author: User

The example code of playing and stopping background music is as follows:

[HTML]View plaincopy
  1. Simpleaudioengine: getinstance ()-> playbackgroundmusic ("sound/jazzaudio", true );
  2. Simpleaudioengine: getinstance ()-> stopbackgroundmusic ("sound/jazz.pdf ");



Where is the playing code of background music suitable? For example, in the setting scenario, the main code is as follows:

[HTML]View plaincopy
  1. Bool setting: Init ()
  2. {
  3. // Playback code ①
  4. Return true;
  5. }
  6. Void setting: onenter ()
  7. {
  8. Layer: onenter ();
  9. Log ("setting onenter ");
  10. // Playback code ②
  11. }
  12. Void setting: onentertransitiondidfinish ()
  13. {
  14. Layer: onentertransitiondidfinish ();
  15. Log ("setting onentertransitiondidfinish ");
  16. // Playback code ③
  17. }
  18. Void setting: onexit ()
  19. {
  20. Layer: onexit ();
  21. Log ("setting onexit ");
  22. }
  23. Void setting: onexittransitiondidstart ()
  24. {
  25. Layer: onexittransitiondidstart ();
  26. Log ("setting onexittransitiondidstart ");
  27. }
  28. Void setting: cleanup ()
  29. {
  30. Layer: cleanup ();
  31. Log ("setting cleanup ");
  32. }




For playing background music, in theory, we can use the playing code simpleaudioengine: getinstance ()-> playbackgroundmusic ("sound/synthmusic", true) place it in three locations (①, ②, and ③ In the code ). Next we will analyze their differences separately.
1. Place the Code in line ①.
Put the Code in line ① (that is, in the setting: init function). If the background music stop statement is not called in the previous scenario, the background music can be played normally. However, if the preceding scene-layer helloworld: onexit function calls the background music stop statement, the background music will be played several seconds and then stopped.

To explain this phenomenon, we can refer to Section 6.3.2 multi-scenario switching lifecycle. Use the pushscene function to enter the setting scenario from the helloworld implementation scenario. The sequence of Life Cycle Function calls is shown in.

 
It can be seen that the helloworld: onexit call is after setting: init, so when we start playing background music in setting: init, we will call helloworld :: onexit stops playing background music.
Note that a problem may occur regardless of whether the player is playing or stopped on the same file.


2. Place the Code in line ②.
Put the Code in line ② (that is, in the setting: onexit function). If the stop Statement of background music is not called in the previous scenario, the background music can be played normally. If the preceding scene layer helloworld: onexit function has a background music stop statement, the background music will be played several seconds and then stopped. The reason is the same as that in line ① Of the code.


3. Place the Code in line ③
We recommend that you place the Code in line ③, because the onentertransitiondidfinish function is called at the entry layer and the end of the transition animation. If the code is put here, you do not need to consider whether the previous scenario has called the background music stop statement. In addition, the user will not hear the sound first, and then the interface will appear.

To sum up, whether the background music can be played successfully is related to the function in which the background music stop statement is called in the previous scenario. If the previous scenario does not call the stop Statement of background music, the problem will be simple. We can place the playback code in code ①, ②, or ③. However, if the background music stop statement is called in the previous scenario, it is better to play the background music in the onentertransitiondidfinish function.

 

 

More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ development" book exchange discussion site: http://www.cocoagame.net welcome to join the cocos2d-x Technology Discussion Group: 257760386, 327403678

 

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.