iOS: play sound for duration in background - ios

I wondering if there was a way to play a sound or music for a predetermined duration even if the app is put into the background.
For instance, say the sound is 30 mins long but only want to play it for 10 mins, how can I pause the sound while in the background. Or is there a way to tell AVAudioPlayer to loop 0.333 times?
Is there a better class to use instead of AVAudioPlayer to achieve this?
EDIT:
So far I've searched SO and Google to no avail. And have been reading through Apple's docs.
Thanks in advance for the help.

Two solutions:-
-1. You can have Local and Push Notification
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1
Notification can play sound when delivered.
-2. You can make you app run in background and play sound
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
iOS/iTunes rules limit what you can do in background to pass moderation but it mentions playing music.

Related

AVAudioPlayer not playing after a while when app is in background

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!

Play sound at a specific time in background iOS

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!

iOS audio background mode for short one time sounds

I want to create a feature in my app the makes a sound every 5 seconds. The sound file is short and in order to play it every 5 seconds i use a timer.
The issue is i want to play it even if the app is in the background for a maximum time of 30 min and could also be different bpm for each user. I have used background audio mode to do it but i could find in the documentation how does playing sounds is threated by the system i dont want my app to be suspended. I only saw referance to playing music songs in the background with a player.
How can i be certain i will receive the background time needed just for playing small sounds every few seconds and not playing continues music? I was thinking about empty sound wave file but afraid apple would reject my app.
Thanks for any help i can get.

UILocalNotification - play sound longer than 30 seconds

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.

Can we play sounds in the background at a particular intervals in the iPhone app?

In my app I need to play some sounds like beeps at a particular intervals of time (May not be uniform intervals), even the app is in the background. I have gone through the google and many suggested to use AV Foundation’s AVPlayer. I have gone through the following tutorial also.
http://www.raywenderlich.com/29948/backgrounding-for-ios
This tutorial explains how to play a queue of items.
But in my app there will not be any continuous music. Just we need to play some sounds while the app in foreground or background.
Simply i need to play sounds as RunKeeper does.
Please help me out.
You can try scheduling a local notification and providing a custom sound: https://developer.apple.com/library/ios/documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html. This will also display an alert in notification centre / lock screen though.

Resources