1.
URLWithString:
构建网络路径
2.
fileURLWithPath:
构建本地路径
3.
由于当前
audioPlayer
没有被当前的控制器所持有,所以,为避免提前释放,需要将
audioPlayer
设置为实例变量
AVAudioPlayer
*
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
4.
准备播放,分配播放所需要的资源,并将资源添加到播放队列中
[audioPlayer prepareToPlay];
5.
使用
audioPlayer
进行停止功能,应该先暂停,再停止
6.
播放系统声音
(1)拿到系统声音文件地址
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"44th Street Medium" ofType : @"caf" ];
(2) 构建 URL
NSURL *url = [ NSURL fileURLWithPath :filePath];
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"44th Street Medium" ofType : @"caf" ];
(2) 构建 URL
NSURL *url = [ NSURL fileURLWithPath :filePath];
(3)
SystemSoundID
soundID;
(4)
将声音文件注册成为系统声音
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
7.
播放系统声音(只能在真机上看到效果)
AudioServicesPlaySystemSound(soundID);
8.真机震动 (只能在真机上看到效果)
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);