Error Thread 1: signal SIGBRT - objective-c

I'm learning Objective C and I'm following examples from this book. However in some examples I keep getting this error! I followed the steps exactly, please help! I've already tried restoring the iOS simulator to its default settings and still no luck.
It's an app where you can record a sound and play it back. Here's the code...
- (IBAction)recordAudio:(id)sender {
if ([self.recordButton.titleLabel.text isEqualToString:#"Record Audio"]){
[self.audioRecorder record];
[self.recordButton setTitle:#"Stop Recording"
forState:UIControlStateNormal];
} else {
[self.audioRecorder stop];
[self.recordButton setTitle:#"Record Audio"
forState:UIControlStateNormal];
// Load the new sound in the audioplayer for playback
NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringsByAppendingPaths:#"sound.caf"]];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundFileURL error:nil];
}
}

I ran part of your code, and this was the error message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***
-[__NSCFString stringsByAppendingPaths:]: paths argument is not an array'
As the documentation says, stringsByAppendingPaths returns an NSArray of NSString objects made by separately appending each NSString in an NSArray of paths to the receiver. #"sound.caf" is an NSString, not an NSArray, which raises the exception.
Change the following:
NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringsByAppendingPaths:#"sound.caf"]];
To:
NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringByAppendingPathComponent:#"sound.caf"]];
and it should work.

Related

Terminate called throwning an exception

I have make a piece of code for playing a video file. But when i build it i get the folowing error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter'
First throw call stack:
(0x1ed8022 0x11c0cd6 0x1e80a48 0x1e809b9 0xc6553b 0xc654c5 0x2be8 0x1ed9e99 0x32614e 0x3260e6 0x3ccade 0x3ccfa7 0x3cc266 0x34b3c0 0x34b5e6 0x331dc4 0x325634 0x17baef5 0x1eac195 0x1e10ff2 0x1e0f8da 0x1e0ed84 0x1e0ec9b 0x17b97d8 0x17b988a 0x323626 0x29b2 0x2925)
terminate called throwing an exception
I have tried a lot to fix it but nothing does work! Do you no a solution? And yes, i have implementated the mediaplayer framework!
I have coded the follwing code:
-(IBAction)playvideo {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"film" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release];
playercontroller = nil;
}
The problem is described in this line:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter'
Which is telling you that you are passing a nil into a parameter that doesn't allow a nil.
Steps to be sure that this is the problem:
-(IBAction)playvideo {
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"film" ofType:#"mp4"]
if (!videoPath) {
NSLog(#"Video path is nil. My bundle must be set up incorrectly");
return; // return early.
}
NSURL *url = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
// The rest of your implementation here.
}
This should at least help you to localise the problem.
As an aside - you should be using the URL based methods for getting resources instead of string based file-paths. But one thing at a time.

Hpple implementation/Unrecognized selector

I am working with the hpple html parser here: https://github.com/topfunky/hpple
To test the function I've added it to a simple project and am able to compile and open the simulator without errors, but when it is called, I get an unrecognized selector error.
//THIS ACTION SET TO RUN WITH THE PUSH OF A BUTTON
- (IBAction)parseElements{
NSString *urlRequest = item.link;
NSLog(#"urlRequest defined.");
NSData *htmlData = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlRequest] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"htmlData created.");
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSLog(#"xpathParser made.");
NSString *queriedItem = #"[#class='title']";
// THE APP FAILS WHILE TRYING TO EXECUTE THE NEXT LINE
NSArray *elements = [xpathParser searchWithXPathQuery:queriedItem];
NSLog(#"elements searched.");
TFHppleElement *element = [elements objectAtIndex:0];
NSLog(#"element recalled.");
NSString *storyTitle = [element content];
NSLog(#"The title of the story is: %#", storyTitle);
}
The NSLogs manage to display through "xpathParser made" and then I receive this unrecognized selector message:
-[__NSCFString bytes]: unrecognized selector sent to instance 0x6a52d60
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x6a52d60'
* First throw call stack:
(0x16c8052 0x1859d0a 0x16c9ced 0x162ef00 0x162ece2 0x495c 0x352e 0x2e3f 0x16c9ec9 0x1395c2 0x13955a 0x1deb76 0x1df03f 0x1de2fe 0x15ea30 0x15ec56 0x145384 0x138aa9 0x15b2fa9 0x169c1c5 0x1601022 0x15ff90a 0x15fedb4 0x15feccb 0x15b1879 0x15b193e 0x136a9b 0x2658 0x25b5)
terminate called throwing an exception
I understand it doesn't like SOMETHING, but what is causing the glitch or are additional frameworks/imports necessary for proper execution? As it stands I have UIKit, viewcontroller.h and TFHpple.h set as the only imports in that file.
Here's your problem:
NSData *htmlData = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlRequest] encoding:NSUTF8StringEncoding error:nil];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
TFHpple's initWithHTMLData is supposed to take an NSData. You declare htmlData to be an NSData, but the actual object you're assigning to it is an NSString.
This should fix it:
NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: urlRequest]];

AVAudioPlayer error

i'm using this code but it's not working;
Import:
#import <AudioToolbox/AudioServices.h>
Header File:
AVAudioPlayer *_audioPlayer;
Code:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/welcome.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
_audioPlayer.numberOfLoops = 0;
if (_audioPlayer == nil)
{
NSLog(#"%#", [error description]);
}
else
{
[_audioPlayer play];
NSLog(#"PLAY");
}
The NSLog:
Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"
it's not working on my iPhone (5.1) iPad (5.0) iPhone 5.0 Simulator.
Anyone know how i can fix this?
You get that error if no file was found at the path you specified.
Also, check that you do have a file welcome.mp3 and that it is bundled with the application target. To do that, select the target, go to Build Phases tab and make sure the file is in the list Copy Bundle Resources. If it is not there, add it.

(iOS) Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43

NSString *songNameEscaped = [songName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *songURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", #"http://somerootpath/Music/", songNameEscaped]];
NSLog(#"songURL = %#", songURL);
NSError *avPlayerError = nil;
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:&avPlayerError];
if (avPlayerError)
{
NSLog(#"Error: %#", [avPlayerError description]);
}
else
{
[avPlayer play];
}
If I copy the NSLog output from NSLog(#"songURL = %#", songURL); and paste it in safari, Quicktime plugin plays the files no problem so I know the URLs are valid. I've tried with .m4a and .mp3 files and have tried removing the spaces from songName but not matter what I always get Error:
Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)".
But they are just standard .m4a / .mp3 files created in iTunes.
Apparently AVAudioPlayer doesn't support streaming. Apple recommends AVPlayer for this although it is not as conveinient for finding things like current time and duration .
Error -43 indicates an inability to find the data. This could be because the url was in fact malformed, or because the server doesn't support streaming. You should try to pre-load the song data with an NSData object.
NSString *songNameEscaped = [songName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *songURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", #"http://somerootpath/Music/", songNameEscaped]];
NSLog(#"songURL = %#", songURL);
NSError *avPlayerError = nil;
NSData* songData = [NSData dataWithContentsOfURL:songURL error:&avPlayerError];
if (songData)
{
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithData error:&avPlayerError];
if (avPlayer)
{
[avPlayer prepareToPlay];
[avPlayer play];
}
else
{
NSLog(#"Error initializing data for AVAudioPlayer. Possibly an Unsupported Format");
NSLog(#"Error: %#", [avPlayerError description]);
}
}
else
{
NSLog(#"Error initializing data for AVAudioPlayer. Possibly Malformed URL");
NSLog(#"Error: %#", [avPlayerError description]);
}
I had the same problem yesterday. Turns out my url was wrong.
I had something like you here:
NSURL *songURLID = [NSURL URLWithString:[NSString stringWithFormat:#"%#%d", #"http://somerootpath/Music/", songNameEscaped]];
NSLog(#"songURL = **%d**", songURL**ID**);
But, my songURL was of NSString type. My NSLog wrote it correctly but when I put %d in url it turn out wrong. I suggest you try to check out your AVAudioPlayer url again.
try something like described here to see the players url.
You may want to try checking the targets on your audio file. I had a similar issue and fixed it by making sure that my mp4 file target membership check box was checked. I must not have checked it when I was importing the file originally.
in Apple documentation
An instance of the AVAudioPlayer class, called an audio player,
provides playback of audio data from a file or memory.
Apple recommends that you use this class for audio playback unless you
are playing audio captured from a network stream or require very low
I/O latency.

Converting NSMutableString to NSUrl in Objective C

I actually stuck with a problem while parsing xml-file from a dynamic url in Objective-C.
The Parser works fine for the whole project, but now I have to set a dynamic URL to parse the needed XML-File.
I have two Variables: One for the BaseURL and one for the Params.
Here is my documented Code:
//The Baseurl
NSMutableString* baselink = [NSMutableString stringWithString:zimmertyp.typlink];
//Adds the params to URL
[baselink appendString:aSlice.link];
//In Log it shows the right url to my XML-File with params
NSLog(#"Selected-URL: %#", url);
//I tried to convert the String to NSURL here
NSURL *url = [NSURL URLWithString: baselink];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
self.navigationItem.prompt = #" ";
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
Everytime the Parser tries to walk through, I get:
2011-02-16 16:40:03.371 Project[15566:207] Selected-URL: http://xml.projectwebsite.de/price/doppelzimmer.xml
?Zimmertyp=Doppelzimmer+Classic
2011-02-16 16:40:03.373 Project[15566:207] Error Error Error!!!
2011-02-16 16:40:03.374 Project[15566:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString stringByAppendingString:]: nil argument'
It seems like the NSUrl is empty, or am I wrong?
The App crashes right at the point when the Parser initialized with the url.
Can anyone tell me what's my fault?
greeting,
Zarakas
I solved the problem by myself. The AppendString method added whitespaces to the String... dunno why. I escaped the spaces from the string. Now parser works fine.. ;)