这是录制和播放音频的代码,它完美地创建了音频文件,但播放代码没有播放文件。我不明白出了什么问题我尝试了所有 nslog,音频文件也在文件夹中完美生成。
- (IBAction)recStartStop:(id)sender
{
if(toggle)
{
[recStartStopBtn setImage:[UIImage imageNamed:@"StopRed"] forState:UIControlStateNormal];
toggle = NO;
playBtn.enabled = toggle;
playBtn.hidden = !toggle;
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
NSLog(@"Using File called: %@",recordedTmpFile);
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
else
{
recStartStopBtn.enabled = NO;
recStartStopBtn.hidden = YES;
playAudioBtn.hidden = NO;
NSLog(@"Using File called: %@",recordedTmpFile);
[recorder stop];
}
}
- (IBAction)playAudio:(id)sender
{
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:nil];
[avPlayer prepareToPlay];
[avPlayer play];
NSLog(@"%@",recordedTmpFile);
}