分享

iOS开发 录音以及播放 AVAudioRecorder

 玄冰优 2016-05-04

业务需求,需要一个类似于微信语音效果的功能,故整理成demo。
地址:https://github.com/Lian1990/RecordDemo
ps:加入了转码MP3的部分,只能真机运行。鸡肋了。。。  lame


AVAudioSession
AVAudioRecorder
AVAudioPlayer

// ====

AVAudioSession
比较懒,找了一个介绍非常详细的文章,大家学习一下。
http://www./industry/20140717/9162.html
在demo中执行使用的是
AVAudioSessionCategoryPlayAndRecord

AVAudioSessionCategoryPlayAndRecord
这个类别允许你的应用中同时进行声音的播放和录制。当你的声音录制或播放开始后,其他应用的声音播放将会停止。主UI界面会照常工作。这时,即使屏幕被锁定或者设备为静音模式,音频回放和录制都会继续。


// ====
AVAudioRecorder
 //设置录音格式
    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
                              [NSNumber numberWithFloat:44100],AVSampleRateKey,
                              [NSNumber numberWithInt:2],AVNumberOfChannelsKey,
                              [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                              [NSNumber numberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey,
                              [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                              [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                              nil];

########
/*
 1.AVNumberOfChannelsKey 通道数 通常为双声道 值2
 2.AVSampleRateKey 采样率 单位HZ 通常设置成44100 也就是44.1k
 3.AVLinearPCMBitDepthKey 比特率 8 16 24 32
 4.AVEncoderAudioQualityKey 声音质量
 ① AVAudioQualityMin  = 0, 最小的质量
 ② AVAudioQualityLow  = 0x20, 比较低的质量
 ③ AVAudioQualityMedium = 0x40, 中间的质量
 ④ AVAudioQualityHigh  = 0x60,高的质量
 ⑤ AVAudioQualityMax  = 0x7F 最好的质量
 5.AVEncoderBitRateKey 音频编码的比特率 单位Kbps 传输的速率 一般设置128000 也就是128kbps
 
 */

// ==== 用到的小的api 都进行一下总结
  NSTimer

五种初始化方法:

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化一个Invocation对象
    NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];
    [invo setTarget:self];
    [invo setSelector:@selector(myLog)];
    NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];
    //加入主循环池中
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
    //开始循环
    [timer fire];
}

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invo repeats:YES];

+(NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(myLog) userInfo:nil repeats:NO];

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:@"123" repeats:YES];

- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep 

NSTimer * timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

注意:这五种初始化方法的异同:

        1、参数repeats是指定是否循环执行,YES将循环,NO将只执行一次。

        2、timerWithTimeInterval这两个类方法创建出来的对象如果不用 addTimer: forMode方法手动加入主循环池中,将不会循环执行。并且如果不手动调用fair,则定时器不会启动。

        3、scheduledTimerWithTimeInterval这两个方法不需要手动调用fair,会自动执行,并且自动加入主循环池。

        4、init方法需要手动加入循环池,它会在设定的启动时间启动。


内存的释放

 [timer invalidate]  是唯一的方法将定时器从循环池中移除





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多