音频文件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];