Step One: Locate the item in the Resource folder Info.plist add a
Array of Required background modes
and add an element below, whose value is
APP plays Audio
Step Two:
Need to import the appropriate framework and header files
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
Step Three:
Disable automatic lock screen when program is running
[[Uiapplicationsharedapplication]setidletimerdisabled:yes];
Step four:
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:@ ' Hatsune ミク-world is Mine ' oftype:@ "MP3"]; Create a music file path
Nsurl * musicurl= [[Nsurlalloc]initfileurlwithpath:musicfilepath];
Avaudioplayer * Theplayer = [[Avaudioplayeralloc]initwithcontentsofurl:musicurlerror:nil];
[Theplayerplay];
[Theplayersetvolume:1]; Set the volume size
theplayer.numberofloops = -1;//Set music playback times 1 to loop
[musicurlrelease];
[Theplayerrelease];