我一直在錄製音頻並在IOS上播放它們。記錄和播放.wav文件的問題IOS
這是我使用的錄音:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
recordSettings = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 16000.0],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,// kAudioFormatLinearPCM
[NSNumber numberWithInt:8],AVLinearPCMBitDepthKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithInt: AVAudioQualityLow],AVEncoderAudioQualityKey,
nil];
recorderFilePath = [[NSString stringWithFormat:@"%@/%@", filePath, fileName] retain];
NSLog(@"recorderFilePath: %@",recorderFilePath);
NSURL *audioFileURL = [NSURL fileURLWithPath:recorderFilePath];
recorder = [[ AVAudioRecorder alloc] initWithURL:audioFileURL settings:recordSettings error:&err];
if(!recorder){
NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
//prepare to record
[recorder setDelegate: self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
和記錄只是正常工作對我來說,在與上述設置.wav文件。現在,問題在於播放。
以下是播放錄製的.wav音頻文件。
NSURL * url = [NSURL fileURLWithPath:filePath];
audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&err] autorelease];
if (! audioPlayer) {
NSLog(@"Sound named '%@' had error %@", name, [err localizedDescription]);
} else {
[audioPlayer prepareToPlay]; // Getting the return value NO here.
}
// audioPlayer.volume = 1; // Tried setting different values, but of no help.
[audioPlayer play]; // Getting the return value NO here.
它不會拋出任何錯誤,但它什麼都不做。沒有聲音在播放。雖然可以用播放器播放「.mp3」文件(來自資源的測試文件),但播放使用上述設置錄製的文件是個問題。
我不知道我在這裏錯過了什麼。請幫忙!謝謝
我覺得,問題在於錄音。我試過一個例子「.wav」文件,它很好。你認爲這些設置需要改變才能錄製好聲音嗎? – user1873452 2013-02-25 05:40:26
不,這對我來說似乎還行...... – Yogi 2013-02-25 05:46:35
在這種情況下,只能播放使用AVAudioRecorder錄製的文件。 – user1873452 2013-02-25 06:25:24