IOS project: the use of NSThread sleepForTimeInterval, the game is interrupted in the middle (answer the phone or click the Home button), and return to the game audio sound effect failure problem

IOS project: the use of NSThread sleepForTimeInterval, the game is interrupted in the middle (answer the phone or click the Home button), and return to the game audio sound effect failure problem

Device/Engine: Mac(11.7)/cocos

Development tools: Xcode

Development language: c++/java

错误信息:AVAudioSession_iOS.mm:1271 Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

Recently during the project, users reported that all the sound effects will disappear when the game is interrupted in the middle of the game, and the experience will not be very good. At the beginning, I thought that the stopEffect option might be accidentally set somewhere, but I checked it and found nothing, so Just break the point to test, and found that there is an error message as follows:AVAudioSession_iOS.mm:1271 Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

At first glance, it can be judged that it is an audio conflict problem. I checked it with Chatgpt and gave a check to see if there are other audio streams running when the audio is restored, and the delay can be increased to avoid this situation, as shown below:
insert image description here
insert image description here

The idea of ​​modification is basically obtained through the prompt. The source code in the previous project is to directly call the code to restore the audio. The hidden danger of this piece is that it is directly called and executed visually, but when the program is running, the method call and execution will be delayed. , when running the code to restore the audio, there may be other functions in the program that are already running, and there may be audio stream conflicts. So the modification method is to delay calling the relevant audio recovery code.

The following figure is the code at the beginning:

void AppDelegate::applicationWillEnterForeground()
{
    
    
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->resumeAllEffects();
    CCDirector::sharedDirector()->startAnimation();
}

The following is the modified code, which is delayed in the .mm file:

void AppDelegate::applicationWillEnterForeground()
{
    
    
    DeviceManager::sharedManager()->resumeAllSounds();
    CCDirector::sharedDirector()->startAnimation();
}

The .mm file is modified as follows:

void DeviceManager::resumeAllSounds()
{
    
    
    dispatch_queue_t resumeQueue = dispatch_queue_create("resumeSound", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(resumeQueue, ^{
    
    
        CCLog("check resume");
        [NSThread sleepForTimeInterval:0.5];
        CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
        CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeAllEffects();
    });
}

The above is a newly added asynchronous execution code block, which is used to add the effect of delaying the call and ensure that it is executed before the program resumes. Avoid conflicts between program multitasking. NSThread sleepForTimeInterval:0.5A function is code that delays an operation.

So far, the problem of unresponsive audio has been solved.

Hope to bring you help! ! ! If you have any questions to discuss, you can comment and private message. Welcome to discuss~

Guess you like

Origin blog.csdn.net/weixin_44309889/article/details/130361809