Reputation: 17902
I'm working on record audio file and send to server. Record, stop, play and sending working properly. But my problem is if i click stop button i want to re save that audio file with new name in docs dir and send this audio file to server. My code is...
// File path where the recording will be saved on the iOS device
audioFilePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"RecordAudio.wav"];
NSLog(@"%@", audioFilePath);
NSURL *outputFileURL = [NSURL fileURLWithPath:audioFilePath];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
// Define the recorder setting
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];
[recordSetting setValue:[NSNumber numberWithInt: AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
// Initiate and prepare the recorder
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:&error];
audioRecorder.delegate = self;
audioRecorder.meteringEnabled = YES;
// Deal with any errors
if (error)
NSLog(@"error: %@", [error localizedDescription]);
else
[audioRecorder prepareToRecord];
// Check to see if we have permission to use the microphone.
if ([session respondsToSelector:@selector(requestRecordPermission:)]) {
[session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
if (granted) {
NSLog(@"Mic access granted");
}
else {
NSLog(@"Mic access NOT granted");
}
}];
}
-(NSURL *)applicationDocumentsDirectory {
NSLog(@"%@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject]);
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
}
- (IBAction)record:(id)sender {
NSLog(@"Started Recording");
if (!audioRecorder.recording)
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];
// Start recording
[audioRecorder record];
}
}
- (IBAction)stop:(id)sender {
NSLog(@"Stop Recording");
if ([audioRecorder isRecording])
{
[audioRecorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:NO error:nil];
}
else if (audioPlayer.playing)
[audioPlayer stop];
}
If i write this code in stop method ..
NSLog(@"%@", audioRecorder.url); // RecordAudio.wav
NSString *urlString = [audioRecorder.url.absoluteString stringByReplacingOccurrencesOfString:@"RecordAudio.wav" withString:@"RecordAudio2.wav"];
self.sendingURL = [NSURL URLWithString:urlString];
NSLog(@"%@", self.sendingURL);
When i click stop button i'm getting error Error: The operation couldn’t be completed. (OSStatus error 2003334207.)
Upvotes: 0
Views: 160
Reputation: 5684
You cannot change the name of that audio file. What you can do is create a copy of that audio file with your new name using the NSFileManager class
NSFileManager *fManager = [NSFileManager defaultManager];
[fManager copyItemAtURL:oldURL toURL:newURL error:error];
Upvotes: 1