先生,您认为我的代码错误是什么......因为我无法录制音频。你能帮我完成我的项目吗?我想制作一个简单的录音工程。有三个按钮(PLAY,STOP,RECORD)......顺便说一句,我没有使用nib文件。在Objective-C IM新手我的做法是纯粹Programmatically..Thanks提前更多的权力..Objective C:AVFoundation Recording Issue ..
,这是我在viewDidLoad中的代码()
-(void)viewDidLoad
{
[super viewDidLoad];{
playButton.enabled = NO;
stopButton.enabled = NO;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
}
else
{
[audioRecorder prepareToRecord];
}
}
-(void) recordButton:(UIButton *)sender
{
if (!audioRecorder.recording)
{
playButton.enabled = NO;
stopButton.enabled = YES;
[audioRecorder record];
NSLog(@"Record");
}
}
-(void)stop:(UIButton *)sender
{
stopButton.enabled = NO;
playButton.enabled = YES;
recordButton.enabled = YES;
if (audioRecorder.recording)
{
[audioRecorder stop];
NSLog(@"Stop");
}
else if (audioPlayer.playing)
{
[audioPlayer stop];
}
}
-(void) playAudio:(UIButton *)sender
{
NSError *error;
if (!audioRecorder.recording)
{
stopButton.enabled = YES;
recordButton.enabled = NO;
NSLog(@"Play");
if (audioPlayer)
{
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error];
audioPlayer.delegate = self;
}
if (error)
{ NSLog(@"Error: %@",
[error localizedDescription]);
}
else
[audioPlayer play];
}
}
先生感谢您的信息..但我下载了..扫描,但仍然在即时遇到麻烦 – 2012-07-16 13:11:09
这[教程](http://www.iphoneam.com/blog/index.php?title=使用iphone-to-record-audio-a-guide&more = 1&c = 1&tb = 1&pb = 1)可能会更有趣。最后是一个带有完整解决方案的zip。 – werner 2012-07-16 13:15:38
再次感谢该链接先生..你有相同的问题,但在.xib /(IBOutlet或IBAction)方法的教程?再次感谢 – 2012-07-16 13:17:46