音頻文件sound.m4a放到Supporting Files目錄
引用頭文件
#import <AVFoundation/AVFoundation.h>
定義一個全局的屬性:
@property (nonatomic, strong) AVAudioPlayer *player; //音頻播放器
//初始化音頻播放器
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"m4a"];
self.player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:path] error:nil];
self.player.numberOfLoops = 1;
self.player.volume = 1.0;
//播放
[self.player play];