I'm trying to find responses to this in Apple's documents, but I don't:
1) Is it technically possible to start playing an audio file when an app goes to background?
2) If technically possible... is it "legal"? Would Apple approve an app that starts playing an audio file without an explicit interaction of the user?
Thanks in advance
An app can start playing an audio file in the background if it is already playing audio before the app goes into the background, has the appropriate audio background mode capabilities key set, and mixes the new audio file into an already playing audio unit, queue or player without stopping. There are plenty of approved apps in the App store that do this (e.g. they sequence through playlists).
An app can't start playing an audio file in the background if it is not already playing or recording audio, as the OS will take away and disable the app's audio session as it goes into the background.
Reports are that apps that play "silence" to get around this restriction are currently being rejected by Apple, although some legacy examples (alarm clocks?) may still exist in the iOS App store. But for private (non-App-store) apps, this trick currently seems to work just fine.
Also, remote notifications can start playing very short audio files contained within an app's bundle, even if the app is in the background without an active audio session.
Related
My app (made with Flutter but this should not matter) has something like a timer functionality that makes a tick sound in regular periods (between 10s and 3min). I have the background mode Audio, AirPlay, and Picture in Picture activated and the following in my Info.plist.
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
but the audio will still stop when running in background.
This occurs when running the app in profile mode, when I run in debug mode, the audio continues when running in background.
What can I do to have the audio continue to run in background?
There is a relevant note in the audio_service 0.18.0 README which can help here:
Note that the audio background mode permits an app to run in the background only for the purpose of playing audio. The OS may kill your process if it sits idly without playing audio, for example, by using a timer to sleep for a few seconds. If your app needs to pause for a few seconds between audio tracks, consider playing a silent audio track to create that effect rather than using an idle timer.
Well, unless you did this in native code (Swift/Objective-C), your code is running inside the Flutter engine - probably with some Dart Timer.periodic.
The Flutter engine may be killed off at any point in time when the app is in the background. On Android this can even happen when simply switching to the camera and back to the app afterwards. On iOS usually after some fixed time or on high system load.
In this regard, Flutter (and most other cross-platform toolkits) are very different to native apps.
You can start with the official documentation here: https://flutter.dev/docs/development/packages-and-plugins/background-processes
This may be a good article: https://medium.com/vrt-digital-studio/flutter-workmanager-81e0cfbd6f6e
I don't know enough about iOS but I think there is no easy way to schedule execution in the small intervals you require. On Android something like the AlarmManager can be used.
You can try writing the scheduling code natively and schedule it from the app via a MethodChannel when the period is set.
You can look at these libraries:
https://pub.dev/packages/workmanager (probably can't wake up at the small intervals you need)
https://pub.dev/packages/android_alarm_manager_plus (only for Android)
https://pub.dev/packages/audio_service (may give you some idea on how to achieve background execution on iOS)
Edit:
After reading more about Enabling Background Audio on iOS, it seems to me, that this only works when using
an AVAudioSession. Which you are probably not using. To get this working you need some native code. The audio_service package uses such a session. You can try scheduling with Dart code and playing the sound via the audio_service package. Sounds like it could work but I have no experience with this package.
Please, pay attention to the answer from #RyanHeise – he's correct on the point of using AudioSession: in background you should play either sound or silence. As soon as audio will be paused, app can be suspended.
Also, Important Note: when app entering background, scheduled timers will pause. That's why you might think it stopped working. Do not use scheduling via Timers on background - rely on events from the system.
following the examples, I enabled the Background Audio capability in my app, even though I do not need it. I want simple playback only, with midi triggering sounds (sf2) from the app bundle only, in foreground only. I got everything working and submitted my first version to the App Store and I get complaints from the reviewer saying that if I don't use background audio (so they can't test it) then I should switch it off in the Capabilities. If I do that my foreground audio triggering stops working. Can anyone help in what is the proper way configure AKSettings and the app in this case, so I could pass the app store review?
I created an iBeacon app to detect beacons using phonegap/cordova. How the app works is that once it detects a nearby beacon and the app is closed or running in the background i use local notification which includes an audio (common apple notication sound). I don't know why apple rejects it with the below comment:
2.16: Multitasking apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc.
Next Steps
The audio key is intended for use by applications that provide audible content to the user while in the background, such as music player or streaming audio applications. Please revise your app to provide audible content to the user while the app is in the background or remove the "audio" setting from the UIBackgroundModes key.
Your app does not provide continuous streaming audio. For details on delivering an audible alert for a local notification, please refer to the About Local and Remote Notifications Programming Guide.
We hope you will consider making the necessary changes to be in compliance with the App Store Review Guidelines and will resubmit your revised binary.
Apple's comment is quite clear. Your app is incorrectly specifying that it needs the audio background mode. But it doesn't. That mode is only to be used to play audio (such as music) while the app is in the background. It is not to be used just to get audio from a local notification.
Remove the audio background mode setting from your app and Apple will be happy.
Even our App received a rejection last week for the same reason.
We didn't even need to submit a new binary. All we did was explained to the reviewer why we were using each of the background modes which we had listed in our plist. We did this by uploading a video in which we explained the purpose of each background mode. And then provided a link of this video in the resolution centre in iTunes connect. The reviewer had a look at it and later approved our app.
So you have to do a similar thing. If you need the background mode(audio in your case). Just explain it's intended purpose to the reviewer in the resolution centre. Else, just upload a new binary with the audio background mode removed from the plist.
I'm very new to xcode and am trying to figure out how I can pause music coming in from another app such as iTunes or Spotify so that it does not run in the background of my app. Is there a command I'm missing or do I have to have a button to shut it off?
Please have a look at the Audio Session Programming Guide. You have to create an audio session with category AVAudioSessionCategorySoloAmbient.
In Corona SDK how do you allow existing music/audio already playing in the background when your app (corona sdk based app) is launched and run? In particular:
iTunes Music/Podcasts - ability to have background music/audio to keep playing when you launch your app (i.e. corona sdk built)
Other Apps (e.g. audible.com app for audiobooks) - ability to have audio from such an app continuing when you launch your app (i.e. corona sdk built)
I'm guessing (2) may not be possible, but at least how do you implement (1)? I know there are apps out there (well I've seen IOS ones at least) which offer the ability for your background music to keep playing, but need to find out how to do this with Corona SDK? or if it's not yet possible just to understand this
Note, my app was intending to just play short audio clips (e.g. menu button pushes) on top of the background music/audio. That is wasn't planning to stream music in my app
From my understanding, Corona allows background music to be played no matter if it's from iTunes or other apps.
However since build 2126, there was a regression bug that made external (background) music stops when the app launched.
And it is fixed in the recent Corona daily build, 2179.
Please take a look at this Corona forum post if you want to know the detailed development for this issue:http://forums.coronalabs.com/topic/40837-allow-external-music/
I have tested it & verified that the background music continues when my app launches if I build with build 2179 or before build 2126.
http://docs.coronalabs.com/api/library/audio/index.html
you can check the about audio load, play, streaming, pause etc.
audio.loadSound() — loads an entire sound into memory. It should be used for shorter audio files that may be used repeatedly throughout the app.
audio.loadStream() — reads in small chunks of an audio file over the course of its duration. It should be used for longer audio tracks like background music. Note that streamed files may have a slightly higher latency cost and CPU cost than files loaded with audio.loadSound()
Go through the above link.
I found this post corona post very useful related to it and it has content to resolve your issue, Please check:
http://developer.coronalabs.com/code/sample-code-loop-background-music-synchronized-your-apps