Gavin.han

致力于移动开发 技术改变生活
随笔 - 133, 文章 - 0, 评论 - 46, 阅读 - 42万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

ios 音频录制

Posted on   gavin.han  阅读(9306)  评论(0编辑  收藏  举报

     新建Empty Applicaton,添加HomeViewController文件。还是看代码吧,将理论太枯燥,理论在代码中会提到。

 HomeViewController.h代码:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
 
@interface HomeViewController : UIViewController
<AVAudioRecorderDelegate, AVAudioPlayerDelegate>{
 
}
 
@property (nonatomic, retain) AVAudioRecorder *audioRecorder;
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
 
- (NSString *)audioRecordingPath;
- (NSDictionary *)audioRecordingSettings;
 

@end 

 HomeViewController.m代码:

 
#import "HomeViewController.h"
 
@interface HomeViewController ()
 
@end
 
@implementation HomeViewController
 
@synthesize audioPlayer;
@synthesize audioRecorder;
 
//设置录制的音频文件的位置
- (NSString *)audioRecordingPath{
 
    NSString *result = nil;
    NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsFolde = [folders objectAtIndex:0];
    result = [documentsFolde stringByAppendingPathComponent:@"Recording.m4a"];
    return (result);
}
 
//在初始化AVAudioRecord实例之前,需要进行基本的录音设置
- (NSDictionary *)audioRecordingSettings{
 
    NSDictionary *result = nil;
    
    NSMutableDictionary *settings = [[[NSMutableDictionary alloc] init] autorelease];//录音时所必需的参数设置
    
    [settings setValue:[NSNumber numberWithInteger:kAudioFormatAppleLossless] 
                forKey:AVFormatIDKey];
    
    [settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];
    
    [settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey];
    
    [settings setValue:[NSNumber numberWithInteger:AVAudioQualityLow] 
                forKey:AVEncoderAudioQualityKey];
    
    result = [NSDictionary dictionaryWithDictionary:settings];
    
    return (result);
}
 
//停止音频的录制
- (void)stopRecordingOnAudioRecorder:(AVAudioRecorder *)recorder{
 
    [recorder stop];
}
 
//当AVAudioRecorder对象录音终止的时候会调用audioRecorderDidFinishRecording:successfully:方法
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
 
    //如果flag为真,代表录音正常结束,使用AVAudioPlayer将其播放出来,否则日志记录失败原因
    if (flag == YES) {
        NSLog(@"录音完成!");
        NSError *playbackError = nil;
        NSError *readingError = nil;
        NSData *fileData = [NSData dataWithContentsOfFile:[self audioRecordingPath] options:NSDataReadingMapped error:&readingError];
        
        AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithData:fileData 
                                                                 error:&playbackError];
        
        self.audioPlayer = newPlayer;
        [newPlayer release];
        
        if (self.audioPlayer != nil) {
            self.audioPlayer.delegate = self;
            if ([self.audioPlayer prepareToPlay] == YES && 
                [self.audioPlayer play] == YES) {
                NSLog(@"开始播放录制的音频!");
            } else {
                NSLog(@"不能播放录制的音频!");
            }
        }else {
            NSLog(@"音频播放失败!");
        }
        
    } else {
        NSLog(@"录音过程意外终止!");
    }
    self.audioRecorder = nil;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSError *error = nil;
    NSString *pathOfRecordingFile = [self audioRecordingPath];
    
    NSURL *audioRecordingUrl = [NSURL fileURLWithPath:pathOfRecordingFile];
    
    AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc] 
                                    initWithURL:audioRecordingUrl 
                                    settings:[self audioRecordingSettings]
                                    error:&error];
    self.audioRecorder = newRecorder;
    [newRecorder release];
    
    if (self.audioRecorder != nil) {
        self.audioRecorder.delegate = self;
        
        if ([self.audioRecorder prepareToRecord] == YES &&
            [self.audioRecorder record] == YES) {
            //如果audioRecorder实例化成功,则开始录制声音,并且通过performSelector方法设置在录制声音10s以后执行stopRecordingOnAudioRecorder方法,用于停止录音
            NSLog(@"录制声音开始!");
            
            [self performSelector:@selector(stopRecordingOnAudioRecorder:)
                       withObject:self.audioRecorder
                       afterDelay:10.0f];
            
        } else {
            NSLog(@"录制失败!");
            self.audioRecorder =nil;
        }
} else {
    NSLog(@"auioRecorder实例创建失败!");
}
}
 
- (void)viewDidUnLoad{
 
    if (self.audioRecorder != nil) {
        if ([self.audioRecorder isRecording] == YES) {
            [self.audioRecorder stop];
        }
        self.audioRecorder = nil;
    }
    
    if (self.audioPlayer != nil) {
        if ([self.audioPlayer isPlaying] == YES) {
            [self.audioPlayer stop];
        }
        self.audioPlayer = nil;
    }
}
 
- (void)dealloc{
 
    [audioPlayer release];
    [audioRecorder release];
    [super dealloc];
}
 
@end
 

 PS: 录制音频时所必需的参数设置 

 AVFormatIDKey  录制音频的格式。

kAudioFormatLinearPCM: lpcm格式 

kAudioFormatAC3: ac-3格式

kAudioFormatMPEG4AAC: aac格式

kAudioFormatMPEG4CELP: celp格式 

kAudioFormatMPEG4HVXC: hvxc格式 

kAudioFormatMPEG4Layer1: mp1格式 

kAudioFormatMPEG4Layer2: mp2 格式 

kAudioFormatMPEG4Layer3: mp3 格式 

kAudioFormatTimeCode: time格式 

kAudioFormatMIDIStream: midi格式 

kAudioFormatAppleLossless:alac格式  

 

AVSampleRateKey 录制音频时的采用视频

AVNumberOfChannelsKey  录制音频时的通道数量

AVEncoderAudioQualityKey  录制音频的质量 

AVAudioQualityMin 

AVAudioQualityLow

AVAudioQualityMedium

AVAudioQualityHigh

AVAudioQualityMax

 

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示