Step 1: find info. plist of the project in the resource folder and add
Array of Required background modes
And add an element below, whose value is
App plays audio
Step 2:
You need to import the corresponding framework and header files.
# Import <AudioToolbox/AudioToolbox. h>
# Import <AVFoundation/AVFoundation. h>
Step 3:
// Disable automatic screen lock when the program is running
[[UIApplicationsharedApplication] setIdleTimerDisabled: YES];
Step 4:
AVAudioSession * audioSession = [AVAudioSessionsharedInstance];
NSError * err = nil;
[AudioSession setCategory: AVAudioSessionCategoryPlayAndRecorderror: & err];
If (err)
{
NSLog (@ "audioSession: % @ % d % @", [errdomain], [errcode], [[erruserInfo] description]);
}
[AudioSessionsetActive: YESerror: & err];
Err = nil;
If (err)
{
NSLog (@ "audioSession: % @ % d % @", [errdomain], [errcode], [[erruserInfo] description]);
}
NSString * musicFilePath = [[NSBundlemainBundle] pathForResource: @ "ク-World Is Mine" ofType: @ "mp3"]; // create a music file path
NSURL * musicURL = [[NSURLalloc] initFileURLWithPath: musicFilePath];
AVAudioPlayer * thePlayer = [[AVAudioPlayeralloc] initWithContentsOfURL: musicURLerror: nil];
[ThePlayerplay];
[ThePlayersetVolume: 1]; // sets the volume.
ThePlayer. numberOfLoops =-1; // set the number of times the music is played-1 to keep repeating
[MusicURLrelease];
[ThePlayerrelease];