background music stops after 3/4 runs in iphone app - iphone

I am playing sounds in loop in my app. So it should continue playing through out the app. but sometimes it stops after playing sound for 3/4 times.I don't understand whats happening.
I am using audio-toolbox framework for playing sound. creating audio queue and then playing sounds in loop. I am also playing sound from ipod library using mediaplayer. Same thing happening with song from ipod.
I have set [musicPlayer setRepeatMode: MPMusicRepeatModeOne]; but still it stops after 3/4 times.

How exactly are you playing the sound? Show us the code. Do you use System Sound Services? They are not meant to play longer sounds, quote Audio Toolbox Reference:
You can use System Sound Services to
play short (30 seconds or less)
sounds.
This could be the source of your problem. But until you show us the code we can’t but guess.

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.

can iOS AudioServicesPlaySystemSound be blocked until sound done playing?

I need my software to wait until the sound is completely played before proceeding.
I've got a lot of sounds to develop, so using the callback method will take a lot of development time.
Is there a way to call AudioServicesPlaySystemSound and have it block until it completes playing, without using the callback method?
You can't - you have actually no exact control of when your sound exactly start and finish. Also not that the sound is played in a background thread for the AudioServicesPlaySystemSound API. I have read this article recently for playing sound on iOS take a look at it. You might want to use an other API for playing your sound.

AVAudioPlayer lag when calling play

I have set up an AVAudioPlayer object in viewDidLoad, as per the Apple guidelines, calling prepareToPlay as the last line in viewDidLoad (i have tried in awakeFromNib also).
When i press my play button, there is a pause, as it would appear to load the file, then it plays.
In audioPlayerDidFinishPlaying, i reload the player with a different sound, and when clicking play for a second time, the file plays instantly.
What would cause the player to lag on the first play?
Thanks.
The delay is due to AVAudioPlayer being initialised. Please see this answer.
The audio system runs on several asynchronous software processes (audio units, OS drivers, etc.) and hardware systems (DMA, DACs, audio amp power supplies, etc.) that never really all completely finish initialization until some sound is actually played all the way out the speakers or earphones.
Here's one method to do that: Create a sound file containing a half second of silence. On app start up, while your app and view controller are still loading, use AVAudioPlayer to play this file of silence. Now when your view finishes loading, AVAudioPlayer should be ready to play subsequent non-silent sounds much faster, since some audio (silence) has already already gone all the way out to the speakers.
What kind of sound are you playing? Alerts, something longer? If alerts, I did go this way and it's much better with lags ...
create system sound with AudioServicesCreateSystemSoundID
play system sound with AudioServicesPlaySystemSound
dispose system sound with AudioServicesDisposeSystemSoundID
... you only need to store SystemSoundID for each sound you would like to play.

How can I speed up or slow down a background music track with iPhone SDK?

Is there an easy way to control the playback speed/tempo of a sound file loop played using Audio Queue Services? For example, if a game is playing background music, I want to make the BGM speed up as time runs out, but without changing the pitch of the music. Thx!
There's no trivial way to do this that I know of. On the Mac, you'd presumably use Audio Units to do this, but I think the support for those is limited on the iPhone SDK.
You can do some processing during your AudioQueue playback callback, between AudioFileReadPackets() and AudioQueueEnqueueBuffer() but I think speed-shifting without changing the tone will require rather a lot of CPU time.
It'd probably be easier to have more than one recording of the music, and switch files during a silent passage of the music.

iPhone (SDK 2.2): adusting playback volume while NOT actively playing music w/ AVFoundation?

So I have an app which plays many short sound clips. I need to know when the sounds are finished playing, and I need to use mp3s, so I'm using AVFoundation for the sound playback.
When a sound is actively playing, and the user uses the hardware volume buttons, the playback volume changes. Problem is, the app is NOT constantly playing sounds, and when it's not, and the hardware buttons are used, the RINGER volume gets adjusted instead.
How do I set it up so, as long as the app is running, the user can adjust the playback volume?
Thanks!
Turns out this can be accomplished by allocating an AVAudioPlayer with any valid sound file and calling the prepareToPlay method, without ever calling the play method.
Works perfectly.
Start an AudioSession and don't stop it when you're not playing sounds.
so you want to disable ringer playback volume as long as the app is running? therefore the hardware controls will only adjust the app playback sounds?
i dont think this is possible unless you are "always playing sound" for example, many games are always playing background music or what have you.
You might be able to accomplish this by constantly playing a 0 volume sound as long as your app is running. You could then play your sound clips over it.
How do I play multiple sounds simultaneously?