Playing music in iOS background and playing music in ios background
In the info. plist file, add
The complete test code for playing audio in the background is as follows:
Import file <AVFoundation/AVFoundation. h>
// Set the background playing audio
AVAudioSession * session = [AVAudioSession sharedInstance];
[Session setActive: YES error: nil];
[Session setCategory: AVAudioSessionCategoryPlayback error: nil];
// Allow the app to support remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Play background music
NSString * musicPath = [[NSBundle mainBundle] pathForResource: @ "background" ofType: @ "mp3"];
NSURL * url = [[NSURL alloc] initFileURLWithPath: musicPath];
// Create a player
Player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil];
[Player prepareToPlay];
[Player setVolume: 1];
Player. numberOfLoops =-1; // set the number of times the music is played-1 to keep repeating
[Player play]; // play