How to allow external music/audio to continue in Corona SDK apps when they launch? - coronasdk

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

Related

iOS Audio/Sound won't play in background with background mode active

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.

App in background swift iOS

i have a big problem. I am working on an app about accessibility. The app should work through the iPhone microphone to take over sounds from the environment (indoor) and compare them in real time with sounds in the app database (recorded from users previously). If sounds match, the user will be warned by the app through a notification. So... firstly, the app should listen sounds working in background, but after some minutes, iOS closes the app in background automatically. IS THERE SOME SOLUTIONS FOR THIS PROBLEM? TO ALLOW THE APP BE OPENED AND WORKING?
Secondly, do you know in which way is possible compare sounds recorded with sounds listened by microphone in real time?
Really really thank you. 🙏🏻
Regarding the problem that your app is automatically closed, it seems that something was not configured properly in your project.
When the UIBackgroundModes key contains the audio value, the system’s
media frameworks automatically prevent the corresponding app from
being suspended when it moves to the background.
Make sure that you have enabled the "Audio, AirPlay and Picture in Picture" background mode in your project.
Fore the first question:
Because your app will be recording the audio while in background, you can:
set UIBackgroundModes key in your app’s Info.plist file to audio.
It should keep your app alive when you press Home button. See the Declaring Your App’s Supported Background Tasks section
At some point, iOS can kill your app due to some conditions, but
eventually it would restart your app later.
And be careful with this, you might have to deal with Apple Review team to prove that your app is not doing something harm to user privacy. Otherwise, they wouldn't let it go live.
For the second question:
I think you can use some AI/Machine Learning service out there to do so. It's much more accurate and faster than building it your own.

iOS/Xcode: is there a difference between running the App on an iPhone while connected to Xcode compared to opening the installed App afterwards?

I have a Timer which plays AVAudio at a specific time while the screen is locked (app in foreground when locked and plist setting 'Application does not run in background' set to YES).
When I run my code while my iPhone is connected to Xcode it works as desired, playing audio while locked even when the iPhone has been locked for hours.
Now if I disconnect my iPhone and just open the app by tapping the icon the audio won't be played if it's set for more than a minute.
Is there a difference that causes this problem? If I would publish my App to the AppStore which behaviour would my users get?
That 'Application does not run in background' flag is a very old flag that means your app does not support multitasking and should be terminated when the user presses the home button. There's no good reason I can think of to set that flag on a newly developed application. Unless you understand exactly what it's for and are positive that setting it to yes is the correct thing to do, don't do that.
(That flag was added when iOS 4 was released, to allow apps that could not handle multi-tasking to request the old terminate-on-home-button behavior from iOS 3. Given that we're now 6 major iOS releases from iOS 4, it seems outdated.)
So remove that flag. It probably doesn't have any impact on your question, but remove it or set it to no in any case.
If you want to run a timer while the phone is locked and play sounds at designated times then you will probably need to set your app up as a background sound player, and ask for more background time when you get a message that you are going to the background.
That being said it's likely that Apple will reject your app. Running a timer from the background means that the processor on the phone has to run at full speed all the time, which will DRAMATICALLY reduce battery life.
Apple only allows a very small subset of apps to run in the background, and then only under specific circumstances (like a background sound playing app when the user is actively listening to music, or a navigation app that is actively tracking the user's location and notifying them when it's time to make a turn to follow directions.)
Should be no different. If you'd like to feel what do your users' feel then you can publish it to TestFlight which is the best app beta test environment. Find some testers or try it yourself.
The directions should be to verify the playing logic and the sound file relationship which link to the connection.

Playing audio files in background in iOS

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.

Running an app function from background- iPhone

I need to create an app which can handle movement events while running on the background.
i.e each time an accelerometer event is happening it should be tracked in the application memory-when the application is running in the background.
My questions are :
1. Is it possible on IOS?
2. How can it be done?
(I came up with some relevant questions but i'm not sure that the answers are up to date due to IOS changes.)
Thanks,
Asaf
You should have a look to this thread (and the links inside) and the apple documentation.
To summarize no third party apps can run in the background (even though some of apple apps do this) excepting for:
Tracking the User’s Location
Playing Background Audio
Implementing a VoIP App
Downloading Newsstand Content in the Background
Communicating With an External Accessory

Resources