Project Summary: audio playback problems, project summary sound effects

Source: Internet
Author: User

Project Summary: audio playback problems, project summary sound effects

Project Summary-audio playback Problems

DionysosLai (906391500@qq.com)

Cocos2dx uses a relatively simple and rough voice system. From the name of The Voice System SimpleAudioEngine, it can be seen that the system itself is not very complex, so when using a sound system, there will be some obscure details that need our special attention during development.

Differences between voice and sound effects

In general, cocos2dx only uses two sound channel modes: backmuscic and effectmusic. The former is used to play background music, and the latter is generally used to play sound effects. The difference between background and sound effects is that the former is relatively large and the latter is relatively small. At the same time, the background sound provides the function of checking whether the playback is complete, but the sound effect is not. If you need to detect audio playback problems in the game, you can only modify the engine code and add a voice channel. In our company's development project, a voice channel is added for playing character sounds because the game needs to detect sound playback problems at all times.

On the other hand, in terms of pre-loading, the background sound does not need to be preloaded, and only the sound effect needs to be preloaded. At the same time, if two background sounds are pre-loaded, the memory of the previous background sound will be automatically discarded. This problem does not occur in terms of sound effects.

Sound Effect pre-loading problems

The sound effect is certainly loaded into the memory, and the preLoadEffect interface is not used in time to pre-load the sound effect. When we play the sound for the first time, the sound effect will also be loaded into the memory, then play the sound. This is why sometimes the first sound effect of a game is not played, or when the first sound effect is played, the game obviously gets stuck.

On the other hand, the sound effects of pre-loading consume a certain amount of time and cannot be loaded too much at a time. This scenario is generally used to load the sound effects of the scenario. When you exit this scenario, you can release the audio memory.

At the same time, when the sound effect is loaded, or when the sound effect is not loaded, the game enters the background when the sound effect is played. It is very likely that the system will be suspended and restarted. This is an inevitable bug. The cause of this problem is that pauseAllEffects is executed during the sound loading process. I believe that the appDelegate code is as follows:

// This function will be called when the app is inactive. When comes a phone call,it's be invoked toovoid AppDelegate::applicationDidEnterBackground() {    CCDirector::sharedDirector()->stopAnimation();    // if you use SimpleAudioEngine, it must be pause    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();SimpleAudioEngine::sharedEngine()->pauseVoice();SimpleAudioEngine::sharedEngine()->pauseAllEffects();}// this function will be called when the app is active againvoid AppDelegate::applicationWillEnterForeground() {    CCDirector::sharedDirector()->startAnimation();    // if you use SimpleAudioEngine, it must resume here    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();SimpleAudioEngine::sharedEngine()->resumeVoice();SimpleAudioEngine::sharedEngine()->resumeAllEffects();}

 

This problem is basically inevitable, or it cannot be solved as far as possible.

Loop sound playback Problems

Sometimes, some sound effects need to be played cyclically. During loop playback, various problems may occur on the PC end. For example, if the game cannot be closed in time, the game returns to the background and enters the foreground. The previously paused loop audio will be played again, therefore, loop sound debugging must be performed on mobile terminals.

Loop sound playback may sometimes fail. This problem is very strange. Generally, if you want to play the loop sound, the sound size should be within 40 kb.

On the other hand, loop sound effects need to record the playback ID value of this sound effect. On the win32 end, the modified sound ID value is based on the sound path pSzFilePathCalculated,Therefore, the ID value obtained before and after the first playback of A sound effect and the second playback of A sound effect is the same. On the Mobile End, the ID value of the sound is automatically increased to + 1 for each playback. If the sound effect is not preloaded, the first sound effect ID value is 0, which is very important. This point, you can refer to the article: http://post.justbilt.com/2013/11/28/cocos2dx_loop_sound/, which is detailed in place.

Multiple sound effects

When multiple sound effects are played at the same time, the game gets stuck and the same sound is played from time to time. If multiple sound effects are played at the same time, the game gets stuck and the sound effects are not played together, there will be a significant gap between the front and back. Therefore, to avoid playing multiple sound effects at the same time, this is why I did not advocate the use of each item to eliminate one more sound problem, but to play the sound effects based on the number of items. For example, if the number of items to be deleted is less than 3, the sound effect is played, the number of items to be deleted is exceeded, and the B sound effect is played. Different types of sound effects are required in programming.

Sound resource release Problems

In general, when the game exits and the entire program ends, the system will release the game memory. However, the system does not release the sound memory. We need to release it on our own initiative. Here we only need to add a simple interface: CocosDenshion: SimpleAudioEngine: end () when the game exits (); you can. It's amazing, too.



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.