【发布时间】:2012-03-01 11:01:30
【问题描述】:
我正在尝试制作一个录音应用程序,所以显然我需要能够录制和播放音频文件。看起来它正在工作,根本没有错误,但它不会工作。我不确定它是否真的在录制?
它应该自己创建声音文件,对吗?我不必将声音文件放入项目中吗?
- (void)viewDidLoad
{
[super viewDidLoad];
playButton.enabled = NO;
stopButton.enabled = NO;
NSArray *dirPaths;
NSString *docsDir;
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];
}
}
// Methods called by the start recording button, stop recording, and play.
-(void) recordAudio
{
if (!audioRecorder.recording)
{
playButton.enabled = NO;
stopButton.enabled = YES;
[audioRecorder record];
}
}
-(void)stop
{
stopButton.enabled = NO;
playButton.enabled = YES;
recordButton.enabled = YES;
if (audioRecorder.recording)
{
[audioRecorder stop];
} else if (audioPlayer.playing) {
[audioPlayer stop];
}
}
-(void) playAudio
{
if (!audioRecorder.recording)
{
stopButton.enabled = YES;
recordButton.enabled = NO;
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioRecorder.url
error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else {
[audioPlayer setVolume:1.0];
[audioPlayer play];
}
}
}
// Delegate methods
-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
recordButton.enabled = YES;
stopButton.enabled = NO;
NSLog(@"did play");
}
-(void)audioPlayerDecodeErrorDidOccur:
(AVAudioPlayer *)player
error:(NSError *)error
{
NSLog(@"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:
(AVAudioRecorder *)recorder
successfully:(BOOL)flag
{
NSLog(@"did record");
}
-(void)audioRecorderEncodeErrorDidOccur:
(AVAudioRecorder *)recorder
error:(NSError *)error
{
NSLog(@"Encode Error occurred");
}
提前致谢。我对整个录音和处理音频文件和目录非常陌生。
【问题讨论】:
-
你是从techotopia.com/index.php/…复制粘贴过来的
-
是的,我做到了——在学习新概念时,我倾向于复制粘贴一些预定义的东西,让它工作,弄乱它,看看会发生什么,然后从头开始创建我自己的——每个人的学习方式都不一样,这对我有用 - 所以我必须先让这个工作:)
-
我只是问,这样做的一个问题是,如果它从一开始就不起作用,你不知道它有什么问题。
标签: iphone objective-c xcode avfoundation record