Audio will play in iPhone simulator, but not on device - iphone

Basically, when a button on my app is touched, audio is played. Simple enough, and yet it fails to work on my actual device, but works well in the simulator. When the button is touched, this function is executed:
- (IBAction)playButtonSound
{
SystemSoundID buttonSound;
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"Button_Sound" withExtension: #"caf"];
CFURLRef buttonSoundURLRef = (CFURLRef) [tapSound retain];
AudioServicesCreateSystemSoundID (buttonSoundURLRef, &buttonSound);
AudioServicesPlaySystemSound(buttonSound);
[tapSound release];
}
I'm still an iPhone newbie, so I'm not entirely sure what the cause of this is.

Works on iPhone Simulator but not on device
Make sure you write the correct file names, iOS is case sensitive, simulator is not.

Do you have system sounds disabled on your device? As its name implies, AudioServicesPlaySystemSound will normally not play if the user has system sounds disabled. There is a property kAudioServicesPropertyIsUISound that is supposed to be able to be set to NO to disable this behavior.

Related

How come my .aif short sound file will only play on iOS simulator and not on device?

I'm looking to play a sound on my iPhone (iOS 6.1) depending on pressed buttons.
After converting to aif, caf, wav and compressed formats, I could not get sound on the device. Sound volumes have been checked, device has been rebooted, no uppercase letters are included in filenames.
I read many topics about this problem and could not find a proper solution, which means my problem is either more complex or plain stupid - surely plain stupid.
Here is my code to play the sound:
// Plays sound of given filename
- (void) playSound:(NSString*)name {
SystemSoundID sound = [self createSoundID:name];
AudioServicesPlaySystemSound(sound);
NSLog(#"file played: %#",name);
}
// Creates a playable sound out of filename
-(SystemSoundID) createSoundID:(NSString*) name
{
NSString *path = [NSString stringWithFormat: #"%#/%#",
[[NSBundle mainBundle] resourcePath], name];
NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
return soundID;
}
If the sound works fine on simulator but not in real device that means you should turn on your device's Ringer and Alert Settings. you should also check the mute button, if its' on or not.
The problem was that the method calling playSound was not called with an NSTimer. It seems that for the sounds to play, they must be called by the main thread of the application (which seems legit...).
So I modified the app so that the main thread would call those sounds and voila, it worked on the device as well. The code above was correct.

Iphone SDK: sound problems

i've created a drum app. As of now, the app's kinda satisfying but not great.
The problem is a strange delay or "lag" that sometimes happens when i press my sound buttons. When i spam a button it begins to freeze, when unfreezed again, it plays the same sound stacked on itself about 4 times :(
What am i doing wrong?
Are my samples bad?
Should i use an imageView and touchesBegan to simulate the buttons?
This is the code that i use to play a sound when a button is pushed:
- (IBAction) HHC {
NSString *path = [[NSBundle mainBundle] pathForResource:#"HHClosed"ofType:#"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
}
Please give me some good advice :)
Thanks in advance
-DD
Try doing this and see if it makes your performance better.
In your .h file declare this:
SystemSoundID soundID;
In your .m file in the -viewDidLoad method add this code:
-(void) viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"HHClosed"ofType:#"wav"];
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);
}
And in the method you showed just keep this one line of code:
- (IBAction) HHC {
AudioServicesPlaySystemSound (soundID);
}
Let me know if that makes the freezing going away. If it doesn't try Core Audio
Every time you hit the button you are loading the sound file from the filesystem, bringing it into memory, and then playing it.
This is very inefficient. Your audio samples should already be loaded in memory at launch, so they don't need to be read in from the filesystem. Also, AudioServicesPlaySystemSound isn't really designed for this kind of usage - its main purpose is playing back short alert sounds (ie, a system alert). If you need low-latency audio playback you should be looking at the Core Audio framework instead.
For low latency sound appropriate for a drum app, you will want to use the RemoteIO Audio Unit API, which is not simple. You will need to know how to handle, buffer and mix PCM sound samples. Start by reading a book on digital audio technology.

MPMoviePlayerController problem when play a movie

i put a movie when my application will lunch . just like gameloft games .
so when application has lunched , movie plays fine but before the move plays .. my FirstViewController xib file show first then moview start to play ! why ?
here is my code :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"movie" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
IntroMovie.movieControlMode = MPMovieControlModeHidden;
[IntroMovie play];
}
Your question is a little unclear, but what I think you are asking is why your movie is not playing when the application launches before the user sees the controls and items in your XIB file. If this is correct, then this is the normal behavior as typically the application will launch and only then call the function you have (applicationDidFinishLaunching). There is no guarantee that you will get that function called before the controls have loaded and are viewable on the screen all the time.
It is bad form to start you application with a movie, but if you want to then you have to deal with the fact that there will potentially be a default image or even your controls that you put in your XIB file.
One way to start with a movie, is that you must hide or delete everything in the XIB file so the screen will appear blank when the application loads. Then, when you application calls the applicationDidFinishLaunching function the user will not have seen your application and the first thing they will see is the movie. When the movie is finished playing, you will need to have a notification listener instruct your application to show the controls for your application or switch to another XIB file that has the app controls within it.
I hope this helps.

Make AVFoundation framework not fading when screen fades

I am implementing some audiobooks for iPhone. I used AVFoundation. Something like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"intro" ofType:#"mp3"];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
I have a problem. When the screen goes dark (single audio files can be very long) the audio stops playing.
I solved this problem with this string of code
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// something else here...
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
This does not allow the iPhone to "sleep". However you can guess how this is foolish: your battery level goes down in minutes and this is not possible by audiobooks lasting more than 20 hours, for example...
So, do you know a way to prevent that when the screen sleeps the AVAudioPlayer does not stop playing?
Thanks...
Fabio
Set your audio session to kAudioSessionCategory_MediaPlayback to playback while the screen is locked.
Could I suggest that once you start playing the audio file that you tell the user to press the power button (not home button) which will lock the phone. It will not close you app but it will power off the screen with you application running in the background. Currently several apps do this.

Audio volume stuck at low level and adjusting volume has no effect?

When I use my app (on the device), the actual volume works OK for a while, but after a few days it seems to get 'stuck' at a low level.
Adjusting the volume rocker has no effect - and it shows 'Ringer' text. I've noticed that other people's apps are similarly affected if they show the 'Ringer' text when adjusting the volume. But apps which don't show 'Ringer' text are not affected by this.
How would I remove the 'Ringer' text and get my app to respond properly to different volumes?
I found some code on Apple's forums which fixes it. You have to use the AVFoundation.framework and put a bit of code into your app delegate. And put an audio file called 'blank.aif' into your project. Basically, it's a hack which prepares a file to play, but then never plays it. This fools the system into thinking that sound is being played all the time, which allows the user to use the main volume control.
#import <AVFoundation/AVFoundation.h>
//...
-(void)applicationDidFinishLaunching:(UIApplication *)application {
//volume control hack
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"blanksound" ofType:#"aif"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *volumeHack = [[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]retain];
[fileURL release];
[volumeHack prepareToPlay];
// other applicationDidFinishLaunching stuff
}