iOS之音频录制及播放
https://www.jianshu.com/p/fb7dfb033989
https://blog.csdn.net/u014084081/article/details/89027144
https://blog.csdn.net/crazy_sunshine/article/details/93715973
1)Demo环境:
Swift4.2,
Xcode10.1
2)Demo地址:
https://github.com/cxymq/RecordForChat
3)AVAudioSession学习记录
https://blog.csdn.net/Crazy_SunShine/article/details/80104838
步骤如下:
1.需要申请麦克风权限
2.调用 AVFoundation 的API
主要用到 AVAudioRecorder(录音) 、AVAudioPlayer(播放)、AVAudioSession(设置音频硬件设备)
录音开始前,先检测权限,如果允许,则设置音频硬件设备:
audioS.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
audioS.setActive(true, options: AVAudioSession.SetActiveOptions.init(rawValue: 0))
设置存放路径:
-
letpath: String= (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?? nil)!
-
-
let order = voiceList.count as Int
-
-
let namePath = "/Record\(String(describing: order)).wav"
-
-
let voiceFilePath = path.appending(namePath)
进行录音设置:
(详情见附录1)
-
letrecordSetting = [
-
-
AVFormatIDKey: kAudioFormatLinearPCM,
-
-
AVSampleRateKey: 8000.0,
-
-
AVNumberOfChannelsKey: 1,
-
-
AVLinearPCMBitDepthKey: 16,
-
-
AVLinearPCMIsNonInterleaved: false,
-
-
AVLinearPCMIsFloatKey: false,
-
-
AVLinearPCMIsBigEndianKey: false
-
-
] as [String : Any]
然后调用 record() 开始录音。
播放录音,只需要提供音频路径,调用 play() 即可。
附录:
1.录音设置标注(参考:https://www.jianshu.com/p/692304a8fc95)
AVFormatIDKey
录音的文件格式
CF_ENUM(AudioFormatID)
{
kAudioFormatLinearPCM = 'lpcm',
kAudioFormatAC3 = 'ac-3',
kAudioFormat60958AC3 = 'cac3',
kAudioFormatAppleIMA4 = 'ima4',
kAudioFormatMPEG4AAC = 'aac ',
kAudioFormatMPEG4CELP = 'celp',
kAudioFormatMPEG4HVXC = 'hvxc',
kAudioFormatMPEG4TwinVQ = 'twvq',
kAudioFormatMACE3 = 'MAC3',
kAudioFormatMACE6 = 'MAC6',
kAudioFormatULaw = 'ulaw',
kAudioFormatALaw = 'alaw',
kAudioFormatQDesign = 'QDMC',
kAudioFormatQDesign2 = 'QDM2',
kAudioFormatQUALCOMM = 'Qclp',
kAudioFormatMPEGLayer1 = '.mp1',
kAudioFormatMPEGLayer2 = '.mp2',
kAudioFormatMPEGLayer3 = '.mp3',
kAudioFormatTimeCode = 'time',
kAudioFormatMIDIStream = 'midi',
kAudioFormatParameterValueStream = 'apvs',
kAudioFormatAppleLossless = 'alac',
kAudioFormatMPEG4AAC_HE = 'aach',
kAudioFormatMPEG4AAC_LD = 'aacl',
kAudioFormatMPEG4AAC_ELD = 'aace',
kAudioFormatMPEG4AAC_ELD_SBR = 'aacf',
kAudioFormatMPEG4AAC_ELD_V2 = 'aacg',
kAudioFormatMPEG4AAC_HE_V2 = 'aacp',
kAudioFormatMPEG4AAC_Spatial = 'aacs',
kAudioFormatAMR = 'samr',
kAudioFormatAMR_WB = 'sawb',
kAudioFormatAudible = 'AUDB',
kAudioFormatiLBC = 'ilbc',
kAudioFormatDVIIntelIMA = 0x6D730011,
kAudioFormatMicrosoftGSM = 0x6D730031,
kAudioFormatAES3 = 'aes3',
kAudioFormatEnhancedAC3 = 'ec-3',
kAudioFormatFLAC = 'flac',
kAudioFormatOpus = 'opus'
};
-
kAudioFormatLinearPCM: 音频的原始文件(caf)
-
kAudioFormatMPEG4AAC: aac文件格式的音频
注意
文件格式不可以直接.mp3格式,会报错
AVSampleRateKey
采用率(赫兹)``
常用的有
-
8,000 Hz: 电话所用采样率, 对于人的说话已经足够
-
11,025 Hz: 说话
参考文章:
https://baike.baidu.com/item/%E9%9F%B3%E9%A2%91%E9%87%87%E6%A0%B7%E7%8E%87/9023551
AVNumberOfChannelsKey
采用通道数,一般有:
-
1: 单声道
-
2: 双声道
linear PCM keys
AVLinearPCMBitDepthKey
采样深度,影响声音质量,比如从caf转mp3,如果设置为8的话,声音可能会失真,需要设置更高一点(比如16)
取值有: 8, 16, 24, 32
AVLinearPCMIsBigEndianKey
-
YES: 大端模式
-
NO: 小端模式
参考文章:
https://blog.csdn.net/chivalrousli/article/details/38419995
AVLinearPCMIsFloatKey
是否支持浮点处理
AVLinearPCMIsNonInterleaved
交叉的
audio file type key11.0 之后
AVAudioFileTypeKey
AudioFile.h文件中可以查看具体的
encoder property keys
编码属性
AVEncoderAudioQualityKey
编码质量
取值有:
typedef NS_ENUM(NSInteger, AVAudioQuality) {
AVAudioQualityMin = 0,
AVAudioQualityLow = 0x20,
AVAudioQualityMedium = 0x40,
AVAudioQualityHigh = 0x60,
AVAudioQualityMax = 0x7F
};
AVEncoderAudioQualityForVBRKey
动态比特率,取值是 AVAudioQuality,只和AVAudioBitRateStrategy_Variable有关
AVEncoderBitRateKey
编码比特率
注意
AVEncoderBitRateKey和AVEncoderBitRatePerChannelKey只需要设置一个 ;only one of AVEncoderBitRateKey and AVEncoderBitRatePerChannelKey should be provided.
AVEncoderBitRatePerChannelKey
每个声道的比特率
AVEncoderBitRateStrategyKey
编码比特率的策略
取值有:
/* values for AVEncoderBitRateStrategyKey */
AVF_EXPORT NSString *const AVAudioBitRateStrategy_Constant NS_AVAILABLE(10_9, 7_0);
AVF_EXPORT NSString *const AVAudioBitRateStrategy_LongTermAverage NS_AVAILABLE(10_9, 7_0);
AVF_EXPORT NSString *const AVAudioBitRateStrategy_VariableConstrained NS_AVAILABLE(10_9, 7_0);
AVF_EXPORT NSString *const AVAudioBitRateStrategy_Variable NS_AVAILABLE(10_9, 7_0);
AVEncoderBitDepthHintKey
取值 8 to 32
-----------------------------------------------------------
个人博客:https://blog.csdn.net/Crazy_SunShine
Github:https://github.com/cxymq
个人公众号:Flutter小同学
个人网站:http://chenhui.today/
---------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2015-02-19 iOS博客推荐