AVAudioplayer retrieving nil argument for NSURL - objective-c

Hi there I am using the following snippet of the code.
NSString *path = [[NSBundle mainBundle]pathForResource:#"mtgcko" ofType:#"caf"];
[audioPlayer initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
audioPlayer.delegate = self;
[audioPlayer play];
It should work but on my device and simulator I receive the following error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
but the file I'm calling in the NSURL exist in MainBundle.
Don't have any idea where could be the problem.
I read in some articles on web that the problem could be in codecs?
But this seems not to be, because I am able to play all other formats including aiif, mp3, wav ...

Related

AVAudioPlayer is not working in sprite kit scene Xcode 7

i have been using the following code for playing background music in a sprite kit game and it was working fine in Xcode 6.4 but it throws a exception and crashes the game now:
- (void)startBackgroundMusic
{
NSError *err;
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"lostinspace.caf" ofType:nil]];
_backgroundAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];
if (err) {
NSLog(#"error in audio play %#",[err userInfo]);
return;
}
[_backgroundAudioPlayer prepareToPlay];
_backgroundAudioPlayer.numberOfLoops = -1;
[_backgroundAudioPlayer setVolume:0.5];
[_backgroundAudioPlayer play];
}
declaring the above code in didMoveToView as :[self startBackgroundMusic];
i have looked up the docs but can't find out what is wrong with what i already have. anyone knows how to fix this issue? below is the counsel out put:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
Instead of:
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"lostinspace.caf" ofType:nil]];
Try this:
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"lostinspace" ofType:#"caf"]];

Download ePub (NSURLConnection), save it, and open it with UIDocumentInteractionController

So I am trying to download an ePub book with NSURLConnection. So far, the download is working perfectly. The issue comes when I try to save it and open it. When I try to open it with UIDocumentInteractionController, the app crashes saying that the directory is null.
Here is my code:
//Download ePub file
NSURL *url = [NSURL URLWithString:#"http://www.terceiroanjo.com/materiais/missaopiloto.epub"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
then
// save it ("data" = NSMutableData that was appended from the received data of the download)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *filePath = [NSString stringWithFormat:#"%u/%#", NSDocumentDirectory,#"missaopiloto.epub"];
[data writeToFile:filePath atomically:YES];
}
and to open the ePub:
NSString *path = [[NSBundle mainBundle] pathForResource:#"missaopiloto" ofType:#"epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
When I try to open it, the app crashes:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
I get it, the directory is nill, but why? What am I doing wrong. Any ideas?
Thanks.
I think the problem here is that, once you download and save a file, it's not becoming part of the bundle.
If you want to access the file you must provide the local path and create the local URL
NSString *path = [NSString stringWithFormat:#"%u/%#", NSDocumentDirectory,#"missaopiloto.epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

Error about FMDB(Objective-c Database Library)

I'm developing iOS App with FMDB(Database Library of using SQLite easily).
For creating a database named "test.db", I'm writing down the following code for making the path of the database.
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *paths = [manager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSString *documentDirs = [paths objectAtIndex:0];
NSString *writableDBPath = [documentDirs stringByAppendingPathComponent:#"test.db"];
When I tested with Xcode Simulator, I got the following error.
2014-07-13 21:27:44.084 ninethtest[1481:a0b] -[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x8a5fdd0
2014-07-13 21:27:44.095 ninethtest[1481:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x8a5fdd0'
Could you tell me how to solve this error.
You are mixing-up NSString and NSURL objects. You want:
NSURL *documentDirs = [paths objectAtIndex:0];
NSURL *writableDBPath = [documentDirs URLByAppendingPathComponent:#"test.db"];
and if the FMDB API requires an NSString object for the database path, you can extract an NSString object from an NSURL object using:
NSString *writableDBPathString = [writableDBPath path];

NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance 0x712e450'

I am new to iPhone App development.
When I run a sample project, I did which parses an xml feed and displays the contents along with image in a table view, I get this error -
"NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance 0x712e450'"
It is occurring only when I try to display the image in UITableViewCell.
The code I used for getting images from url is -
if([elementName isEqualToString:IMAGE])
{
NSURL *imageUrl = [attributeDict objectForKey:#"url"];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
bbc.image = [UIImage imageWithData:imageData];
}
where bbc is a class(NSObject subclass) object used to store the parsed contents.
I think you are using NSString as NSURL. Try this:
NSURL *imageUrl =[NSURL URLWithString:[attributeDict objectForKey:#"url"]];
It looks like "url" is in fact an NSString, not an NSURL object. Convert it to an NSURL object yourself:
if ([elementName isEqualToString:IMAGE])
{
NSString *urlStr = [attributeDict objectForKey:#"url"];
NSURL *imageUrl = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
bbc.image = [UIImage imageWithData:imageData];
}
imageURL is not a NSURL, but a string.

File not found error in attempt to play audio (Obj-C)

I'm new to this, so in an attempt to keep things simple, I'm trying to add sound into my main.m
As far as I know, the following code should work, but when I run it, I get a "Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)" (File not found)"
I've tried it with various audio files and formats - added to the project in Xcode but can't figure out why it isn't finding my G1.wav
AVAudioPlayer *audioPlayer;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"/G1.wav"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
EDIT removed NSBundle and mainBundle references - my misunderstanding
You might be better off with this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"G1" ofType:#"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
...
Using pathForResource:ofType: you don't have to worry about where in the hierarchy of your bundle the file is. The API call figures it out. And in your code, whatever resourcePath is returning is clearly not the right path.
You might add some NSLog() calls here and there to see what is really happening, as well.