Initialize music //Create music file path nsstring *musicfilepath = [[NSBundle mainbundle] pathforresource:@ "Eyeexe" oftype:@ "MP3"] ; Determine if the file exists if ([[[Nsfilemanager Defaultmanager] Fileexistsatpath:musicfilepath]) { Nsurl *musicurl = [ Nsurl Fileurlwithpath:musicfilepath]; Nserror *myerror = nil; Create player mybackmusic = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl error:&myerror]; if (Mybackmusic = = nil) { NSLog (@ "error = = =%@", [myerror description]); } [Mybackmusic setvolume:1]; Set Volume size mybackmusic.numberofloops = -1;//Set the number of music plays -1 for the Loop [Mybackmusic preparetoplay]; } Setting the lock screen will continue to play [[Avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayback Error:nil]; [[Avaudiosession sharedinstance] Setactive:yes Error:nil];
The corresponding methods are:
/* Methods This return BOOL return YES on success and NO on failure. */-(BOOL) preparetoplay;/* get ready-to-play the sound. happens automatically on play. */-(BOOL) play;/* sound is played asynchronously. */-(BOOL) Playattime: (nstimeinterval) Time ns_available (10_7, 4_0); /* Play a sound some time on the future. Time is a absolute time based on and greater than devicecurrenttime. */-(void) pause;/* pauses playback, but remains ready to play. */-(void) stop;/* stops playback. No longer ready to play. */
Related parameters:
/* Properties */@property (readonly, getter=isplaying) BOOL playing; /* It playing or not? */@property (readonly) Nsuinteger numberofchannels; @property (readonly) nstimeinterval duration; /* The duration of the sound. *//* the delegate would be sent messages from the Avaudioplayerdelegate protocol */@property (assign) Id<avaudioplayerde Legate> delegate; /* One of these properties would be non-nil based on the init ... method used */@property (readonly) Nsurl *url; /* Returns nil if object is not created with a URL */@property (readonly) NSData *data; /* Returns nil if object is not created with a data object */@property float pan ns_available (10_7, 4_0); /* Set panning. -1.0 is left, 0.0 are center, 1.0 is right. */@property float volume; /* The volume for the sound. The nominal range is from 0.0 to 1.0. */@property BOOL enablerate ns_available (10_8, 5_0); /* Must set Enablerate to YES for the effect. You must set this before calling Preparetoplay. */@properTy float rate ns_available (10_8, 5_0); /* See enablerate. The playback rate is the sound. 1.0 is normal, 0.5 are half speed, and 2.0 is a double speed. *//* If The sound is playing, currenttime are the offset into the sound of the current playback position. If The sound isn't playing, currenttime is the offset into the sound where playing would start. */@property Nstimeinterval currenttime;/* Returns the current time associated with the output device */@property (readonly) Nstimeinterval devicecurrenttime ns_available (10_7, 4_0);/* "Numberofloops" is the number of times that the sound would re Turn to the beginning upon reaching the end. A value of zero means to play the sound just once. A value of one would result in playing the sound twice, and so on. Any negative number would loop indefinitely until stopped.*/@property nsinteger numberofloops;/* Settings */@property (read only) Nsdictionary *settings ns_available (10_7, 4_0); /* Returns a settings dictionary with keys as described in AvaudiosettIngs.h *//* Metering * * @property (getter=ismeteringenabled) BOOL meteringenabled; /* turns level metering on or off. Default is OFF. */-(void) updatemeters; /* Call to Refresh meter values */-(float) Peakpowerforchannel: (Nsuinteger) channelnumber; /* Returns peak power in decibels for a given channel */-(float) Averagepowerforchannel: (Nsuinteger) channelnumber; /* Returns average power in decibels for a given channel */
Background Playback Audio Method:
1. Add a line to the plist file Required background modes
Add an items, set the properties to: App plays audio or streams audio/video using AirPlay
As shown below:
2. Add the following code:
Setting the lock screen will continue to play [[Avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayback Error:nil]; [[Avaudiosession sharedinstance] Setactive:yes Error:nil];
The avaudiosessioncategoryplayback is set to indicate that the user is switching to silent mode or the lock screen is ignored and the music continues to play. And don't play music from other apps, of course you can set Kaudiosessionproperty_overridecategorymixwithothers to mix music with other apps.
In addition to Avaudiosessioncategoryplayback, there are the following other category
NSString *const avaudiosessioncategoryambient; NSString *const avaudiosessioncategorysoloambient; NSString *const Avaudiosessioncategoryplayback; NSString *const Avaudiosessioncategoryrecord; NSString *const Avaudiosessioncategoryplayandrecord;
AvaudiosessioncategoryambientMusic is no longer played in silent mode or on the lock screen and mixes with other app sounds.
AvaudiosessioncategorysoloambientThe default mode, silent mode or lock screen no longer plays music, and does not mix with other app sounds.
AvaudiosessioncategoryrecordDo not play music, lock screen status continue recording
AvaudiosessioncategoryplayandrecordPlay music, and record
iOS Dev-Play local audio (can play back)