I am using below code
// it is showing nil for path
NSString *path = [[NSBundle mainBundle] pathForResource:#"filetext.doc" ofType:nil];
NSURL *url1 = [NSURL fileURLWithPath:path];
// it is showing working
NSString *path = [[NSBundle mainBundle] pathForResource:#"filetext.txt" ofType:nil];
NSURL *url1 = [NSURL fileURLWithPath:path];
Please help what is the issue
According to the docs, the method returns nil if the file cannot be found. The conclusion, therefore, is that the file is missing. Check your project's Build Phases/Copy Bundle Resources to ensure that the file is actually present because in all likelihood it is not.
I have been able to read a file called 'exerciseA.htm' from a live url. but now i need to bring the file locally add it to the project and read it from there. I have dragged the file into xcode to add it. Now i need to read its contents to a string, however the following doesnt seem to work:
NSString* exerciseAPage = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"exerciseA" ofType:#"htm"] encoding:NSUTF8StringEncoding error:&error];
[exerciseWebViewA loadHTMLString:exerciseAPage baseURL:nil];
Is this the correct way to load local htm file to a webview?
I do it the following way (Which is basically the same as you specify):
NSURL *url = [[NSBundle mainBundle] URLForResource:nameOfHtmlFile withExtension:#"html"];
NSError *error;
NSString *contentString = [NSString stringWithContentsOfURL:url encoding:NSStringEncodingConversionAllowLossy error:&error];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:contentString baseURL:baseUrl];
Hope this helps
Alex
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.
I'm working on an iOS application that needs to play some sounds using the AVFoundation framework. The workspace structure in Xcode 4 contains two projects:
Workspace
The application itself (main project)
A utility library
After building the utility library, it results in a static library which is used in the main application as a framework.
So, when trying to play a sound inside the main application by using the code below, it works as expected.
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *path = [NSString stringWithFormat:#"%#/sound.mp3", resourcePath];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&error];
[audioPlayer play];
In contrast, when trying to play exactly the same sound (or any other) inside the utility library using the same code as above, no sound is played at all, even though error is nil and the audioPlayer property values are the right ones (number of channels, duration).
I've made sure the AVFoundation framework is in both projects.
Also, my class uses the AVAudioPlayerDelegate protocol and implements these two methods:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;
None of these methods is called after trying to play the sound.
If I use the AudioToolbox framework instead, then it plays the sound. But I'm interested in using AVFoundation for several reasons.
Any idea of what is going on? Am I missing something about AVFoundation? Could it be related to using AVAudioPlayer from inside a static library?
Thanks in advance.
I found the solution and it's related to something I didn't mention and didn't think about: I'm compiling with Automatic Reference Counting (ARC).
ARC inserts a release call to the audio player, so it's deallocated right after leaving the method where it is created. It has nothing to do with AVAudioPlayer but with ARC.
In order to solve this issue, I just created an AVAudioPlayer attribute to the class that deals with sounds so that it is not released anymore by ARC. Well, at least, it's not released until the instance of this class is released.
For more information about ARC, refer to Automatic Reference Counting: Zeroing Weak References with details on why I had this issue.
Yes, absolutely; ARC was the reason with my code as well.
I introduced a property:
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
and that made everything work out fine.
use this it will work definitely....
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSURL *url = [NSURL fileURLWithPath:#"path of the sound file :)"];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_player setDelegate:self];
[_player prepareToPlay];
[_player play];
Have you configured the AVAudioSession?
I had a similar problem and fixed it using something like this:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
or
[audioSession setCategory:AVAudioSessionCategoryPlayback error:NULL];
Hope it helps.
Even having audioPlayer var declared in the class scope, it won't play.
At least on iOS 10 iPhone 5c physical device.
play() result is true so no errors appear to happen.
Had to add this (Swift 3, 4) and now it plays the sound correctly.
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayback)
I have created open source project for simple audio player. Take a look at it if you have any problems with playing audio in iOS (in background too) : https://github.com/wzbozon/DKAudioPlayer
I had the same issue. I had resolved it by adding below line in .h file:
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
And in .m file :
NSError *error;
NSString *soundFilePath = [NSString stringWithFormat:#"%#/dancepechance.mp3",
[[NSBundle mainBundle] resourcePath]];
NSURL *url = [NSURL fileURLWithPath:soundFilePath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
NSLog(#"Error %#",error);
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
You can use AVPlayerViewController from AVKit instead for both audio and video. Code is available for both swift and objective c here.
for me this did the job
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
} catch {
assertionFailure("Failed to configure `AVAAudioSession`: \(error.localizedDescription)")
}
Please stay on the page for some time, I was facing same issue. and use sleep to resolve it.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"sounds-1027-are-you-kidding"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
sleep(2);
I have some mp3 files in my resources folder, how can i play those mp3 files? I just know their name.
Just to supplement #Georg's answer.
As you can see from the doc, -URLForResource:withExtension: is available only since 4.0. As 3.x is still widespread, it's better to use the backward-compatible methods:
By converting a path to NSURL:
NSString* path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"mp3"];
NSURL* url = [NSURL fileURLWithPath:path];
....
or, by CoreFoundation methods:
CFURLRef url = CFBundleCopyResourceURL(CFBundleGetMain(),
CFSTR("myFile"), CFSTR("mp3"), NULL);
....
CFRelease(url);
You can use the AVAudioPlayer. To get the URL for the files use NSBundles -URLForResource:withExtension: (or downward compatible alternatives, see Kennys answer):
NSURL *url = [[NSBundle mainBundle] URLForResource:#"myFile" withExtension:#"mp3"];
NSError *error = 0;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
// check error ...
[player play];
// ...
And don't forget to read the Multimedia Programming Guides section on using audio.