1.音效播放
音效播放常见函数总结
加载音效文件
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
释放音效资源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效带点震动
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
示例代码:
导入#import <AVFoundation/AVFoundation.h>
// 1.获得音效文件的全路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil];
// 2.加载音效文件, 创建音效ID(SoundID)
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &_soundID);
// 3.播放
AudioServicesPlaySystemSound(_soundID);
2.音乐播放:
AVAudioPlayer的类, 能够用于播放本地音频文件
加载音乐文件
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (id)initWithData:(NSData *)data error:(NSError **)outError;
准备播放(缓冲,提高播放的流畅性)
- (BOOL)prepareToPlay;
播放(异步播放)
- (BOOL)play;
是否正在播放
@property(readonly, getter=isPlaying) BOOL playing;
时长
@property(readonly) NSTimeInterval duration;
当前的播放位置
@property NSTimeInterval currentTime;
播放次数(-1代表无限循环播放,其他代表播放numberOfLoops+1次)
@property NSInteger numberOfLoops;
音量
@property float volume;
是否允许更改速率
@property BOOL enableRate;
播放速率(1是正常速率,0.5是一般速率,2是双倍速率)
@property float rate;
有多少个声道
@property(readonly) NSUInteger numberOfChannels;
示例代码
// 0.音频文件的URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"235319.mp3"withExtension:nil];
// 1.创建播放器(一个AVAudioPlayer只能播放一个URL)
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// 2.缓冲
[player prepareToPlay];
self.player = player;
//播放
[self.player play];
//暂停
[self.player pause];
//stop之后player应该销毁,否则会出现一些意外的错误
[self.player stop];
文件格式转换:
转换aiff格式
afconvert -f AIFF -d I8 filename
转换caf格式
afconvert -f caff -d aac -b 32000 filename
批量转换
find . -name '*.mp3' -exec afconvert -f caff -d aac -b 32000 {} \;