iOS 实现录音并保存在指定文件目录下面

本文介绍了在iOS中如何实现实现录音并将其保存到指定文件目录下。首先,通过遍历文件目录并在UITableView中显示所有文件名。录音时涉及设置session和采样率。在录音前检查并创建存放目录,然后开始录音。最后,列举了录音文件并展示在列表中。然而,目前存在播放录音使用听筒声音和未转换为通用音频格式的问题。

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

原理:
进入界面,先遍历文件目录,将所有的文件名,显示在uitableview中。在录音时需要设置session以及录音采样率。

1.ios录音主要使用ios自带的类,是工程中需要手动添加这俩个framework

#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>

2.在录音前,先监测文件存放目录是否存在,不存在就创建目录

NSDate *  date=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    NSString *datefloder= [dateformatter stringFromDate:date];
    dateaudioPath=[NSString stringWithFormat:@"%@/",datefloder];
    fileMgr = [NSFileManager defaultManager];
    //指向文件目录
    NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    audioRecoderSavePath=[NSString stringWithFormat:@"%@/%@%@", documentsDirectory,audioPath,dateaudioPath];

    if (![fileMgr fileExistsAtPath:audioRecoderSavePath]) {
        [fileMgr createDirectoryAtPath:audioRecoderSavePath withIntermediateDirectories:YES attributes:nil error:nil];
    }

3.点击录音,开始录音

          if(!isRecording)
            {
                isRecording = YES;
                stateLabel.text=@"录音中";
                [startRecoderBtn setTitle:@"停止录音" forState:(UIControlStateNormal)];
               // startRecoderBtn.titleLabel.text=@"停止录音";
                NSDate *  date=[NSDate date];
                NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
                [dateformatter setDateFormat:@"YYYYMMddHHYYSS"];
                recoderName= [NSString stringWithFormat:@"%@%@",[dateformatter stringFromDate:date],@".caf"];
                //tempRecoderPath=[NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"%@%@",audioPath,recoderName]];
                tempRecoderPath=[NSString stringWithFormat:@"%@%@",audioRecoderSavePath,recoderName];
                tempRecordedFile = [NSURL fileURLWithPath:tempRecoderPath];
                recorder = [[AVAudioRecorder alloc] initWithURL:tempRecordedFile settings:[self getAudioSetting] error:nil];
                recorder.delegate=self;
                [recorder prepareToRecord];
                [recorder record];
                avplayer = nil;
            }
            //If the app is recording, we want to stop recording, enable the play button, and make the record button say "REC"
            else
            {
                isRecording = NO;
                stateLabel.text=[NSString stringWithFormat:@"%@%@",@"录音完成",recoderName];
                //startRecoderBtn.titleLabel.text=@"开始录音";
                 [startRecoderBtn setTitle:@"开始录音" forState:(UIControlStateNormal)];
                [recorder stop];
                recorder = nil;

            }

4.在列表展示文件目录中,遍历所有的录音文件

-(NSMutableArray *)getFilenamelistOfType:(NSString *)type fromDirPath:(NSString *)dirPath
{
    NSMutableArray *filenamelist = [[NSMutableArray alloc]init];
    NSArray *tmplist = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];

    for (NSString *filename in tmplist) {
        NSString *fullpath = [dirPath stringByAppendingPathComponent:filename];
        if ([self isFileExistAtPath:fullpath]) {
            if ([[filename pathExtension] isEqualToString:type]) {
                AudioObject *ob=[[AudioObject alloc]init];
                ob.audioRecoderName=filename;
                ob.audioRecoderPath=fullpath;
                ob.audioRecoderIsChecked=NO;
                [filenamelist addObject:ob];
            }
        }
    }

    return filenamelist;
}

-(BOOL)isFileExistAtPath:(NSString*)fileFullPath {
    BOOL isExist = NO;
    isExist = [[NSFileManager defaultManager] fileExistsAtPath:fileFullPath];
    return isExist;
}

完整代码
头文件

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值