ios 录音,播放 tips

  • 背景
  • 最近在做iOS录音相关东西,也遇到了一些坑,顺便记录下。
  • 正文
  • 系统自带的AVFoundation框架,提供了AVAudioRecorder(录音),AVAudioPlayer(播放)两个最简单易用的API.
  • 录音播放的基础教程几篇(传送门):
    http://www.cnblogs.com/kenshincui/p/4186022.html
    http://blog.csdn.net/ysy441088327/article/details/8164120
    http://msching.github.io/
    http://code.cocoachina.com/view/126074 (新增)
  • 主要遇到了几个问题:
  • 1.AVaudioRecorder的基本录音格式设置
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:          
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,//设置录音格式
[NSNumber numberWithFloat:8000], AVSampleRateKey,//设置录音采样率,8000是电话采样率,对于一般录音已经够了
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey, nil];
  • 注意点:录音的格式可以是AAC,M4A,PCM,具体选择根据你的项目需求。设置了格式对app的影响主要是录音文件的大小。一般情况的PCM>AAC>M4A。如果对本地存储没有限制,请随意选择。当需要控制文件大小时,就需要考虑牺牲一些,例如音质,采样质量等。
  • 其次:iOS7 之后,请再录音之前设置相应的AVAudioSession(相当于一个大管家)
    基本设置如下
 AVAudioSession *audioSession=[AVAudioSession sharedInstance];
    //设置为播放和录音状态,以便可以在录制完之后播放录音
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
    [audioSession setActive:YES error:nil];
    //设置播放器为扬声器模式
    [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
    NSError *audioError = nil;
    BOOL success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&audioError];
        if(!success)
        {
        NSLog(@"error doing outputaudioportoverride - %@", [audioError localizedDescription]);
         }
    if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
        [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
            if (granted) {
                // Microphone enabled code
                PADebug(@"正常录音");
            }
            else {
                // Microphone disabled code
                PADebug(@"失败录音");
            }
        }];
    }
  • 注意点:首先需要注意的是,当你在只录音的时候可以讲category设置为AVAudioSessionCategoryRecord;需要录音并播放的时候设置AVAudioSessionCategoryPlayAndRecord;需要播放的时候设置AVAudioSessionCategoryPlayback。
  • 其次,由于录音并播放时,会发现一个录音的时候声音正常,到播放的时候,声音很小。这是由于默认播放是通过听筒,而非扬声器。请设置音频输出为扬声器。并在插上耳机的时候,会优先耳机。
    解决声音小请参考:http://ruckt.info/playing-sound-through-iphone-speaker/

  • iOS上,录音音频格式可以为以下几种:

AAC (MPEG-4 Advanced Audio Coding)

ALAC (Apple Lossless)

iLBC (internet Low Bitrate Codec, for speech)

IMA4 (IMA/ADPCM)

Linear PCM (uncompressed, linear pulse-code modulation)

  • 文件格式可以为:.caf(默认支持最全);.acc;.m4a等,如果需要和安卓同步,就需要统一格式,并需要转码。

  • 局限性:recorder,player简单易用,但是有局限性。
    对我项目影响最大的是,多次录音时,并播放时,会出现文件错误。
    在继续利用recorder,player的前提下,就需要将每次录音完成的文件进行数据追加。
if ([[NSFileManager defaultManager] fileExistsAtPath:临时音频路径) {

        NSData *tempAudioData = [[NSData alloc] initWithContentsOfFile:临时音频路径];
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:音频路径]) {
            NSMutableData *newAudioData = [NSMutableData data];
            NSData *audioData = [[NSData alloc] initWithContentsOfFile:[self configureAudioRecordFilePath:self.currentFileName]];
            [newAudioData appendData:audioData];
            [newAudioData appendData:tempAudioData];
            PADebug(@"data length:%zd", [newAudioData length]);
            [newAudioData writeToFile:音频路径 atomically:YES];
        }else
        {
            [tempAudioData writeToFile:[self configureAudioRecordFilePath:self.currentFileName] atomically:YES];
        }
        [[NSFileManager defaultManager]removeItemAtPath:音频路径 error:nil];
    }

你可能感兴趣的:(ios 录音,播放 tips)