Locating an audio file to play in Objective C - objective-c

I'm a Swift guy going back to do some Objc work and I'm trying to add a sound to my app, problem is, the path is null when trying to locate the file
The file is located at myProj/Resources/Sounds/genericButtonPress.wav
NSString *path = [[NSBundle mainBundle] pathForResource:#"myProj/Resources/Sounds/genericButtonPress" ofType:#"wav"];
NSLog(#"%#", path); // NULL
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player play];
From the docs, it looked like pathForResource had to be absolute but I've also tried with just genericButtonPress as my string.
Would love some help here. Thanks!

The answer is this:
Remember kids, always make sure that your files are assigned to your proper target or things won't show up and won't work. That goes for fonts and audio files especially.

My solution is
ViewController.h
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#interface ViewController : UIViewController
#property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)actionPlayAudio:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:#"youraudiofilename" ofType:#"wav"];
NSURL *soundFileURL = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player play];
}

Related

How do I play audio when my ios app starts?

I have been following tutorials and looking at other posts but I can't seem to get audio to start playing automatically when my app starts.
I want the wav file to just keep playing in the background and looping. I do NOT want start and stop controls etc...
Below is the code I have in my ViewController.m file (is this the right place?) and I have added the AVFoundation.frameowrk and imported it to my .m file. Additionally I have added my wav file called AlienRoom to my supporting files. Any help would be awesome! I am new so please go step by step or make it real clear if you can. Thank you!!!
p.s. I haven't added the code to make it loop so any help there would be great too!
//play background sound upon opening app
-(void) awakeFromNib
{
NSString *path = [[NSBundle mainBundle] pathForResource: #"AlienRoom" ofType: #"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
Implement AVAudioPlayerDelegate in ViewController.h
#interface ViewController : UIViewController <AVAudioPlayerDelegate>
#property(nonatomic,strong)AVAudioPlayer *theAudio;
ViewController.m file
-(void)viewWillAppear:(BOOL)animated
{
NSString *path = [[NSBundle mainBundle] pathForResource: #"AlienRoom" ofType: #"wav"];
if(!self.theAudio)
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.theAudio.delegate = self;
[self.theAudio play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{
[NSThread detachNewThreadSelector:#selector(playAudioAgain) toTarget:self withObject:nil];
}
- (void)playAudioAgain{
[self.theAudio play];
}
-(void)viewWillDisappear:(BOOL)animated
{
if(self.theAudio.isPlaying)
{
self.theAudio.delegate = nil;
[self.theAudio stop];
}
}

AVAudioPlayer problems - no issues but doesn't work

To play a button sound when button tapped, I use AudioToolbox and AVFoundation.
I import these in my .h using
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
Via storyboard, I connected a UIButton to a new IBAction and then included the following in my .m
- (IBAction)playSound11:(id)sender {
//Load an audio file from the bundle
NSString *audioFile = [[NSBundle mainBundle] pathForResource:#"11" ofType:#"mp3"];
//Convert string to NSURL
NSURL *audioURL = [NSURL fileURLWithPath:audioFile];
//Initialize an audio player with the mp3 URL
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
// Start playing
[player play];
}
I get no issues and the app does open but there is no sound at all.
What am I doing wrong?
Problem with your URL perhaps?
This is how I use it
NSURL *audioURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/11.mp3", [[NSBundle mainBundle] resourcePath]]];

How can I load an HTML file?

How do I load an HTML file into my app (xcode)? I'm using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
}
When I start the app, I only see a white page. What's wrong?
.h file:
#import <UIKit/UIKit.h>
#interface poules : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic,retain) UIWebView *webView;
#end
Here is a working example from one of my projects. My index.html file is in a folder called Documentation/html in the resources directory. Its important to note that these are "folder references", not groups (hence the blue icon):
then to load it in a webView:
NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
NSArray *pathComponents = [NSArray arrayWithObjects:resourceDir, #"Documentation", #"html", #"index.html", nil];
NSURL *indexUrl = [NSURL fileURLWithPathComponents:pathComponents];
NSURLRequest *req = [NSURLRequest requestWithURL:indexUrl];
[webView loadRequest:req];
try loadHTMLString:baseURL: method
NSString *html=[NSString stringWithContentsOfFile:your_html_path encoding:NSUTF8StringEncoding error:nil];
[webview loadHTMLString:html baseURL:baseURL];
First thing you can check whether your webView is connected or not.
If it is then, you can break down your code to check what is wrong with request that you are trying to load.
1.. Create a NSString for the file path like this, and check if the path is returned or not.
NSString *urlString = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
2.. Create a NSURL from the above string like this, and check if the url is correct or nil.
NSURL *url = [NSURL URLFromString:urlString];
3.. And then create the request.
NSURLRequest *request = [NSURLRequest requestFromURL:url];

Can I play loops using Audio Toolbox?

in my app i use AudioToolbox framework and i need to play a loop can i ?
this is the AVAudioplayer code that goes slowly with 2 touches simultaneously
- (AVAudioPlayer *)getNoteFromFilename:(NSString *)name andoftype:(NSString *)type{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:name
ofType:type];
NSURL *fileURL = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *note = [[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil] autorelease];
return note;
}
in the viewdidload
ViewDidLoad {
self.Note = [self getNoteFromFilename:#"Note" andoftype:#"wav"];
}
calling it by this in IBAction :
[Note prepareToPlay];
[Note play];
NB: i have retaind all the button and the AVAudioPlayer
Use AVAudioPlayer instead. There you can easily loop using the numberOfLoops property.

problem loading url in view

i'm confused about an error while i'm trying an url in a view. while compiling i get the following 2 errors (in h-file an in m-file):
Expected identifier before '*' token
maybe anybody can help me out of my trouble? thanks in advance!
my code:
File "RssWebViewController.h":
#import "RssWebViewController.h"
- (void)NavigateToUrl:(NSString) *url{
NSURL *requestUrl = [NSURL URLWithString:self.url];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj]
}
File "RssWebViewController.h":
#import <UIKit/UIKit.h>
#interface RssWebViewController : UIView {
UIWebView *WebView;
}
#property (nonatomic, retain) IBOutlet UIWebView *WebView;
- (void) NavigateToUrl:(NSString) *url;
#end
You need to structure your function definition with the * inside the parentheses:
- (void) NavigateToUrl: (NSString *) url;
It seems that you're referencing self.url, but really should be looking at url (no self.)
Here's a clearer version of the method:
- (void) NavigateToUrl: (NSString *) url {
NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString: url]];
[self.WebView loadRequest: request];
}