• 录音,


    1,初始化 AVAudioSession

     //7.0第一次运行会提示,是否允许使用麦克风

            AVAudioSession *session = [AVAudioSessionsharedInstance];

            NSError *sessionError;

            //AVAudioSessionCategoryPlayAndRecord用于录音和播放

            [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

            if(session == nil)

                NSLog(@"Error creating session: %@", [sessionError description]);

            else

                [session setActive:YES error:nil];

    2,生成目录

        NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        playName = [NSString stringWithFormat:@"%@/play.aac",docDir];

    3,录制

       recorder = [[AVAudioRecorderalloc] initWithURL:[NSURLURLWithString:playName] settings:recorderSettingsDicterror:&error];

            

            if (recorder) {

                recorder.meteringEnabled = YES;

                [recorder prepareToRecord];

                [recorder record];

    4,播放

      player = [[AVAudioPlayeralloc] initWithContentsOfURL:[NSURLURLWithString:playName] error:&playerError];

        

        if (player == nil)

        {

            NSLog(@"ERror creating player: %@", [playerError description]);

        }else{

            [player play];

        }

    ps:好像AVAudioSession 与AVAudioRecorder, AVAudioPlayer没有什么关系,代码之间看不到,

    a,设置:

    //录音设置
    NSMutableDictionary *recordSetting = [[[NSMutableDictionary alloc]init] autorelease];
    //设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
    //录音通道数 1 或 2
    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
    //线性采样位数 8、16、24、32
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    //录音的质量
    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
    //要找的文件路径
    NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //创建存储的文件夹
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/lll.aac", strUrl]];
    urlPlay = url;//获得播放路径
    NSLog(@"录音的路径为:%@",strUrl);
    NSLog(@"录音====路径:%@",url);
    NSError *error;
    //初始化
    recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];

    //开启音量检测
    recorder.meteringEnabled = YES;
    recorder.delegate = self;

    b,启动,时间限制,

    - (IBAction)btnDown:(id)sender
    {

    //创建录音文件,准备录音
    if ([recorder prepareToRecord]) {
    //开始
    [recorder record];
    }

    //设置定时检测
    timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];

    }

    - (IBAction)btnUp:(id)sender
    {
    double cTime = recorder.currentTime;
    if (cTime > 2) {//如果录制时间<2 不发送

    [mainRecorder recordForDuration:60.0];//设置录音时限60秒,该方法自动执行prepareToRecord


    NSLog(@"发出去");
    }else {
    //删除记录的文件
    [recorder deleteRecording];
    //删除存储的
    }
    [recorder stop];
    [timer invalidate];//移除计时器
    }
    - (IBAction)btnDragUp:(id)sender
    {
    //删除录制文件
    [recorder deleteRecording];
    [recorder stop];
    [timer invalidate];

    NSLog(@"取消发送");
    }

    
    
  • 相关阅读:
    管理页面的类 PageHelper
    接下来打算写一下visual stuido 2013使用git进行远端管理。
    转一下网上找来的tortoise git不用每次都输入邮箱和密码的方法。备查看
    tortoise git使用 git版本库的rsa key来进行ssh连接
    2015年4月1日 14:36:56 EF 主从表更新
    Oop分析方法
    Knative 实战:基于 Knative Serverless 技术实现天气服务-下篇
    超大规模商用 K8s 场景下,阿里巴巴如何动态解决容器资源的按需分配问题?
    从零开始入门 K8s | 可观测性:你的应用健康吗?
    Knative 暂时不会捐给任何基金会 | 云原生生态周报 Vol. 22
  • 原文地址:https://www.cnblogs.com/guligei/p/3517894.html
Copyright © 2020-2023  润新知