EDIT: Figured it out! I was saving the absoluteString of the URL, and when I tried initializing the audio player I used [NSURL fileURLWithPath:name]; when it should have been [NSURL urlWithString:name]. By creating another fileURLWithPath it was changing the path and wouldn't work. I'll leave this for some sample code I guess!
Okay, so I have a recorded audio piece from the user in my app. I want the user to be able to save these recordings, so I thought I should save the string of the url of the recording into core data, fetch the url, and load an AVAudioPlayer with that url. But it doesn't work.
It gives me:
Error in audioPlayer: The operation could not be completed.
This is the NSString of the URL:
file://localhost/file:/localhost/var/mobile/Applications/E09081DE-434F-46CA-A598-
A2470973C0F1/Documents/Rang%2520De%2520sound.caf
I have a feeling it has something to do with the fact that it saves in localhost, and I can't actually access that file when trying to initialize the audio player. But I'm not sure. I do know that the URL can be accessed right away, because after I record I am able to play back the recording using that URL.
How I'm saving the recording:
- (IBAction)saveRecording
{
rapAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObject *newSong;
newSong = [NSEntityDescription
insertNewObjectForEntityForName:@"URLRecordings"
inManagedObjectContext:context];
NSError *error;
NSURL *urlOfRecording = audioRecorder.url;
NSString *urla = [urlOfRecording absoluteString];
NSLog(@"in saveRecording, the url is: %@",urla);
[newSong setValue:urla forKey:@"urlOfSong"];
[context save:&error];
status.text = @"Song Saved!";
}
When I fetch it, the URL changes! I don't get why! Look:
2012-08-03 16:31:17.434 Rap To Beats[5054:707] in saveRecording, the url is: file://localhost/var/mobile/Applications/E09081DE-434F-46CA-A598-A2470973C0F1/Documents/Le%20Gayi%20sound.caf
2012-08-03 16:31:24.381 Rap To Beats[5054:707] after fetch, the url is: file://localhost/file:/localhost/var/mobile/Applications/E09081DE-434F-46CA-A598-A2470973C0F1/Documents/Le%2520Gayi%2520sound.caf
It went from Le%20 to Le%2520! I didn't re-record or anything I simply saved the same string in core data and fetched it! There's also some change in the localhost at the beginning.
Then this is how I'm fetching it:
rapAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"URLRecordings"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"urlOfSong" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context
executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[self setUrlArray:mutableFetchResults];
Any help on how to fix this guys? If it has to do with the localhost, then how do I save it somewhere else?
Here's the code on where it is saved:
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *name = self.beatName.text;
NSString *soundFilePath1 = [docsDir
stringByAppendingPathComponent:name];
NSString *soundFilePath = [soundFilePath1
stringByAppendingString:@" sound.caf"];
NSLog(@"in viewDidLoad, the url is: %@",soundFilePath);
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44010.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
Sorry for the length of this question, just want to be as helpful as possible and put whatever code might help solve the problem. Let me know if it is something else I am doing incorrectly! Thanks guys!
Figured it out! I was saving the absoluteString
of the URL, and when I tried initializing the audio player I used [NSURL fileURLWithPath:name]
when it should have been [NSURL URLWithString:name]
. By creating another fileURLWithPath
it was changing the path and wouldn't work. I'll leave this for some sample code I guess!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With