AVAudioPlayer error loading file - iphone

I am trying to use the AVAudioPlayer to play a simple sound and I keep getting this error in the console:
2011-09-02 20:29:07.369 MusicLog[967:10103] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn: dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: ___CFObjCIsCollectable
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
Here is my code that should play a sound when my button is pressed:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"tick" ofType:#"caf"];
NSError *error;
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *tickPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[tickPlayer setDelegate:self];
[tickPlayer setNumberOfLoops:0];
[tickPlayer prepareToPlay];
if (tickPlayer == nil)
{
NSLog(#"%#", [error description]);
}
else
{
[tickPlayer play];
}
Do I somehow have the file in the wrong place? I just dragged the file into the project and selected copy if needed.
EDIT: After more testing I found that I do not get this error when running IOS 4.2 simulator, only on the IOS 5 simulator. Can I assume that this is a bug in iOS 5?
Thanks,

I've had the same problem. It only seems to be a problem on iOS5 in simulator. I haven't tested on an iOS 5 device yet. I keep hoping XCode upgrades will make the error go away, but so far no luck.

I've had the same error, but the sound played nonetheless after I keep the variable as a class attribute. If the code that you put here is written inside a method, try moving this variable. I guess the sound didn't play because the audioPlayer instance is lost after the method went out of scope.
#implementation SomeAudioController {
AVAudioPlayer *tickPlayer;
}

Make your AVAudioPlayer a property w/ a strong reference.
Source: AVAudioPlayer refuses to play anything, but no error, etc
The above answer helped me to play the .wav file in my simulator, but I haven't gotten it to work on my device yet. Something about missing codecs..?
Edit:
For the audio to play on the device, I also had to add the following line to AppDelegate.m's didFinishLaunchingWithOptions:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Try to check if you have enabled the "All Exceptions" breakpoint, which will result in the debugger breaks at the exception instead. Just disable this feature and built it again.

I also had the same type of error when I tried to play some mp3 files on button click and compiled to iOS5. The error disappeared when I compiled to iOS4 but the sound still doesn't come out. There was no runtime error and the NSLogs printed properly in the debug console. Did you find a way to play your mp3 files?

From Multimedia Programming Guide:
Preferred Audio Formats in iOS For uncompressed (highest quality)
audio, use 16-bit, little endian, linear PCM audio data packaged in a
CAF file. You can convert an audio file to this format in Mac OS X
using the afconvert command-line tool, as shown here:
/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT}
The afconvert
tool lets you convert to a wide range of audio data formats and file
types. See the afconvert man page, and enter afconvert -h at a shell
prompt, for more information.
For compressed audio when playing one sound at a time, and when you
don’t need to play audio simultaneously with the iPod application, use
the AAC format packaged in a CAF or m4a file.
For less memory usage when you need to play multiple sounds
simultaneously, use IMA4 (IMA/ADPCM) compression. This reduces file
size but entails minimal CPU impact during decompression. As with
linear PCM data, package IMA4 data in a CAF file.

Related

How can I make safe mp3 files for AVAudioPlayer, without getting errors

I have problem when trying to play some mp3 files on iOS app.
First, I noticed that these files was also not working on Safari for iOS, but now I am having files that works on Safari for iOS, and doesn't work on my App.
I am using thi code to play mp3 audio files:
NSString *pronociationPath = …..
if (pronociationPath != nil)
{
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:pronociationPath];
NSError *error;
self.player=[[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error] autorelease];
[fileURL release];
[self.player setNumberOfLoops:0];
if (self.player == nil) {
NSLog(#"%#",error);
}
else
{
[self.player prepareToPlay];
[self.player play];
}
}
And I am having this error for a file that works on Safari iOS:
Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation
couldn’t be completed. (OSStatus error 1685348671.)"
Is something wrong with my code?
Is there any guide to create safe mp3 files for iOS Apps ?
I've seen Apple documentation, but I don't see any useful informations https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW28
I also received same error (OSStatus error 1685348671) when I tried to play mp3 files on iOS 4.3.
Later on I found that it was the mp3 sound problem which made it incompatible to be played in iOS 4.3. So, I used iTunes Player to convert new version of mp3 sound with existing one. Now it works fine and better.
Steps:
Open your current mp3 sound in iTunes
Select it and Click on "Advanced" Menu >> "Create MP3 Version"
It wil create copy of current audio. Use newly created mp3 file for your app.
It helped me. I hope it might help others as well. All the best!

AVAudioPlayer works fine on iPhone, but doesn't play on iPad?

Greetings,
I have an app were the sounds play find on iPhone but on iPad..no sound at all.
Here's a code snippet:
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:filePath] error:nil];
[self.player play];
Any hints as to why no sound on iPad?
Other apps on the same iPad play sounds fine.
And/or ways to track down the error?
O.k. My mistake.
I have 4.2 on my iPad, where the orientation switch now is the mute switch.
However, the iPod application doesn't obey the mute switch.
Thanks for the suggestions.
Have you made sure that the NSURL method call returns a non-nil result?
I had same problem while I was deploying to ios 3.x.
iPhone 4 & iPad
worked fine with my .wav file
iTouch
didn't worked at all through my ear phone
Here is good way to check and fix your problem
1) Check the file location. (NSURL)
2) Convert into Apple's recommend '.caf' sound format.
Finder->Type 'Terminal'-> Inside terminal Go to the where .wav file locate->Type 'afconvert -f caff -d LEI16#11025 inputfile.wav outfile.caf
3) Maybe you didn't import AudioToolBox framework library.

Sound on simulator but not device

I'm using the following to play an m4a file:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: fileName];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
It works fine on the simulator but I hear nothing on the device. Sounds files I'm using all stay in the bundle. Here is what filePath looks like from the device:
file://localhost/var/mobile/Applications/418945F3-3711-4B4D-BC65-0D78993C77FB/African%20Adventure.app/Switch%201.m4a
Is there an issue with the file path or any thing different I need to do for the device?
Just as a sidenote - I was having the exact same problem and spent probably close to an hour on converting files to the correct format, etc.. Yet the problem was the "mute" switch on the iPad. So even though the volume was up, and I could hear other sounds on the iPad, because the mute switch was turned on, it wasn't playing system sounds.
To add to the confusion, this app uses text-to-speech and the volume coming from the dictation was perfectly fine, it was only the sounds coming from AudioServicesPlaySystemSound() that weren't being played.
I had trouble with this too. Finally I realised it was because AudioServices can only play audio with the following constratints.
Sound files that you play using this
function must be:
- No longer than 30 seconds in duration
- In linear PCM or IMA4 (IMA/ADPCM) format
- Packaged in a .caf, .aif, or .wav file
From Apple docs: http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html
You might want to use the AVAudioPlayer instead of AudioServices.
The following code will take an audio file (.m4a) and play the audio file 1 time. Don't forget to release "audioPlayer" when you're done with it.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"m4a"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
{
NSLog([error description]);
}
else
{
[audioPlayer play];
}
Hope this example helps you with playing audio on the actual device. It might also be a good idea to increase the device audio when the file is playing.
Note: You will need to add the AVFoundation framework to your project if you have not already done so. As well as import the header file.
#import <AVFoundation/AVFoundation.h>
Update:
From Apple's Core Audio Overview Document
Audio Session Services
Audio Session Services lets you manage audio sessions in your application—coordinating the audio behavior in your application with background applications on an iPhone or iPod touch. Audio Session Services consists of a subset of the functions, data types, and constants declared in the AudioServices.h header file in AudioToolbox.framework.
The AVAudioPlayer Class
The AVAudioPlayer class provides a simple Objective-C interface for playing sounds. If your application does not require stereo positioning or precise synchronization, and if you are not playing audio captured from a network stream, Apple recommends that you use this class for playback. This class is declared in the AVAudioPlayer.h header file in AVFoundation.framework.
Start by error-checking your returns. Is filePath nil? Do either of the AudioServices functions return an error? The most likely cause is case-sensitivity. The iPhone filesystem is case sensitive while the Mac is not. But the first step in debugging is to look at the errors the system is providing.
The simulator uses regular QuickTime for playback, so it's easy to have media assets which work in the sim, but fail on the device due to missing / unsupported codecs. The test is if you can play the file at all on the device, eg through Safari or the iPod app.

Multiple audio sounds in iPhone app?

I've gotten to play a single sound in the iPhone app I've started, but I now desire to play multiple sounds based on a button. To create a separate set of .m and .h files for each audio sounds, and then including them all, doesn't seem the most efficient way to tackle this in terms of coding...then again, I'm just starting out with Cocoa and only just completed my first app ever.
Any help is appreciated. The key here is multiple sounds, each triggered by its own button or image. Thanks.
If the files are MP3 or AAC format, then you can only play one at a time. This is a limitation of core audio on the iPhone.
In terms of playing multiple sounds at once, that's easy, just create a new player instance for every one that you want to play (and remember to release them when you're done)
NSString *path = [[NSBundle mainBundle] pathForResource:#"dream" ofType:#"m4a"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
To convert an MP3 into something like IMA4 (which you can play more than one at once) you would run the following (in terminal, in leopard):
/usr/bin/afconvert -f caff -d ima4 sound.mp3 sound.caf
The above code is firmware 2.2 only, but you can do the same with the AudioQueue files if you want to support older firmwares (it's just a lot more complex, so I haven't listed the code here).
If you have access to the iPhone developer network, then there are a bunch of code samples that show you how to play audio.
All you need to do is make one class that has a function called
-(void)Play:(NSString*)sSoundFile {
// play sound file here
}
I don't have any direct experience with iPhone development, but in theory could you create a single large sound file with all your sounds in it? Each sound could be separated by just enough silence to be individually accessed by a time index. Of course, the downside is that you'd have to load the entire file into memory, unless you could figure out a way to load chunks in a random-access fashion...
#rustyshelf - does this work in the simulator? I'm using that code, and using #import but neither audioPlayerDidFinishPlaying nor audioPlayerDecodeErrorDidOccur ever get called. Very annoying to try to debug.
http://www.icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/
in this example in ios5 you have to put
CFURLRef soundurl=(__bridge CFURLRef)[NSURL fileURLWithPath:objstring];
in place of
CFURLRef soundurl=(CFURLRef)[NSURL fileURLWithPath:objstring];
Hardik Mamtora

Trying to play sound through iPhone Simulator

I'm trying to play a sound file from an iPhone program.
Here's the code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"play" ofType:#"caf"];
NSFileHandle *bodyf = [NSFileHandle fileHandleForReadingAtPath:path];
NSData *body = [bodyf availableData];
NSLog( #"length of play.caf %d",[body length] );
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSLog( [url description] );
NSLog( #"%d", AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID) );
The first NSLog is to check that I have access to the file (I did), the second NSLog is to show the file URL, and the third NSLog returns -1500 "An unspecified error has occurred."
For the second NSLog, I get the following output:
file://localhost/Users/alan/Library/Application 敲慬楴敶瑓楲杮upport/iPhone蒠ꁻތĀ⾅獕牥⽳污湡䰯扩慲祲䄯灰楬慣楴湯匠灵潰瑲椯桐湯⁥楓畭慬潴⽲獕牥䄯灰楬慣楴湯⽳䙂㕅㡂㤱䌭䐳ⴸ䐴䙃㠭㍃ⴷ䍁㈶㠵䙁㤴㈰䰯捯瑡䵥⹥灡⽰汰祡挮晡imulator/User/Applications/BFE5B819-C3D8-4DCF-8C37-AC6258AF4902/LocateMe.app/play.caf
This is either due to my misunderstanding of the "description" method, or this contributes to the problem.
Any idea what is going wrong?
The first parameter to NSLog is a format string; you're passing [URL description] as the format string to the second use of NSLog. That's bad, because if the description of the URL contains any % characters then it will wind up printing random stuff from the stack.
Instead, write
NSLog(#"%#", URL);
You don't need to even use -description here; NSLog will invoke it for you automatically because %# means "an object," not "an NSString," and it's smart enough to do the right thing for you.
In addition, it seems that the iPhone simulator does not like to play (systemSound) sounds over 5 seconds in length. I had the same issue as out apps almost always play a start up systemSound.
One of the apps I was working on the simulator would not play the sound but the device would. Later I found that there is some strange limitation on the simulator and the way it handles sounds. I've filed a bug with Apple. Hopefully this will be address with the release of the iPhone SDK 3.0.
An update on this.
I got my keys to allow me to run my app on the device.
I found that my .caf file successfully played when the app ran on the device.
I then tried various alternatives.
A .wav file that is 60 seconds long failed to work on the simulator and the device.
A .wav file that is 5 seconds long would work on the simulator, but not on the device.
A .aiff file that is 5 seconds long works on the simulator and the device.
It would be good to know definitively what the simulator and the device are checking about the file when it is passed to AudioServicesCreateSystemSoundID
I tried the same with my application that plays sounds just fine. The sounds don’t play in the Simulator and when I try to NSLog the URL, I get the same garbage as You and EXC_BAD_ACCESS on the top of it. When I log the URL on the device, the URL is fine, but I get EXC_BAD_ACCESS nevertheless. When I drop the logging, sounds play and everything works. If somebody could explain this behaviour I’d be grateful. As for Your problem, I’d drop the logging and try the code on the device.
Use "AVAudioPlayer" class for playing .caf files