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

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.

Related

unable to get the Audio output in iOS

I have a codebase which plays audio and video perfectly. I have converted that into a small framework and started using it as a part of another code. When i do this, there is no audio output.... I am unable to hear any audio output while it is being played. All the Audio player delegate methods are being called but there is no sound. The same is happening with Videos as well. All the videos are being played with out sound. Its like watching a silent movie.... Any ideas? I am using AVAudioPlayer for playing audio files and MPMoviePlayerController for playing videos.
edit : This happens only on device. It works all fine in simulator
Just came across this post, I was doing the same thing, and actually AVAduioSession is needed to play audio thru speaker/headset. So having AVAudioSession is a must.
//REALLY NEED THIS LINE FOR MUSIC TO PLAY TO SPEAKER
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
and for background playing, add these 2 lines and change setting in plist to allow background mode
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
So stupid of me.... The problem was with the volume button on iPad. The volume button in iPad with iOS 5 has some weird bug. It does not update itself when we toggle the button between screen lock and audio output. I guess this is bug from apple and filed a bug. Hope they solve this in their next update.

AVAudioPlayer error loading file

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.

playing a sound doesn't work on iPhone

I've added a sound to my app.
In my .h i've added:
CFURLRef soundFileURL;
SystemSoundID soundFile;
in my viewDidLoad in my .m:
soundFileURL = CFBundleCopyResourceURL(
CFBundleGetMainBundle(),
CFSTR("sound"),
CFSTR("mp3"),
NULL);
AudioServicesCreateSystemSoundID(
soundFileURL,
&soundFile);
and lastly i've added a -playSound method:
-(void)playSound {
NSLog(#"playSound");
AudioServicesPlayAlertSound(soundFile); }
It works fine on the iPhone Simulator, but when I build the app on my iPhone the console says the sound was played but it wasn't.
I read that many others had this problem too but I didn't find any solutions.
What's wrong?
It could be your encoding - the audio codecs on the simulator are much different than those provided by your actual iPhone hardware. Try reencoding.
Also - you don't want to use an mp3 for a sound, you want to use a non-compressed file format (I suggest aiff from experience) because there isn't dedicated decoding hardware to support the sound decoding you're trying to do. (There is for playing music, which mp3 is recommended for)
I would recommend AVAudioPlayer in the AVFoundation framework. It has a simple asynchronous interface for sound playback on iOS.
Check out the Apple programming guide here.
I had a similar issue. It turned out I was referencing "Horn.caf" with
NSURL *hornSound = [[NSBundle mainBundle] URLForResource: #"horn"
withExtension: #"caf"];
Note the difference in case; My OS X Lion install is on a case insensitive file system.
Changing the code to the following fixed the issue.
NSURL *hornSound = [[NSBundle mainBundle] URLForResource: #"Horn"
withExtension: #"caf"];
Hope that helps others.

How to play background audio using AVFoundation for iPhone/iPoad Touch with iOS 4?

I think that it's clear from the title...
The Problem:
I already have some objective-c code that uses AVFoundation and its AVAudioPlayer to play some sounds in my iPhone apps, it takes a path for a folder in the documents and plays all the files in it one after the other using an AVAudioPlayer object (I implemented AVAudionPlayerDelegate so I can give the player the next file when the current one is done)...
The Question:
Now how can I keep these sounds playing when my app is in the background cause I upgraded my devices to iOS 4.1 and also my iOS SDK and Xcode...
What I did:
I added the "UIBackgroundModes" to my info.plist file and put "audio" in it and I set the category for the sharedInstance in AVAudionSeession to AVAudioSessionCategoryPlayback and setActive to YES and no errors happened there but still when I press the home button, the music stops!
What else should I do?
Thanks in advance
I had this problem,and solved this way: I added UIBackgroundModes and put audio there. Then in my AudioPlayer class (class where i play my audio) in "init" method i've put this code:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
NOTE: In simulator this will not work! You need real device.

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