1. Background music play cycle play long music support MP3 format
#import <AVFoundation/AVFoundation.h>;
1NSString *musicfilepath = [[NSBundle mainbundle] Pathforresource:@"Changan"OfType:@"mp3"];//Create a music file path2Nsurl *musicurl =[[Nsurl alloc] initfileurlwithpath:musicfilepath];3 4Avaudioplayer *theplayer =[[Avaudioplayer alloc] Initwithcontentsofurl:musicurl Error:nil];5 6 //Create player7Self.mybackmusic = Theplayer;//assigning to a class variable of its own definition8 9 [Musicurl release];Ten [Theplayer release]; One A [Mybackmusic Preparetoplay]; -[Mybackmusic SetVolume:1];//Set Volume size -Mybackmusic.numberofloops =-1;//set the number of music plays-1 to keep looping the[Mybackmusic play];//Play
2. Sound effects play short sound
#import <AudioToolbox/AudioToolbox.h>
NSString *thesoundfilepath = [[NSBundle mainbundle] Pathforresource:@"clapping Crowd Studio"OfType:@"CAF"];//Create a music file pathCfurlref Thesoundurl =(cfurlref) [Nsurl Fileurlwithpath:thesoundfilepath]; Audioservicescreatesystemsoundid (Thesoundurl,&sameviewsoundid);//variable soundid corresponds to URLAudioservicesplaysystemsound (sameviewsoundid);//Play Soundid Sound