When the application is suspended rather than permanently exiting, all the validity of the application will be suspended. In this case, if you need to play audio or video, you need to activate the play "single routine ", to ensure that the playing module is not suspended. Enable the audio or video to continue playing in the background without terminating the program due to suspension. You only need to add the following code in the corresponding position of the program:
NSError * myErr;
BOOL bAudioInputAvailable = FALSE;
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
BAudioInputAvailable = [audioSession inputIsAvailable];
If (bAudioInputAvailable)
{
[AudioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: & myErr];
}
Else
{
[AudioSession setCategory: AVAudioSessionCategoryPlayback error: & myErr];
}
Be sure to execute the above Code in the playback code module or when the playback starts. Otherwise, the code is invalid.
In this way, even the screen lock does not affect the playing of music.
From cloud huaikong-abel