Play a .wav with Audio Toolbox - iphone

I am trying to play a .wav file using Audio Toolbox. I have this so far:
However my file will not play. The If condition is triggered right, however it doesn't play.
It will print the error message if the file isn't there however. Any clue what is wrong?
-(void) playSound : (NSString *) fName : (NSString *) ext
{
SystemSoundID audioEffect;
NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
if ([[NSFileManager defaultManager] fileExistsAtPath : path])
{
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else
{
NSLog(#"error, file not found: %#", path);
}
AudioServicesDisposeSystemSoundID(audioEffect);;
}

You can't dispose of the sound ID immediately after telling it to play, since this causes the playback to stop. That's why you didn't hear it in the first place.
What you should do instead is hold a reference to the ID in a property on whatever object is responsible for creating it (e.g., a view controller). You'd declare it like this in your interface declaration:
#property(nonatomic, assign) SystemSoundID audioEffect;
...and subsequently assign to it like this:
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &_audioEffect);
This will not only allow you to properly dispose of it at a later time when you know you're done with it (e.g. in viewDidUnload), but will also prevent you from having to read the file in from disk each time you want to play it back, since each ID can be played an unlimited number of times.
One final hint: you can ask for the URL to a bundle resource directly instead of requesting a string and then transforming it to a URL.
NSURL *audioURL = [[NSBundle mainBundle] URLForResource:fName withExtension:ext];
if([[NSFileManager defaultManager] fileExistsAtPath:[audioURL path]])
// ...

I figured it out from Warrenm's comment. I had to delete the last line, as it was killing the sound milliseconds after it was set to play, and therefore can't be heard.
-(void) playSound : (NSString *) fName : (NSString *) ext
{
SystemSoundID audioEffect;
NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
if ([[NSFileManager defaultManager] fileExistsAtPath : path])
{
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else
{
NSLog(#"error, file not found: %#", path);
}
}

Related

How to create a NSMutableDictionary using dictionaryWithContentsOfFile: method

I want create and return a dictionary using the keys and values found in a file specified by a given path. I have my file on my Desktop:ciudades.txt (a human readable file!!! no a xml, just for practice). What method of my NSString i need to use and how? Please can somebody help me filling on my code the XXXXXXX. Thanks in advance
- (NSMutableDictionary)ciudades
{
if (!ciudades) {
NSString *path = [NSString XXXXXXXXX];
ciudades = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Define a function.
-(NSMutableDictionary*) ReadFileAsDictionaryForPath:(NSString* ) path
{
return [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Use it as below
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSMutableDictionary* myDictionary = [self ReadFileAsDictionaryForPath:path];
if(!myDictionary)
{
NSLog(#"Error while reading data from file at path:%#",path);
}
First add that file to your application bundle By adding a existing file from xcode to your project. Then use this method to get the file path for example I'm getting a image's path.
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
then try the dictionaryWithContentsOfFile method and see if it works or not.

AVAudioPlayer -> not working

Audio Player works fine if I initiate it for 20 to 30 times but after that it shows the following error
Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"
The code currently I am using is as follows
- (void)playShortSound:(NSString *)fileName type:(NSString *)type
{
if (!isSound) {
return;
}
NSString *soundFilePath =[[NSBundle mainBundle] pathForResource: fileName ofType: type];
NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: soundFilePath] autorelease];
NSError *error;
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: &error];
//newPlayer.numberOfLoops = 0;
newPlayer.volume=1.0;
if (newPlayer == nil){
NSLog(#"%#",[error description]);
//[self playShortSound:fileName type:type];
//return;
}
else{
[newPlayer play];
newPlayer.delegate=self;
}
}
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) completed {
if (completed == YES) {
NSLog(#"Sound Playing complete");
}else {
NSLog(#"error in completion of %#",player.url);
}
}
I was having the same problem and instead of init the player from url i did it from NSData.
NSString *soundFilePath =[[NSBundle mainBundle] pathForResource: fileName ofType: type];
NSData *soundData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *error;
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithData: soundData
error: &error];
It seem to solved my problem hope it does yours.
By calling the method many times you are allocating the instance of AVAudioPlayer newPlayer many times. This will certainly cause you problems because of the memory allocated. Just make sure you release the objects that you own once you are done using them.
Did you set AudioSessionCategory?
{NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory: avaudiosessioncategoryplayandrecorderror: &setCategoryError];
}
I was trying to play files in my app by picking them from the iPod library when I got this error code. I found that in my iPod library on the device, I had a duplicate entry for the same song. Music player that comes with iOS was able to play one and not the other. That means one entry was not valid anyway. Probably removed from the device but iTunes/iOS did not do proper housekeeping.
It seems that the reference to the non-playable entry existed within the "Recently Added" playlist which is automatically created by the system. I had picked the same file from "Recently Added" playlist to play in the app.
Side Note: I had removed the file from the manually created playlist in which it existed (removed from the iOS device itself).
In short it seems that the file that I loaded into the AVAudioPlayer instance was not available on the device anymore or its referencing URL did not give back a proper file to play. So common reason for this error would be when the player does not get the file to load based on the URL given to it.

iPhone: AVAudioPlayer unsupported file type

My app downloads an mp3 from our server and plays it back to the user. The file is 64 kbps (which is well within the acceptable range for iPhone if I understand correctly). I have looked up how to do this on dozens of sites and they all suggest that i do exactly this:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://.../file.mp3"]];
NSError *e = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error&e];
[player setDelegate:self];
When I run the code, player comes back null and I get this error:
2011-02-04 10:44:46.176 MyApp[6052:207] Error loading audio: Error Domain=NSOSStatusErrorDomain Code=1954115647 "The operation couldn’t be completed. (OSStatus error 1954115647.)"
2011-02-04 10:44:49.647 MyApp[6052:207] unsupported file type
I have checked the file and I know that it works. It will play with no problems on Windows Media Player, and Quicktime on mac. I have also uploaded the file to the iPhone emulator and it plays with no problems whatsoever. The file is fine, but for some reason AVAudioPlayer doesn't like it.
Is there something I need to change? Is there some kind of setting for NSData to specify what kind of file it is? Does anyone have any idea?
At long last i have found a solution to this problem! Instead of initializing the audio player with the NSData object, I saved the file to the Documents folder, and then initialized the player with the file URL
//download file and play from disk
NSData *audioData = [NSData dataWithContentsOfURL:someURL];
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#.mp3", docDirPath , fileName];
[audioData writeToFile:filePath atomically:YES];
NSError *error;
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (player == nil) {
NSLog(#"AudioPlayer did not load properly: %#", [error description]);
} else {
[player play];
}
When the app is done with the file, it can be deleted. Hope that his helps more than just me!
We were dealing with the same issue - we had to write the file to disk first. What I found eventually was that the data we were downloading had extra, blank bytes at the beginning. When reading from disk using initWithContentsOfURL, AVAudioPlayer knew how to deal with this, but when loading from NSData using initWithData, it did not. Trimming those bytes fixed it.
From iOS 7 AVAudioPlayer has new initializer
NSError *error;
[[AVAudioPlayer alloc] initWithData:soundData fileTypeHint:AVFileTypeMPEGLayer3 error:&error]
The supported UTIs
NSString *const AVFileType3GPP;
NSString *const AVFileType3GPP2;
NSString *const AVFileTypeAIFC;
NSString *const AVFileTypeAIFF;
NSString *const AVFileTypeAMR;
NSString *const AVFileTypeAC3;
NSString *const AVFileTypeMPEGLayer3;
NSString *const AVFileTypeSunAU;
NSString *const AVFileTypeCoreAudioFormat;
NSString *const AVFileTypeAppleM4V;
NSString *const AVFileTypeMPEG4;
NSString *const AVFileTypeAppleM4A;
NSString *const AVFileTypeQuickTimeMovie;
NSString *const AVFileTypeWAVE;
If your product name contains space, you will receive the error. My project's product name was: Panorama 1453 TR. initWithContentsOfURL method cannot fetch file path. so it was not working.
you can put a breakPoint to NSURL *fileURL = . After a step you can see fileURL what data has.
I changed to Panorama1453TR , it did work.

iPhone App - WAV sound files don't play

I searched on here and tried out all the different solutions given, but nothing worked. So let me ask:
I'm trying to play a sound on an iPhone app when a button is pressed. I imported the Audio framework, hooked up the button with the method, have the WAV sound file in the bundle, and use the following code to play the sound:
NSString *path = [NSString stringWithFormat:#"%#%#", [[NSBundle mainBundle] resourcePath], #"/filename.wav"];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
But it doesn't play sound when I press a button. (Yes, my sound is on.)
Any ideas why this might be, and how I can fix it? I'd be happy to provide any more information if it helps.
First of all, the preferred sound format for iPhone is LE formatted CAF, or mp3 for music. You can convert a wav to caf with the built in terminal utility:
afconvert -f caff -d LEI16 crash.wav crash.caf
Then the easiest away to play a sound is to use the AVAudioPlayer... this quick function can help you load a sound resource:
- (AVAudioPlayer *) soundNamed:(NSString *)name {
NSString * path;
AVAudioPlayer * snd;
NSError * err;
path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL * url = [NSURL fileURLWithPath:path];
snd = [[[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&err] autorelease];
if (! snd) {
NSLog(#"Sound named '%#' had error %#", name, [err localizedDescription]);
} else {
[snd prepareToPlay];
}
} else {
NSLog(#"Sound file '%#' doesn't exist at '%#'", name, path);
}
return snd;
}
I don't know if you have solved the problem, but for me, on iOS 7, sometimes the (__bridge CFURLRef) casting won't work, in which case, if you print out the result of (__bridge CFURLRef)[NSURL fileURLWithPath:path], it will be 0x0. This leads to failure of AudioServicesCreateSystemSoundID(). The return value of the function is type of OSStatus. If it fails, the function will return -50, which means one or more parameters are invalid. (If it executes successfully, it returns 0.)
I solved this by using the C style function getting the path of the file:
CFBundleRef mainBundle = CFBundleGetMainBundle ();
CFURLRef soundFileUrl;
soundFileUrl = CFBundleCopyResourceURL(mainBundle, CFSTR("fileName"), CFSTR("wav"), NULL);
AudioServicesCreateSystemSoundID(soundFileUrl, &soundID);

playing a sound, waiting it to finish playing and continue (iphone)

i'm new with the iphone and objective c, so far, I have been able to code some small examples.
i would like to play a sound and, continue with rest of the code when the sample finishes playing, i.e:
printf("hello");
playASound["file.wav"];
printf("world");
actually i'm getting: print hello, play the file and print world at the same time
but
what i want is: print hello, play the file, print world...
so, the question is how do i get it?
thanks
btw. here is the playASound code:
-(void) playASound: (NSString *) file {
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
file];
SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//play the file
AudioServicesPlaySystemSound(soundID);
}
From the documentation:
Discussion This function plays a short
sound (30 seconds or less in
duration). Because sound might play
for several seconds, this function is
executed asynchronously. To know when
a sound has finished playing, call the
AudioServicesAddSystemSoundCompletion
function to register a callback
function.
So you'll need to break up your function into two pieces: a function that calls PlayASound and prints "Hello", and a function that is called by the system when the sound is finished playing and prints "World".
// Change PlayASound to return the SystemSoundID it created
-(SystemSoundID) playASound: (NSString *) file {
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
file];
SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//play the file
AudioServicesPlaySystemSound(soundID);
return soundID;
}
-(void)startSound
{
printf("Hello");
SystemSoundID id = [self playASound:#"file.wav"];
AudioServicesAddSystemSoundCompletion (
id,
NULL,
NULL,
endSound,
NULL
);
}
void endSound (
SystemSoundID ssID,
void *clientData
)
{
printf("world\n");
}
See also docs for AudioServicesAddSystemSoundCompletion and AudioServicesSystemSoundCompletionProc.