iOS系列_音频
2018-07-07 本文已影响0人
王二的自留地
简介
用于音频录音或播放最常用的两个frameworks分别是AVFoundation和AudioToolbox。两者提供的API都可以对音频操作,两者使用的场景也不一样。除此之外,两者在架构上有啥区别呢?直到在stackoverflow上找到了下图。

AVFoundation
先上图,AVFoundation Stack on iOS, 来自官方文档。

AVFoundation提供了我们2个类,AVAudioPlayer用于播放音频文件,AVAudioRecorder用于录制音频。
AVAudioPlayer
首先来看AVAudioPlayer, 可以划分成3部分
1.delegate 相关
audioPlayerDidFinishPlaying:successfully:
audioPlayerDecodeErrorDidOccur:error:
2.控制播放暂停等操作
prepareToPlay、play、pause、stop
3.一些配置
volume、rate、currentTime
AVAudioSession
看AVAudioRecorder的使用之前,有必要先了解下AVAudioSession,app通过该AVAudioSession来跟系统打交道,操作音频。上一张来自官方的一张图

通过AVAudioSession可以根据要求配置采样率、音频类型。
AVAudioRecorder
AVAudioRecorder也是分3部分
1.delegate 相关
audioRecorderDidFinishRecording
audioRecorderEndInterruption
2.控制录音的一些操作
prepareToRecord、record、pause、stop
3.通过AVAudiosession进行一些配置
参考
- [https://stackoverflow.com/questions/1877410/whats-the-difference-between-all-these-audio-frameworks/24599187#24599187?newreg=00e6a5ddf86e4848918810980294d23d]
- (https://stackoverflow.com/questions/1877410/whats-the-difference-between-all-these-audio-frameworks/24599187#24599187?newreg=00e6a5ddf86e4848918810980294d23d)
- https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188
- https://developer.apple.com/documentation/avfoundation/avaudioplayer?language=objc