Ich habe dieses Problem mit AVAudioRecorder funktioniert nicht für mich, zumindest von dem, was ich sehen kann (oder nicht hören).iOS - Aufnahme Audio mit AVAudioRecorder scheitern ohne Fehler
Ich bin auf iOS 5 mit ARC ausgerichtet.
Ich habe Setup-Fehler-Objekte, aber keiner von ihnen gefeuert, so denke ich, ich mache hier etwas falsch.
ist hier der Teil waren i-Setup die AVAudioRecorder:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:currentTrack.trackFileName];
currentTrack.trackFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(@"Chemin : %@", currentTrack.trackFileURL.path);
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
currentTrack.trackRecordSettings = recordSetting;
NSError *err = nil;
//[audioSession setDelegate:self];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[[AVAudioSession sharedInstance] setActive:YES error:&err];
err = nil;
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
err = nil;
recorder = [[AVAudioRecorder alloc]
initWithURL:currentTrack.trackFileURL
settings:currentTrack.trackRecordSettings
error:&error];
if (err)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"error: %@", [error localizedDescription]);
} else {
[recorder setDelegate:self];
[recorder prepareToRecord];
BOOL audioHWAvailable = [[AVAudioSession sharedInstance]inputIsAvailable ];
if (!audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: @"Audio input hardware not available"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
return;
}
}
Nein hier geworfen Fehler
Dann, wenn es Zeit ist, um die Aufnahme zu starten:
-(void)startRecordingAudio{
NSLog(@"Start recording");
[[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryRecord error:nil];
[recorder record];
}
Dann, wenn es Zeit, um die Aufnahme zu stoppen:
-(void)stopRecordingAudio{
NSLog(@"Stop recording");
[recorder stop];
NSError *err;
NSData *audioData = [NSData dataWithContentsOfFile:currentTrack.trackFileURL.path options: 0 error:&err];
if(!audioData)
NSLog(@"audio data error: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
NSLog(@"%d", audioData.length);
}
Ist es normal, dass audioData Länge immer 4096 ist? Wenn ich richtig verstehe, ist es etwa 93 ms des Schalls ...
Schließlich, wenn es Zeit ist, den aufgenommenen Ton zu spielen:
-(void)startPlayingAudio{
[[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryPlayback error:nil];
NSLog(@"Start playing");
NSError *error;
if(player == nil){
player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentTrack.trackFileURL
error:&error];
}
player.delegate = self;
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[player play];
}
Ich habe Methoden Setup-Delegat, der nie feuern:
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog (@"audioRecorderDidFinishRecording:successfully:");
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(@"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully: (BOOL)flag
{
NSLog (@"audioRecorderDidFinishRecording:successfully: %d", flag);
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
NSLog(@"Encode Error occurred");
}
-(void)beginInterruption{
NSLog(@"INTERRUPT");
}
Auch ich habe gesehen, dass die .caf-Datei korrekt in meinem App-Dokument-Ordner erstellt werden.
Danke für jede Hilfe, die Sie mitbringen können. Bis jetzt kann ich den Ton nicht hören und ich benutze iPhone 4 Gerät ohne Kopfhörer.