Here's an example of a local resource.
Play long music:
#import <AVFoundation/AVFoundation.h> //url: Network resource path, you can also convert local resources into network resource path //Get a resource locally, and convert to URL nsurl * URL = [[NSBundle mainbundle]urlforresource:@ "background music" withextension:@ "CAF"]; Avaudioplayer *player = [[Avaudioplayer alloc]initwithcontentsofurl:url Error:nil]; _player = player; You need to define a property to point to him, otherwise the method ends up being destroyed, the music stops //Specifies the number of cycles to play, 0 for once //or any negative numbers for infinite playback [player setnumberofloops:-1 ]; Ready to play [player Preparetoplay]; Play, launch a thread to play [player play];
Play a short tone, such as responding to a sound when the button is pressed:
nsurl* URL = [[NSBundle mainbundle]urlforresource:@] sound. * "Withextension:nil"; Create a soundid systemsoundid soundid; Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &soundid); Play Audioservicesplaysystemsound (Soundid);
[IOS:OC] Simple audio playback