Avaudiorecorder performs recording, and the background music is relentlessly stopped. The solution is as follows:
First, set [[avaudiosession sharedinstance] setactive: Yes error: Nil] before starting the recording.
[[Avaudiosession sharedinstance] setcategory: avaudiosessioncategoryplayandrecord error: Nil];
After the recording is completed, it is in its callback function.
-(Void) audiorecorderdidfinishrecording :( avaudiorecorder *) arecorder successfully :( bool) Flag
Add: [[avaudiosession sharedinstance] setactive: no error: Nil];
You can.
(Using avaudioplayer to play music is also handled)
The active attribute of avaudiosession is to set the startup and shutdown of your audio session. After your recording ends, you must disable avaudiosession to enable the background music on your own. (Many examples on the Internet only start it, but it is not closed. As a result, avaudiosession is always active and the normal playing of background music is blocked) the category attribute of avaudiosession is to set its category. It has the following categories:
Get input hardware get output hardware and iPod mix
Follow the ringtone/mute
Avaudiosessioncategoryambient No Yes
Yes
Avaudiosessioncategorysoloambient No Yes No
Yes
Avaudiosessioncategoryplayback No Yes No
No
Avaudiosessioncategoryrecord: No
No
Avaudiosessioncategoryplayandrecord Yes No
No
If you want your music to continue playing in the background after the program exits,
You can set it as follows:
[[Avaudiosession sharedinstance] setcategory: avaudiosessioncategoryplayandrecord error: Nil];
Add it to the plist of your project
Add in info. plist
<Key> required background modes </key>
<Array>
<String> app plays audio </string>
</Array>
You can.