先生,您認爲我的代碼錯誤是什麼......因爲我無法錄製音頻。你能幫我完成我的項目嗎?我想製作一個簡單的錄音工程。有三個按鈕(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