First, open the background running audio in the configuration file, such as.
The next step in the code is to add
Set Background playback
Avaudiosession *audiosession = [avaudiosession sharedinstance];
[Audiosession setcategory:avaudiosessioncategoryplayback error:nil];
[Audiosession setActive:YES error:nil];
Adding remote control Events
[[uiapplication sharedapplication] beginreceivingremotecontrolevents];
play the recording, TempStr is the audio file name added in the project
Nserror *error;
nsurl *tempurl = [[nsurl alloc] initfileurlwithpath: [[nsbundle Mainbundle]pathforresource: TempStr ofType:@ "MP3"];
Create a global player object player, note that it must be a global object, initialized here.
_player = [[avaudioplayer alloc] initwithcontentsofurl: Tempurl
error:&err];
empty URL
Tempurl = nil;
// set the player properties
_player. Volume = 0.8;
_player. CurrentTime = 0.0;
_player. Delegate = self;
[_player preparetoplay];
[_player play];
Finally, remove the remote control event in the Dealloc
[[UIApplication sharedapplication] endreceivingremotecontrolevents];
iOS running audio files in the background