【发布时间】:2011-10-31 21:40:42
【问题描述】:
我需要用 iPhone 麦克风录制音频流。 我正在使用这个东西的 AVAudioRecorder。
我在 init 中初始化 AVAR:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [[paths objectAtIndex:0] retain];
NSError *error;
BOOL isDir = NO;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
pathNewFile= [cachePath stringByAppendingPathComponent:@"123.caf"];
NSLog(@"%@",pathNewFile);
NSDictionary *settings=[[NSDictionary alloc]initWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0f],AVSampleRateKey,
[NSNumber numberWithInt:kAudioFormatAppleLossless],AVFormatIDKey,
[NSNumber numberWithInt:1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil];
recorder=[[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:pathNewFile] settings:settings error:nil];
[recorder prepareToRecord];
当用户按下rec-button时我开始录制:
-(void)recButton:(id)sender{
NSLog(@"start rec");
[rootNotesButton setTitle:@"Stop" forState:UIControlStateNormal];
[rootNotesButton removeTarget:self action:@selector(recButton:) forControlEvents:UIControlEventTouchUpInside];
[rootNotesButton addTarget:self action:@selector(stopButton:) forControlEvents:UIControlEventTouchUpInside];
[recorder record];
}
-(void)stopButton:(id)sender{
NSLog(@"stop rec");
[rootNotesButton setTitle:@"Play" forState:UIControlStateNormal];
[rootNotesButton removeTarget:self action:@selector(stopButton:) forControlEvents:UIControlEventTouchUpInside];
[rootNotesButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
[recorder stop];
}
-(void)playButton:(id)sender{
NSLog(@"play rec");
[rootNotesButton setTitle:@"Stop" forState:UIControlStateNormal];
[rootNotesButton removeTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
[rootNotesButton addTarget:self action:@selector(stopPButton:) forControlEvents:UIControlEventTouchUpInside];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:pathNewFile] error:nil];
[player play];
}
-(void)stopPButton:(id)sender{
NSLog(@"Stop play");
[rootNotesButton setTitle:@"Play" forState:UIControlStateNormal];
[rootNotesButton removeTarget:self action:@selector(stopPButton:) forControlEvents:UIControlEventTouchUpInside];
[rootNotesButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
[player stop];
}
但是当我尝试在模拟器中运行我的项目时:我的调试器在字符串上停止: 录音机=[[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:pathNewFile] 设置:设置错误:无];没有任何信号或错误。
【问题讨论】:
标签: objective-c ios ios-simulator avaudiorecorder