AVAudioPlayer是AVFoundation框架中播放音频的一个类
每个AVAudioPlayer对应一个音频,主要创建方法为
- (instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (instancetype)initWithData:(NSData *)data error:(NSError **)outError;
推荐格式:
- 对于非压缩音频,caf格式(16-bit, little endian,linear PCM)
-对于压缩音频,且一次播放一个音频,使用caf,或者m4a格式
- 若同时播放多个声音,考虑降低内存,可使用IMA4压缩,使用caf格式
可使用mac afconvert 命令转换格式
1. cd 到需要转换的文件目录下
2. 命令格式为 afconvert [option...] input_file [output_file],option的形式是
3. 比如afconvert -f caff -d LEI16 {INPUT} {OUTPUT} 转换为use 16-bit, little endian, linear PCM caf文件
AVAudioPlayer主要的播放方法:
@property NSTimeInterval currentTime; - (BOOL)play; /* sound is played asynchronously. */ - (BOOL)playAtTime:(NSTimeInterval)time NS_AVAILABLE(10_7, 4_0); /* play a sound some time in the future. time is an absolute time based on and greater than deviceCurrentTime. */ - (void)pause; /* pauses playback, but remains ready to play. */ - (void)stop; /* stops playback. no longer ready to play. */