为了账号安全,请及时绑定邮箱和手机立即绑定

如何用AVAudioRecorder在iPhone上录制音频?

如何用AVAudioRecorder在iPhone上录制音频?

牧羊人nacy 2019-07-22 14:43:23
如何用AVAudioRecorder在iPhone上录制音频?既然iPhone3.0SDK已经公开,我想我可以问那些已经在玩3.0SDK的人这个问题了。我想在我的应用程序中记录音频,但我想使用AVAudioRecorder,而不是像SpeakHere示例那样使用更老的记录方式。在iPhoneDevCenter中,没有任何示例说明如何最好地做到这一点,而且只引用了类。我是iPhone开发的新手,所以我正在寻找一个简单的示例来让我开始。提前谢谢。
查看完整描述

3 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

虽然这是一个已回答的问题(有点旧),但我决定将我的完整工作代码发布给其他发现很难找到良好工作(开箱即用)播放和录制示例的人-包括编码、pcm、通过扬声器播放、写在这里的文件:

AudioPlayerViewController.h:

#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface AudioPlayerViewController : UIViewController {AVAudioPlayer *audioPlayer;AVAudioRecorder *audioRecorder;int recordEncoding;enum{
    ENC_AAC = 1,
    ENC_ALAC = 2,
    ENC_IMA4 = 3,
    ENC_ILBC = 4,
    ENC_ULAW = 5,
    ENC_PCM = 6,} encodingTypes;}-(IBAction) startRecording;-(IBAction) stopRecording;-(IBAction) playRecording;-(IBAction) stopPlaying;@end

AudioPlayerViewController.m:

#import "AudioPlayerViewController.h"@implementation AudioPlayerViewController- (void)viewDidLoad{
    [super viewDidLoad];
    recordEncoding = ENC_AAC;}-(IBAction) startRecording{NSLog(@"startRecording");[audioRecorder release];audioRecorder = nil;// Init audio with record capabilityAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];if(recordEncoding == ENC_PCM){
    [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];   }else{
    NSNumber *formatObject;

    switch (recordEncoding) {
        case (ENC_AAC): 
            formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
            break;
        case (ENC_ALAC):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
            break;
        case (ENC_IMA4):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
            break;
        case (ENC_ILBC):
            formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
            break;
        case (ENC_ULAW):
            formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
            break;
        default:
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
    }

    [recordSettings setObject:formatObject forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];}NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];NSError *error = nil;audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];if ([audioRecorder prepareToRecord] == YES){
    [audioRecorder record];}else {
    int errorCode = CFSwapInt32HostToBig ([error code]); 
    NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); }NSLog(@"recording");}-(IBAction) stopRecording{NSLog(@"stopRecording");[audioRecorder stop];NSLog(@"stopped");}-(IBAction) playRecording{NSLog(@"playRecording");// Init audio with playback capabilityAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];NSError *error;audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];audioPlayer.numberOfLoops = 0;[audioPlayer play];NSLog(@"playing");}-(IBAction) stopPlaying{NSLog(@"stopPlaying");[audioPlayer stop];NSLog(@"stopped");}- (void)dealloc{[audioPlayer release];[audioRecorder release];[super dealloc];}@end

希望这能帮到你们中的一些人。


查看完整回答
反对 回复 2019-07-22
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

这真的很有帮助。我唯一的问题是录音后创建的声音文件的大小。我需要缩小文件大小,所以我在设置中做了一些更改。

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

文件大小从360 kb减少到只有25 kb(2秒录音)。


查看完整回答
反对 回复 2019-07-22
  • 3 回答
  • 0 关注
  • 608 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信