I currently have this code that works. I have two questions.
How do I set the directory to "Supporting Files"?
How do I use a variable in the file name? I need it to pull an mp3 of any animal based on a variable.
- (void)playAnimal{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Sheep.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];
}
I'm very new to Objective-C, so a lot of syntax things are new to me as well as general methods. Thanks for your patience!
Use the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Sheep" ofType:#"mp3"]];
NSURL *url = [NSURL fileURLWithPath:path];
When your resources are copied to the bundle, they do not retain their directory structure. Just identify the resource by name and it will find the path for it.
Just as an FYI, the following works, as well:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Sheep.mp3" ofType:#""]];
NSURL *url = [NSURL fileURLWithPath:path];
This may be useful if you have a set of resources logically grouped together in a plist and you are programmatically reading those out. For example:
// reference to a dictionary with the entry 'Sheep.mp3'
NSString *resourceName = [dictionary objectForKey:item1];
NSString *path = [[NSBundle mainBundle] pathForResource:resourceName ofType:#""]];
NSURL *url = [NSURL fileURLWithPath:path];
Related
I am trying to write code with dynamic input for pathForResource but I get null value for the directory listing. the files I am using are wav files. Here is the code. I have a.wav file in the project folder. What is wrong here? how can I make this work? Please help.
int alphanum = 97;
NSString *alphastring = [NSString stringWithFormat:#"%i",alphanum];
NSString *path = [[NSBundle mainBundle] pathForResource:alphastring ofType:#"wav"];
AVAudioPlayer *theplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theplayer play];
I use this line of code to load a local html file into a web view:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
However I want to add some http parameters to the url with no luck so far.
I've tried this:
url = [url URLByAppendingPathComponent:#"?param1=1"];
But after this a html doesn't load in webview.
Is there a way to load local html file in webview with params ?
Do this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
NSString *URLString = [url absoluteString];
NSString *queryString = #"?param1=1";
NSString *URLwithQueryString = [URLString stringByAppendingString: queryString];
NSURL *finalURL = [NSURL URLWithString:URLwithQueryString];
NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[web loadRequest:request];
The most upvoted answer by Prince doesn't always work.
In iOS 7 Apple introduced NSURLComponents class, we can leverage that to securely add a query into NSURL.
NSURL *contentURL = ...;
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:contentURL resolvingAgainstBaseURL:NO];
NSMutableArray *queryItems = [components.queryItems mutableCopy];
if (!queryItems) queryItems = [[NSMutableArray alloc] init];
[queryItems addObject:[NSURLQueryItem queryItemWithName:#"access_token" value:#"token_here"]];
components.queryItems = queryItems;
NSURL *newURL = components.URL;
I use like this way:
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"xml"]];
If your file included into the Resource folder of project navigator. otherwise you have to set your full path in NSString and use that in your NSURL path.
NSURL *modelURL1 = [[NSBundle mainBundle] URLForResource:#"MyTest" withExtension:#"momd"];
NSLog(#"%#", [modelURL1 absoluteURL]);
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyTest" ofType:#"momd"];
NSURL *modelURL = [NSURL URLWithString:path];
NSLog(#"%#", [modelURL absoluteURL]);
The first line is generated by XCode when I use Core Data, but it does not work because the method doesn't work prior to iOS 4. So I need another method, I thought I could use the second one but it returns nil...Why does it not work?
Thanks
The second one doesn't work because path isn't a URL. Use this instead:
NSURL *modelURL = [NSURL fileURLWithPath:path];
I want to play mp3 file placed in resources as code is given below,
NSString *st=#"alarm_1.mp3";
NSLog(st);
NSURL* musicFile = [NSURL fileURLWithPath:st];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
//AVAudioPlayer *click1=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:#"alarm_1.mp3"]];
[click prepareToPlay];
[click play];
[click release];
but unfortunately i cant hear the sound
please help
I don't think your file name is right. Try this syntax:
NSURL *clickURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:clickURL error:nil];
I had a similar problem due to ARC. Instead of defining the AVAudioPlayer in the same method you are using it, you should should have an instance variable somewhere else, such as the UIViewController. This way ARC doesn't auto release this object and audio can play.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
This is answer
I'm grabbing data from an external file using NSURL with the code below. If i wanted to grab this data from an internal file in my resources folder how would the code be different.
here's the NSURL code:
NSURL *dataUrl = [NSURL URLWithString:#"https://sites.google.com/site/*****/***/file.asc"];
NSString *fileString = [NSString stringWithContentsOfURL:dataUrl encoding:NSUTF8StringEncoding error:nil];
this was my attempt:
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"asc"];
NSString *fileString = [NSString initWithContentsOfFile:path];
thanks in advance
Maybe try stringByExpandingTildeInPath
something like
NSString *path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"asc"];
path = [path stringByExpandingTildeInPath];
NSString *fileString = [NSString initWithContentsOfFile:path];