ios进阶酷音频

ios 录音,播放 tips

2017-05-08  本文已影响396人  一个人的思考
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:          
[NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,//设置录音格式
[NSNumber numberWithFloat:8000], AVSampleRateKey,//设置录音采样率,8000是电话采样率,对于一般录音已经够了
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey, nil];
 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(@"失败录音");
            }
        }];
    }

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)


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];
    }
上一篇下一篇

猜你喜欢

热点阅读