How to play longer music as Notification alert? - ios

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.

Related

How are some alarm apps such as Alarmy able to play a sound on iPhone when the app is in the background and the phone is on vibrate

I am working on app that can alert users for some critical things. I use local notifications to alert the user. On iOS, I find that the notifications will not ring if the phone is on vibrate. This is a deal-breaker for many users of the app but I have been putting that question off till now since I thought that iOS doesn't allow an app to play a sound if the app is in the background.
Music apps are able to play songs even when the phone is on vibrate by enabling the audio background mode but it doesn't allow you to schedule a song to be played at a certain time.
Lately I have seen that some apps are able to play a sound at a certain time even though the app is in the background. One such app is Alarmy alarm app. I don't think that they are playing the music via the local notification when the alarm expires because the music continues to play even after I clear the notification. From the local notification documentation, I understood that I am can't run any code when local notification fires till the user clicks on the notification. So, I can't start an audio player which may be able to play the sound in vibrate.
How are such apps able to play a sound even though the phone is on vibate and the app is in background in iOS?
There are few methods to implement this kind of functionality.For reference I recommend this link.
For actually playing the sound while the device’s ringer switch is set to vibrate
First off make sure to include the audio background mode in the capabilities, in order to play audio in the background.
Then,
Swift 4
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.duckOthers, .defaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true)
UIApplication.shared.beginReceivingRemoteControlEvents()
} catch {
NSLog("Audio Session error: \(error)")
}
Here we set the shared audio session’s category to AVAudioSessionCategoryPlayAndRecord, so that we can play sound, while the device’s ringer switch is set to vibrate.
The .duckOthers is specified to make other audio quieter, if there’s any mixable audio playing, so that our alarm can be heard. You can leave that out or use another option, if you prefer a different behavior.
The .defaultToSpeaker is specified, so that the volume would go to the speaker, where it’ll be much louder and should wake up our user with ease.
beginReceivingRemoteControlEvents makes it so that the app handles the remote control options, like the play/pause buttons on the lock screen, in order to make it easier for our user to mute their alarm, once they wake up.
The way this can be done (I have implemented this in my app) is by starting an AVAudioPlayer and specifying a specific time to play. So:
Enable background audio in the app capabilities.
Start and audio session with .playback mode, and start a player at the time you like it to play:
do {
//set up audio session
try AVAudioSession.sharedInstance().setCategory(.playback, options: [.defaultToSpeaker, .duckOthers])
try AVAudioSession.sharedInstance().setActive(true)
//Start AVAudioPlayer
player.play(at: time) //time is a TimeInterval after which the audio will start
}
catch {
...
}
This does not play silence in the background, which violates Apple's rules. It actually starts the player, but the audio will only start at the right time. I think this is probably how Alarmy implemented their alarm, given that it's not a remote notification that triggers the audio nor is the audio played by a local notification (as its not limited to 30 seconds or silenced by the ringer switch).

Play some sound in background, some sound only in foreground

I want to play sound in background for a feature but I want to play sound only in foreground for another feature.
Also, I want to play sound even when the silent switch is turned on.
To play sound in background, the following 2 code is needed:
[Info.plist] UIBackgroundModes = audio
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
To play sound only in foreground, I think I have to set UIBackgroundModes to empty in the Info.plist.
But it is not possible while the app is running.
How can I play sound only in foreground for a feature and sound in background for another feature?
* playing sound in foreground means that sound is paused when the user push the home button of the iPhone and it is resumed when the user open the app again.
How can I play sound only in foreground for a feature and sound in background for another feature
Detect when the app goes into the background, and stop playing the foreground sound and switch to the background sound.

Resume playing music after alarm

My application have permanent music inside that should play when it opened. Problem appears when alarm rang. It automatically stops app music, but it's not resumed after. Anyone know how can I handle this?

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.

How can I prevent my iOS VoIP app from playing audio when the user switches their phone to silent?

I'm building a VoIP app on iOS and I'm using the AVAudioSession category of AVAudioSessionCategoryPlayAndRecord, which is recommended for VoIP apps that need to constantly play and record audio.
However, when the user switches their iPhone ringer to silent mode, the VoIP application will still play sound for an incoming call. This is not desired behavior.
Is there a way to prevent the incoming calls from playing audio when the user has their phone on silent, but still allow them to answer the call and have audio resume?
If you take a look here at the docs for AVAudioSession Categories, you can see that AVAudioSessionCategoryPlayAndRecord unfortunately does not obey the silencing of a phone, as seen below:
Your audio continues with the Silent switch set to silent and with the screen locked. (The switch is called the Ring/Silent switch on iPhone.)
If you want the audio to stop when you turn the phone to silent, you should use AVAudioSessionCategoryAmbient.
I think I found a solution that works for a VoIP application. If the phone is set to silent, I wanted the phone to show an alert and vibrate instead of playing the audible ring.
I can solve this by using local notifications and moving the sound out of the application and adding it to the notification itself. By specifying a sound file as part of the notification, iOS will handle whether it should play the audio or vibrate the phone. This is determined by the position of the Ring/Silent switch on the side of the phone.
Here's an Apple article on adding a sound to a local notification.

Resources