iOS -录音-音频的拼接剪切以及边录边压缩转码

本文介绍了在iOS中实现录音、音频编辑(包括拼接和剪切)以及使用LAME静态库进行音频转码的详细步骤。通过AVFoundation框架,展示了AVAudioRecorder的录音功能,以及如何使用AVMutableComposition进行音频拼接和剪切。同时,文章讲解了LAME库的下载、静态库生成及在iOS项目中的应用,用于将CAF格式的音频转换为MP3。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总体内容
1、录音实现

2、录音的编辑 (拼接音频:可以设置多段,音频的剪切:按照时间段剪切)

3、lame静态库进行压缩转码

一、录音实现
1.1、导入 AVFoundation 框架,多媒体的处理, 基本上都使用这个框架

#import <AVFoundation/AVFoundation.h>

1.2、使用 AVAudioRecorder 进行录音,定义一个JKAudioTool 管理录音的类

(1)、定义一个录音对象,懒加载

@property (nonatomic, strong) AVAudioRecorder *audioRecorder;

-(AVAudioRecorder *)audioRecorder
{
if (!_audioRecorder) {

   // 0. 设置录音会话
   /**
     AVAudioSessionCategoryPlayAndRecord: 可以边播放边录音(也就是平时看到的背景音乐)
    */
   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
   // 启动会话
   [[AVAudioSession sharedInstance] setActive:YES error:nil];

   // 1. 确定录音存放的位置
   NSURL *url = [NSURL URLWithString:self.recordPath];

   // 2. 设置录音参数
   NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
   // 设置编码格式
   /**
     kAudioFormatLinearPCM: 无损压缩,内容非常大
     kAudioFormatMPEG4AAC
   */
   [recordSettings setValue :[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
   // 采样率(通过测试的数据,根据公司的要求可以再去调整),必须保证和转码设置的相同
   [recordSettings setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];
   // 通道数(必须设置为双声道, 不然转码生成的 MP3 会声音尖锐变声.)
   [recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];

   //音频质量,采样质量(音频质量越高,文件的大小也就越大)
   [recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];

   // 3. 创建录音对象
   _audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:nil];
   _audioRecorder.meteringEnabled = YES;
 }

return _audioRecorder;
}

提示:设置 AVAudioSessionCategoryPlayAndRecord: 可以边播放边录音(也就是平时看到的背景音乐)

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

AVSampleRateKey 必须保证和转码设置的相同.

AVNumberOfChannelsKey 必须设置为双声道, 不然转码生成的 MP3 会声音尖锐变声.

(2)、开始录音

  • (void)beginRecordWithRecordPath: (NSString *)recordPath {
    // 记录录音地址
    _recordPath = recordPath;
    // 准备录音
    [self.audioRecorder prepareToRecord];
    // 开始录音
    [self.audioRecorder record];
    }

(3)、结束录音

  • (void)endRecord {
    [self.audioRecorder stop];
    }

(4)、暂停录音

  • (void)pauseRecord {
    [self.audioRecorder pause];
    }

(5)、删除录音

  • (void)deleteRecord {
    [self.audioRecorder
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值