如何用AVAudioRecorder在iPhone上录制音频?既然iPhone3.0SDK已经公开,我想我可以问那些已经在玩3.0SDK的人这个问题了。我想在我的应用程序中记录音频,但我想使用AVAudioRecorder,而不是像SpeakHere示例那样使用更老的记录方式。在iPhoneDevCenter中,没有任何示例说明如何最好地做到这一点,而且只引用了类。我是iPhone开发的新手,所以我正在寻找一个简单的示例来让我开始。提前谢谢。
3 回答
慕标5832272
TA贡献1966条经验 获得超4个赞
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
哆啦的时光机
TA贡献1779条经验 获得超6个赞
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
- 3 回答
- 0 关注
- 287 浏览
添加回答
举报
0/150
提交
取消