因为蓝牙项目遇到需要播放同人语言,但是界面pop的时候需要关闭所以用到。
用AudioServicesCreateSystemSoundID可以播放,但是界面退出他还是继续播放,所以需要用另外一种方式。
#import <AVFoundation/AVFoundation.h>
//播放器
@property (strong, nonatomic) AVPlayer *player;
//播放器初始化
NSString *audioPath = [[NSBundle mainBundle] pathForResource:voiceStr ofType:@"wav"];
NSURL *audioUrl = [NSURL fileURLWithPath:audioPath];
_player = [[AVPlayer alloc] initWithURL:audioUrl];
if (_player == NULL)
{
NSLog(@"fail to play audio :(");
return;
}
[_player setVolume:1];
[_player play];
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[_player pause];
}