i found some useful tips here but to be sure if my requirements are possible to implement here is a short list.
i have an app which runs in background most of the time. the app "lives" for up to some hours and on a certain point i need to play an alarm sound.
so what i have found is that i need to start a AVQueuePlayer and add a mutesound which is playing constantly until i need to play my other sound. is that right?
so i create a singleton class (SoundService) where i initialize the silentsound and the alarmsound in the queue player.
when the app is going to the background i start the player and play the silent sound. if i then need to play my alarm sound while the app is in the background it should work, am i right?
right now my implementation does not work, but am i on the right way?
thanks
You cannot initiate audio in the background. The only thing the audio background mode allows you to do is to continue producing sound as the app goes from the foreground to the background.
However, if your app is capable of receiving remote events, and if it has produced sound so that it is the remote event target, then, with audio background mode, it can go on being the remote event target and thus can produce sound in the background, as long as no other app becomes the remote event target in the meantime.
The most reliable way to produce a sound while in the background is by attaching the sound to a local notification.
notification.soundName = UILocalNotificationDefaultSoundName;
notification.soundName = #"mySound.caf";
Make sure the sound is actually in your app’s bundle, is in the correct format (linear PCM or IMA4).
You can convert from wav and mp3 using:
afconvert -f caff -d LEI16#44100 -c 1 original.wav mySound.caf
Related
I am making an alarm clock app. To play the alarm sound at the appropriate time, I use myAudioPlayer.play(atTime: myAudioPlayer.deviceCurrentTime + secondsUntilAlarm). This way, even if the app is in the background, the audio player plays the alarm sound at the appropriate time.
Note: I got this idea from a different SO answer, which unfortunately I can't seem to find right now.
However, what I've noticed is that alarms are being played correctly if the secondUntilAlarm value is relatively soon, like maybe 20 minutes or less (converted to seconds of course since that's what the method requires). However, if it's longer than that, the sound does not play. Is there something I'm missing with how this method works in the background? Could the app be entering some sort of suspended state or something that disables the audio player from triggering the playback?
Any suggestions or comments would be appreciated--thanks!
So I've actually determined that it does not matter if the app enters the suspended state (in fact, this is expected). I've identified that the reason the alarm sound does not play sometimes because I open another app with audio that deactivates my app's audio session (therefore, it has nothing to do with the time the alarm is set for). To fix this, all I had to do was ensure I set .mixWithOthers for my app's audio session. That way, other audio sessions from other apps don't deactivate mine!
I have a muslim app that triggers local notifications with sounds for prayer times throughout the day. The sounds are limited to 30 seconds. I am searching for a workaround to play sound longer than that.
I found out I can use AVAudioSession to play audio even if the app is in background by turning on Background Modes (Audio, Airplay, and Picture in Picture) in Capabilities target tab and using background task to play the audio. However I want to play sound at a specific time by scheduling it.
Among the workarounds I found are also setting continuous local notifications. However I came across an alarm app (Alarmy) that can play sound when local notification is received. How is that doable? Thank you in advance!
I want to set a music which is longer than 30sec as notification soound. But after searching about this I came to know that it is not possible to add these type of music as notification sound. notification sound must be in 30sec longer otherwise it will not play music. Now how can I add more than 30sec longer music as notification sound?
I had a similar issue for my alarm app as well. What I did was a workaround when your app is in background. You play a music file in your code in background and increase the volume of the device to maximum. You can play as much longer music file as you want. Stop playing the music when someone clicks on the notification.
The only problem with this is that your app should not be killed or terminated.
I am trying to develop an alarm app for iOS.
I have implemented it using UILocalNotification, but the issue is that local notification will allow playing local audio for 30 seconds. So I am looking for some way to extend this playing time.
Is there any way to play online MP3 or media file while the app is in background and when the notification occur by enabling background mode to play music?
There is no way to achieve this. Playing silent sound to keep the app running in background is against Apple rules and your app will be rejected (I know this by experience). Furthermore, playing audio in background eats the battery quickly.
You can try to schedule multiple notifications, but it is not a clean solution either.
I have an app that under certain circumstances needs to play audio the moment it is pushed into background.
I can't use any background key, as I don't fulfill any requirements for these and Apple rejected my app, when I tried to use the background audio key.
Currently I'm start playing the audio file and then request extra processing time within applicationDidEnterBackground: The audio never gets played even though I have the processing time and the app is still running (I've used an NSTimer to check every 2 seconds).
As audio mode I'm using AVAudioSessionCategoryPlayback with AVAudioSessionCategoryOptionMixWithOthers.
With active background audio mode this works fine.
I also know, that I could use local notification and play the audio file there (as it is under 30 seconds), but that would be my last resort.
Does Apple prevent audio from being played in applicationDidEnterBackground: ?
I get a callback for audioPlayerBeginInterruption: immediately.
I've seen:
Why does the following code to play audio in the background not work?
But this doesn't seem to work anymore on iOS8.
Thanks :)
Your app should not be playing a sound (and certainly not a sound of any length) just because the user backgrounds it, and now Apple has succeeded in stopping you from doing this. You should accept this, abandon your attempts to work around this perfectly reasonable restriction, and move on.