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);
本文介绍了在iOS中使用AVAudioPlayer进行音频文件的播放控制,包括构建播放路径、准备播放资源、实现暂停与停止功能等。此外还详细说明了如何播放系统预设的声音及实现设备震动效果。

被折叠的 条评论
为什么被折叠?



