Sounds lost after interruption - iphone

I'm using the SoundEngine provided with Apple's crash landing example.
After an interruption such as an incoming phone call or an alarm I call to applicationWillResignActive: inside my delegate, in order to pause the game and save the state of it. After the interruption ends I return to my game but the sound is gone. Even if i reinitialize it with SoundEngine_Initialize() the game still wont reproduce any of the sounds, unless i restart my app.
How can I restore my game sounds after the interruption ?

Have you looked at the sound manager class used by 71squared ? You can likely just use their sound manager as is... but if you wanted to role your own, you can look at thier code as I know they have solved this in their code.
http://www.71squared.com/2010/01/latest-sound-manager-class/

Related

iPhone: MPMusicPlayerController stops AVAudioPlayer

I'm playing a silent music with AVAudioPlayer when user locks the screen, so that my timers won't stop.
However, when I play an iPod music with [MPMusicPlayerController applicationMusicPlayer], AVAudioPlayer stops,without receiving any call back.
Is there any way so that I can start [MPMusicPlayerController applicationMusicPlayer] playing without stoping AVAudioPlayer playing?
EDIT:
Thanks guys, this is the app I'm working on:
It is an Alarm app, this app allows user to lock screen while app is running,and when it is the time of the alarm, app can play iPod music to wake the user.Local notification can not use iPod music as alert sound, so I have to keep the app running while screen is locked.
If user quit the app, it will use local notification as alarm, whose sound is limited to files in bundle.
I can't use UILocalNotification as timer since when in screen locked status(in UIApplicationStatusInactive), app can't receive local notification generated by the system.
Apple has architected their backgrounding system to really limit things like this from happening. Essentially, there is no way for the you to keep the application running in the background unless it needs to be there. If you explain what you are trying to accomplish, maybe a better solution can be found but as good practice, never use random backgrounding methods to do other things. I am assuming that you might be using the faint music as a way to show something custom on the main screen, this is not a good idea.
Your app will get rejected if you play a silent audio.
Also as per apple's documentation https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103, notifications cannot have sounds (soundName) which play more than 30 seconds.
So you wont be able to release your app in the store.
I figured it out myself.
It is not calling [MPMusicPlayerController +applicationMusicPlayer] that stops AVAudioPlayer, but calling [MPMusicPlayerController -setShuffleMode:], I don't know why calling this would stop AVAudioPlayer, but it is where the problem lies in.
Thanks everyone, I think I should paste my complete code next time.

Iphone sdk - How to play a sound during a phone call after some elapsed time?

Well I am having two issues that i can't get to work, related to audio and calls.
The first one is to play a sound during a phone call. I don't want to play continuous music or stream anything, it is just a simple and short sound that the user will hear at one time during his call.
I have read some posts claiming that this is possible, and I even have an application that does so, but I can't get it to work. My app identifies the call using CTCallCenter and print the logs but never plays the sound or plays it after the app comes to foreground again. I have the .plist property of required background mode App plays audio.
The second issue, is to play the sound after some elapsed time. NSTimers doesn't work when on background mode, nor NSThread sleep on my background process or NSOperation. So how could I play this sound after say 10 seconds of the call?
Also, this behavior has to work also when the application is already on background mode. With CTCallCenter I am only getting the event when the application is interrupted from use, but I don't see any logs when i send the app to background and then begin/receive a call
.
If anyone could point me to the right direction I'll be really grateful.
I havent done this, but NSLocalNotifcation, schedule a notification to play when you app get the call to move into the background. I would expect this to work. Interested to find out if it does.

iOS 5 Audio Alarms Don't Sound Without kAudioSessionProperty_OverrideCategoryMixWithOthers On

I have an audio app that is having some problems with the way iOS 5 has changed audio behaviors. When my app's audio is playing (AVAudioSessionCategoryPlayback), and a Clock.app alarm or timer is fired from the OS, the UIAlertView notification pops up, but without the audio alert. My application sound ducks fine to get out of the way of the audio alert, but the alarm app's audio alert does not sound.
Naturally, tons of support requests poured in over the iOS 5 change. I have solved this temporarily by setting kAudioSessionProperty_OverrideCategoryMixWithOthers which lets the alarm audio come through, but there are a few very undesirable side-effects when doing this:
Other app's audio can play with/over mine.
The remote control events are not routed to my app, but to iPod.app.
None of the above drawbacks are acceptable for my app's requirements. I have been hacking away at this for some time now but haven't been able to crack it. How can I setup my audio such that:
My app's audio still uses the AVAudioSessionCategoryPlayback category for background audio.
The Clock.app alarms still have their audio alerts make sound
The app still responds to remote control notifications
After writing this question I went to file a bug report on this. I created a small sample project that I thought would replicate the issue, but I could not replicate it! This caused me to dig in deep once again and try to figure out what was up here…
I fired up an iOS alarm, then I placed a break point in audioPlayerBeginInterruption: and traced through my code line by line in the debugger. I noticed that before my code ran (while I was paused in the debugger), the iOS 5 alarm was sounding! Luckily it still sounded even as I was stepping through my app, so I was able to figure out which pieces of code specifically caused it to stop sounding.
Part of my interruptionHandler is to (obviously) stop the internal audio of my app to let the interruption come through. I never thought to inspect this method before, but turns out the problem existed in there. My stop method would call prepareToPlay immediately after stopping to make resuming faster the next time.
[self.player stop];
[self.player prepareToPlay]; // <- iOS 5 alarm sound stopped here.
The docs state the prepareToPlay method
preloads buffers and acquires the audio hardware needed for playback, which minimizes the lag between calling the play method and the start of sound output.
Sounds reasonable, and this worked for lesser iOS versions. My hypothesis is that  must have made a change to the Clock.app alarm system such that the new alarm sounds use the hardware, whereas before it used the software. This is what I think is causing the iOS 5 alarms to be silent in some apps.
Removing the prepareToPlay lines caused the alarm to sound without using kAudioSessionProperty_OverrideCategoryMixWithOthers, thus solving all my issues laid out in this question.
TL;DR
Remove the prepareToPlay calls from your stop sound code logic. It will take a microsecond longer to start later, but will allow interruptions to sound.

Multitasking in iphone?

I am developing the Game iPhone application, it has Music player and and few animation. The game is interrupted by a text message or call it is terminated. It should pause the game until you click cancel. But While sound plays if i receive the call or message, audio player is pause, after accept/decline call, it doesn't play continue. how to manage this? actualy the
Have a look at the UIApplicationDelegate reference. There you'll see the various methods that are called as events beyond your control occur (such as a phone call coming in, etc.)
In your case I believe you want to override the applicationWillResignActive and applicationDidBecomeActive methods to handle pausing the game, sounds, etc. and restoring them.
UPDATE: I found this blog post very helpful in understanding all the multitasking delegates: http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

UIAccelerometer is Shaking

I want a functionality in which i want to detect if my device is being shaked.The problem is i can detect the shake with didAccelerate method of UIAcceleratorDelegate , but i dont know how to detect if the device is still shaking. I want to play an audio file when the user shakes the device for first time,i have to check if the user is still shaking the device while playing the 1st audio file,if it is still being shaked, then i have to play another file.
See the sample project GLPaint from Apple which was found by visiting http://developer.apple.com/iphone and entering "shake" in the search box. Developer account not required.
You might consider writing a Method that runs in a separate thread that polls wether your device is being shaken every now and then and fire events that you handle somewhere else in your code (or instead of that, handle whatever you want to handle within the threads context itself, even tough i would not recommend doing that).
You just have to make sure, that your "shake-detektor"-thread exits at some point in time, you probably want to do that when the second audio file stopped playing. So your loop could test on that condition.
Hope I could help a bit.