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"]];
Related
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];
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 ...
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.
I tried AVAudioPlayer it doesnt work.
url= [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/test.mid", [[NSBundle mainBundle] resourcePath]]];
NSLog(#"URL Description is %#",[url description]);
NSError *error=nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error!=nil) {
NSLog(#"Error is %#",[error localizedDescription]);
}
[audioPlayer play];
I get this error
Error is The operation couldn’t be completed. (OSStatus error 1954115647.)
Should I be using another method to do this? Any suggestions?
Thanks
MusicPlayer+MusicSequence on ios 5
You can make use of the built-in CoreMIDI framework. Docs here:
http://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/CACoreMIDIRef/MIDISetup/index.html
http://www.slideshare.net/invalidname/core-midi-and-friends
http://goodliffe.blogspot.com/2010/10/using-coremidi-in-ios-example.html
Hope it helps.
NSError *err;
// Initialize audio player
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
audioPlayer.delegate = self;
[audioPlayer play];
With the code above, I'm trying to initialize playback of a .mp3 file, however the playback does not start at all. There is no sound. What am I doing wrong? I have inspected 'err' and there is nothing there.
Edit: After adding AVAudioSession, I'm getting the following error from AVAudioPlayer
The operation couldn’t be completed. (OSStatus error -43.)
Apparently AVAudioPlayer does not support streaming via. HTTP as I was trying to do, so by using AVPlayer instead, I got it working.
Did you initialize a AVAudioSession?
The following works fine for me. Might give you a hint of whats not working for you.
Initializing:
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:nil];
[session setActive:TRUE error:nil];
Loading:
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer prepareToPlay];
audioPlayer.volume = 1.0;
Playing:
[audioPlayer play];
Update
To find the right NSRUL for fileUrl, I used this code:
NSURL* fileUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"MySound" ofType:#"wav"] isDirectory:NO];
And then I added MySound.wav to the project (bundle).