How to run a audio file again and again in ios sdk - ios7

SystemSoundID soundID6; // declaration
NSURL *gamePlayMusic = [NSURL fileURLWithPath:[[NSBundle
mainBundle]pathForResource:#"song1" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)Music,&soundID6);
AudioServicesPlaySystemSound(soundID6); //running of music
may I know what to code to run this music again after it finish/complete.

1. Add AVKit.framework.
2.create a global webview object like UIWebView* audioPlayerView;
3.create a button action for playing audio file
4.your mp3 file add in to your bundel.
-(IBAction)playAudioFile:(UIButton*)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"your file name" ofType:#"mp3"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[audioPlayerView loadRequest:request];
[audioPlayerView setScalesPageToFit:YES];
[self.view addSubview:audioPlayerView];
}

Related

xcode 5, and 4.6.3 Can't load a rtfd.zip file into UIWebView

Prior to upgrading to xcode 4.6.3 the following code worked perfectly. Now it does NOT. I get a blank white page. This code is placed after super viewDidLoad:
NSString *path = [[NSBundle mainBundle]pathForResource:#"info.rtfd" ofType:#"zip"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView setScalesPageToFit:YES];

OS X Web View App - Won't Load Local .html Web Page

I have the following code in an AppDelegate.m file that opens Google when I open my app. I am trying to change it to open a .HTML web page I copied into my Xcode project but it is not working. I am using Xcode 4.5.2.
The code that works to open Google is:
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]];
[self.webView.mainFrame loadRequest:request];
}
I tried changing it to (so that it would load my test.html file I put in the project folder but the app just comes up blank now). What am I missing?
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"test.html"]];
[self.webView.mainFrame loadRequest:request];
}
As long as your test.html is copied at the root of your project (make sure it's also copied to your final app bundle, huh? - check Build Phases) you can load it up your Web View using this code :
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL* fileURL = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[self.webView.mainFrame loadRequest:request];
[NSURL URLWithString:#"test.html"] is not creating a valid URL to your file. Try that: (it is from an iOS project, but should apply to OS X apps as well). I'm using loadHTMLString:baseURL: instead of loadRequest:
NSString* fileName = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:NULL];
NSURL *baseURL = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
[NSURL URLWithString:#"test.html"]
There's your problem. Assuming that your test.html file is in the resource folder of your application bundle, do this instead:
[[NSBundle mainBundle] URLForResource:#"test" withExtension:#"html"];
NSBundle has a few other methods for easily creating URLs in case the one above isn't appropriate (like if the file is in a subdirectory). Take a look at the NSBundle reference page for more info.

AVAudioPlayer doesn't work with (NSString *)filename

I want to implement a player that plays sounds and/or music. I have these two methods:
-(void)playSound:(int)soundIndex
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[SoundFxFilenameArray objectAtIndex:soundIndex] ofType:#"mp3"]];
if(FxPlayer)
{
[FxPlayer release];
FxPlayer = nil;
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
FxPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[FxPlayer play];
}
-(void)playMusic:(NSString *)filename
{
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:filename ofType:#"mp3"]];
if(MusicPlayer)
{
[MusicPlayer release];
MusicPlayer = nil;
}
MusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[MusicPlayer play];
}
Now, [[SoundPlayer sharedManager] playSound:FXIndex_Default] works just fine. When I try to play music though, [[SoundPlayer sharedManager] playMusic:#"music_file_name_here" just doesn't do anything. Note that both the sound file and the music file are of the same format (mp3).
Also, the "sharedInstance" is because I implemented the singleton pattern for this audio player.
Any pointers or ideas on what is so wrong in my code so that I can play sounds but not music?
Use fileURLWithPath instead of URLWithString in playMusic:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filename ofType:#"mp3"]];
URLWithString expects a complete URL such as "file://path/to/file.mp3" as argument, fileURLWithPath expects just a file path.

Imbedded html file - what is directory?

This is my code:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *indexPath = [NSBundle pathForResource:#"iHelp" ofType:#"html" inDirectory:nil];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
}
The app is crashing because 'nil' for directory name is invalid (DUH!). So for an imbedded html file, what should go there?
UPDATE: found that "webView" has no outlet, although one is defined in the .h file... will close this because I believe there is an answer here. Will open a new question re: the outlets... thanks everybody!
If the file iHelp is in the main bundle, you can retrieve its path like this:
NSString *indexPath = [[NSBundle mainBundle] pathForResource:#"iHelp" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
You can also do that like the following:
NSURL *indexURL = [[NSBundle mainBundle] URLForResource:#"iHelp" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:indexURL]];
Drop the inDirectory: parameter as it will default to your current bundle. Note: it's an instance method.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- (void)viewDidLoad
{
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSString *indexPath = [bundle pathForResource:#"iHelp" ofType:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
If it isn't inside a folder reference, just omit that argument:
[[NSBundle mainBundle] pathForResource:#"iHelp" ofType:#"html"];
You can even go straight to URL:
[[NSBundle mainBundle] URLForResource:#"iHelp" withExtension:#"html"];

UIWebView doesn't load content

I don't understand why if I load the content of a UIWebView in XCode this way:
NSString *string = #"http://www.dummyurl.org/dummy.pdf";
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
everything works fine, but if a build the original string from a php script I wrote:
NSString *strURL = [NSSTring stringWithFormat:#"www.myserver.php"];
NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
nothing works. I checked my script and it returns EXACTLY the original string (http://www.dummyurl.org/dummy.pdf) that works fine with the first method.
I build from a PHP script the content of a UITextView too, but that works fine.
I don't understand why this method works with the UITextView but not with the UIWebView to load the .pdf.
It may be useful
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *tempString = [string stringByTrimmingCharactersInSet:characterSet];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
Make sure you linked the webview (IBOutlet) and the delegate.
With this lines it should load a url:
webView.delegate = self;
NSURLRequest *urlRequest;
NSString *urlToOpen = [NSString stringWithFormat:#"http://www.stackoverflow.com"];
urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlToOpen]];
[self.webView loadRequest:urlRequest];
Hope this helps...
I had this and it was a cache problem:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];