Background Audio mode with MPMusicPlayerController.systemMusicPlayer - ios

I'm working on an iOS app that uses MPMusicPlayerController.systemMusicPlayer to play songs from Apple Music to the user.
My app should be able to append songs to the Music player's queue based on messages it receives from a server. I have this working with a simple WebSocket connection between the app and the server, but as soon as the app enters the background the socket is automatically closed (which makes sense).
Because the actual music playback is done by the Music app, I can't use the Background Audio background mode to keep my app alive. Is there a way around this?
Things I have already considered (and why I don't think they will work):
Remote Notifications are throttled too slow to be of any real use
Background App Refresh is also too slow
PushKit / VoIP (the app isn't a VoIP app)
Playing "blank" or nearly silent audio over the actual audio which seems too "hacky" and likely won't pass app review
Using background location tracking (again, almost certainly won't pass review)
Ditching systemMusicPlayer completely and using AVAudioPlayer with the Apple Music API (this would be reinventing the wheel a little bit and would force streaming even if the media was downloaded)
Using applicationQueuePlayer and just forcing the user to stay in app (this would be a bad user experience imo, they should be able to listen in the background)
Any help would be appreciated, thank you!

In iOS 15 and later, you should be able to just set the background plist flag for your app, and then just use ApplicationMusicPlayer.
https://developer.apple.com/documentation/musickit/applicationmusicplayer

Related

Is it possible to build an alarm clock such as the built in app from Apple?

A lot of todo-list apps such as Wunderlist offer some kind of reminder feature which usually just fires off a notification.
But I have never encountered an app that offer a similar notification view as the built in preinstalled alarm clock. You know, that locks the phone and offers a snooze option.
This makes me wonder if the alarm clock functionality that is built into the preinstalled iOs app from Apple including setting time or add a snooze button etc is only available for that particular app?
Since I'm a designer without any coding skills I only want to know if it possible or not.
Unfortunately this is unavailable for developers. You can check all of alarm clock apps in AppStore, all of them are using the standard local notifications. Which will fire once and that's all. You can of course set your custom sound and change couple of options, but it will never work like one build in Clock app inside iPhone.
You can read more about local notifications in Apple documentation: https://developer.apple.com/reference/usernotifications/unnotificationrequest
I figured Alarmy is constantly playing (when there is an alarm set up) a silent sound in the background. Like Spotify, but silent. This keeps the app active all the time. I came to this conclusion by logging the result AVAudioSession.sharedInstance().isOtherAudioPlaying (Swift 4) in my own application. Whenever Alarmy is terminated it returns false, whenever Alarmy is running in the background it returns true. Same thing happens for the app linked by Ernesto Elsäßer.
I used the same technique and I also used this pod to trigger a function when the time comes and start the real music.
I did this for testing and am not going to make an app out of it because playing silent sounds in the background is against Apple's rules. I don't know how Alarmy (or other apps that use this same method) got away with it.
Some related questions:
How do you constantly run in background?
App “Alarmy” is able to play iTunes song from background state… How?
Wake up application in background using AudioSession like Alarmy iOS app
Also you can check this article out.
This app seems to do exactly this, but I have no clue how ...
I tested it, and it managed to start playing music on a locked device at an arbitrarily chosen time, and for way longer than the 30s timeframe allowed for notification sounds. Further, the music is streamed from an online radio station, meaning they found a way to ...
wake up the app from a scheduled notification without user interaction
create a socket connection to a radio station in the background
start playing sound in the background for an indefinite amount of time
I also tried out the alarm in Airplane Mode, and it still played a locally stored backup song, so it probably doesn't abuse the VoiP background mode or push notifications, but really is triggered by a local notification ...
So although I can't tell how, it looks like there is an App Store Review-proof way to create a real alarm app.
(I am not affiliated with the developers of this app, just did some research on what's possible.)

Keep an iOS application running in background forever

This question might be duplicate of many others but I am still asking because I really need to get any solution of it.
For iOS versions prior to 6, for devices other than those having an A7 chip.
I have an application which interacts with the Accelerometer. I am also running it in background by playing a sound file and turning the Audio services ON for background task.
Now If any third application plays music, it takes control of shared Audio Session of device which causes my background thread to call completion handler assuming that background task has been completed.
Now my application is interruptible by iOS and iOS can kill it if it remains in background for a long time after which I couldn't interact with accelerometer.
Is there any way/trick/hack to keep my application running all the time until unless I kill it by my own.
Again, this is a duplicate question but I really wanted to share my problem with you people to help me in this sense.
Apple is very clear on this:
Implementing Long-Running Background Tasks
For tasks that require more execution time to implement, you must
request specific permissions to run them in the background without
their being suspended. In iOS, only specific app types are allowed to
run in the background:
Apps that play audible content to the user while in the background,
such as a music player app
Apps that record audio content while in the background.
Apps that keep users informed of their location at all times, such as
a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Collecting accelerometer data is not listed. Therefore, what you are trying to do is not possible. "Ways, tricks, hacks" tend to get discovered quickly and offending apps removed from the App Store.

Can i make call to webservice when App is running in background

I need to make this call to web service over a period of time interval like after 10 min the application went to the background. Can I make call to web service from app when App is running on background? Is there any other way to get some latest data when app is running background?
Not possible in iOS6.x or lesser unless your application is has specific requirement to run in background like locations, Voip, music etc...
However this is possible with iOS7, please consider having a look at this tutorial on Background Fetching
You can not do this in background mode. There are few services that can be used in background.
Location updates : The app keeps users informed of their location, even while it is running in the background.
VOIP : The app provides the ability for the user to make phone calls using an Internet connection.
Audio : The app plays audible content to the user or records audio while in the background. (This content includes streaming audio or video content using AirPlay.)

Keeping app using iPodMusicPlayer running in the background

I have written a music player app, and I am trying to add a feature to save all the information I possibly can about the current playing song when the audio stops or the app is quit (some examples are when library updates occur, or the song changes in the background). The goal is to be able to resume audio (mostly for audiobooks) where the user left off, in any reasonable circumstances.
I have been doing considerable research on methods to keep the app running in some form in the background, but I have not been able to implement any of them successfully.
My Goal:
Respond to playback changes with app in background
What I have:
info.plist entry - UIApplicationExitsOnSuspend = NO
info.plist entry - UIBackgroundModes -> item0 = audio
[MPMusicPlayerController iPodMusicPlayer] is what I am using for playback
App targeting iOS5+iOS6
Questions (basically, is this a fruitless endeavor):
does using iPodMusicPlayer preclude my app from running in the background inherently, since it is not actively actually doing anything in my app?
what method of playback, if any, would keep my app active without potentially having to essentially rewrite a good chunk of it
with the fairly simple goals in mind, are there limits on what can be run in the background that would make them impossible?
Would very much appreciate any help on a direction to take on this.

An equivalent to an NSTimer for a background Audio app?

I have a video streaming app that is properly set up to play video via AirPlay while the device is locked or in the background. It works great. However, the one thing that doesn't work when the app goes into the background is the NSTimer I use to send an update back to a server once every 10 seconds. I've read through countless posts about NSTimers not working when an app is in the background. There are lots of proposed hacks and kludges, but I didn't find the proper way to do this, or if there even is a proper way.
However, since mine is an audio app where some background processing is permitted, I would expect there might be some way to periodically send a post to a remote server.
Is there a preferred way to do this?
Thanks,
Craig

Resources