Multipeer Connectivity audio streaming stop work on background - iphone

I'm doing some audio streaming with iOS 7's Multipeer Connectivity framework. The streaming works well, but when I put the app on background it stops working.
Someone can tell me if this is a framework limitation, or I'm doing something wrong?
And, if it is a framework limitation, is it possible to do something to avoid this?
Can I use Background Tasks, to keep streaming and music working on background?
Is possible do this? If is not possible, do any alternatives exist for MultiPeer audio streaming between iOS devices?.
I´m using this example: https://github.com/tonyd256/TDAudioStreamer.
Explained on this page: http://robots.thoughtbot.com/streaming-audio-to-multiple-listeners-via-ios-multipeer-connectivity.
Thanks a lot!

On the Apple documentation for playing audio in the background (scroll down a bit). Some relevant paragraphs:
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. As long as it is playing audio or video content or recording audio content, the app continues to run in the background. However, if recording or playback stops, the system suspends the app.
You can use any of the system audio frameworks to work with background audio content, and the process for using those frameworks is unchanged.
This means that iOS should recognize that you're playing audio through Core Audio, and keep your app unsuspended, as long as you've correctly configured your app for playing audio in the background.
Because your app is not suspended while playing media files, callbacks operate normally while your app is in the background. In your callbacks, though, you should do only the work necessary to provide data for playback. For example, a streaming audio app would need to download the music stream data from its server and push the current audio samples out for playback. Apps should not perform any extraneous tasks that are unrelated to playback.
You should be able to operate normally as long as your app is still playing audio, and is allowed to do what it needs to in order to continue playing audio. This means that you should be able to continue to use MPC in the background to receive the audio data and play it.
Be sure to read the entire documentation on the subject, especially regarding Audio Sessions.

iOS devices get limited cpu cycles for explicit purposes when they have been backgrounded by the user.
According to Apple's documentation on multitasking and execution in the background, the following types of apps are supported, but have to be explicitly declared:
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
Your case falls under Apps that play audible content to the user while in the background, such as a music player app. You can find more information from the link provided above.

Related

WKWebView audio interrupted when app AudioSessionCategory changes

TLDR: How to jumpstart a webviews audioSession without reloading its content?
Hello everyone. I am having a very specific issue with the wkwebview on iOS in regards to the AudioSession.Category management. I guess I start with that it is well understood that the wkwebview operates on its own process and therefor owns its own AudioSession. That means that any changes on the APPs AudioSession won't reflect on the webviews AudioSession.
We build an app, which is entirely web based and has to be for reasons I don't want to go into, so our IOS app is essentially a wrapper for the web content. Recent web content required us to use webrtc functionality do do audio recordings which is only available from iOS 14.3 upwards, which required us to work out a solution, which is backwards compatible down to iOS 12. The idea was to create a native audio recorder, which then interfaces with the webview to transfer audio recordings to the web content.
In order to make recordings within the app the AudioSession.Category needs to change to playAndRecord and is triggered by the webviews content through the messageHandler interface. A change of the APP category will trigger an interruption of the webview audioSession, which from then on does not play any media anymore (until a new url is loaded(single page apps do not make new requests in general)). To fix this a reload of the current url is needed to un-interrupted the sound, which can be somewhat masked behind loading screens but isn't a really good solution.
I am looking for ways to start the audio again without reloading the webview.
Thanks for any insights on this.
I've recently stuck with similar problem. Here is what I've found.
Whenever background music like YouTube in WKWebView or Music app is playing and your app audio session category or mode (but not options [1]) is changed while app's audio session is active background audio is interrupted. Even when re-configuration of audio session is happen between mixable configurations. The solution I've found is to deactivate app audio session prior applying new configuration and activating it again. And in case of playAndRecord it is important not to forget to add [.mixWithOthers]/[.mixWithOthers, .duckOthers] option. For some reason I can't find formal documentation on this behavior but confirmed it with simple demo app which provide manual way to activate/deactivate audio session and configure it to variety of category/mode/options.
[1] It is possible to switch between [.mixWithOthers] and [.mixWithOthers, .duckOthers] on active audio session and not to interrupt background audio, if category and mode are not changed during reconfiguration.

Flutter lower background volume while TTS plays

I have Text to Speech app and I'm wondering if there is a way to allow the user to listen to the TTS audio while they are listening to their music i.e. on spotify or an audio player.
At the moment the TTS plays over the top of spotify by default. Spotify doesn't stop when the tts starts, which is good, but, it is too loud.
Does anyone know if it's possible to lower the volume of spotify or other music that is playing when the user presses play on the TTS?
This requires the usage of AudioManager and AudioFocus. Unfortunately there isn't any package published for flutter to communicate with the platform channel to control volume or request Audio focus and alternate volume programmatically, yet.
developer.android.com/guide/topics/media-apps/volume-and-earphones
> Controlling stream volume programmatically
In rare cases, you can set the volume of an audio stream
programmatically. For example, when your app replaces an existing UI.
This is not recommended because the Android AudioManager mixes all
audio streams of the same type together. These methods change the
volume of every app that uses the stream. Avoid using them:
adjustStreamVolume()
adjustSuggestedStreamVolume()
adjustVolume()
setStreamVolume() setStreamVolume()
setStreamSolo()
setStreamMute()
About Audio Focus -
developer.android.com/guide/topics/media-apps/audio-focus
Managing audio focus
Two or more Android apps can play audio to the same output stream
simultaneously. The system mixes everything together. While this is
technically impressive, it can be very aggravating to a user. To avoid
every music app playing at the same time, Android introduces the idea
of audio focus. Only one app can hold audio focus at a time.
When your app needs to output audio, it should request audio focus.
When it has focus, it can play sound. However, after you acquire audio
focus you may not be able to keep it until you’re done playing.
Another app can request focus, which preempts your hold on audio
focus. If that happens your app should pause playing or lower its
volume to let users hear the new audio source more easily.
Audio focus is cooperative. Apps are encouraged to comply with the
audio focus guidelines, but the system does not enforce the rules. If
an app wants to continue to play loudly even after losing audio focus,
nothing can prevent that. This is a bad experience and there's a good
chance that users will uninstall an app that misbehaves in this way.
#cmd_prompter is right in that you're looking for the AudioManager on Android. (On iOS, the equivalent is the AVAudioSession.)
However, there is a Flutter package available for this use case now — the audio_session package.
The package allows you to determine how your app deals with background audio. You can ask other apps to "duck" their audio (= temporarily lower their volume) or to pause playback altogether.

On iPhone, how do you allow another app to keep running in the background while your app is running?

For example, if I start a song in Pandora and then open Safari to browse the web the Pandora song will keep playing. However, if I start a song in Pandora and then open my app it kills the Pandora song. How do I let Pandora keep playing while my app is running?
You need to tell the iOS system what behavior it should use to integrate the two audio sessions. By default it cancels the background audio when you use audio. But this behavior can be modified as described in the AVAudioSession docs here.
To quote:
Working with Music Players
To play audio from a user’s iPod library along with your own sounds
(as described in iPod Library Access Programming Guide), you must use
a so-called mixable category configuration for your audio session.
There are two, alternative ways to configure an audio session as
mixable:
Use the AVAudioSessionCategoryAmbient (or the equivalent
kAudioSessionCategory_AmbientSound) category—which is always mixable.
Use the mixable category override property
kAudioSessionProperty_OverrideCategoryMixWithOthers, as described in
“Fine-Tuning the Category,” to make an otherwise nonmixable playback
category mixable. Having used one of these options, your sounds will
not interrupt a music player—and neither will a music player’s sounds
interrupt yours.
There are two reasons why your app might "kill" the background app:
It starts to play music of its own
You use too much memory for it to keep playing and the OS closes the background app
There is no special option you need to select; you just need to be a "good citizen."

How can I record currently playing audio on the iPhone?

I'd like to record what the iPhone is currently outputting. So I'm thinking about recording audio from Apps like Music (iPod), Skype, any Radio Streaming App, Phone, Instacast... I don't want to record my own audio or the mic input.
Is there an official way to do this? How do I do it? It seems like AVAudioRecorder does not allow this, can somebody confirm?
Officially you can't. The audio stream belongs to the app playing it ,and iOS.
The Sandbox paradigm means that a resource owned by your App can't be used by another App. Resource here means Audio/Video stream or file. Exceptions are when a mediator like Document interaction controller are used.
If you want to do this you'd have to start with deducing AVFoundation's private methods and find out if theres a way there. Needless to say this it wouldn't be saleable on the App store and will probably only be possible on a jailbreak.
Good Luck.
TLDR;
This is only feasible only from time to time, as it's a time expensive process.
You can record the screen while listening your songs on Spotify, Music or whatever music application.
This will generate a video on your Photos application. That video can be converted on MP3 from your computer.
Actually, this is not true. The screen recordings will not actually have the audio from Apple Music at all, as it blocks it. Discord also uses this pipe as well, so you cannot record Discord audio either this way.

How to play a sound while running in the background?

I need to be able to play a sound while running the application in the background.
I tried with UILocalNotifications but the file needs to be part of the bundle, and that will not be the case, as I need to generate the sound files on the fly...
So is there any way that I can play a sound while running in the background?
Thanks.
You can't, unless you continuously play audio and have the appropriate audio session category set for your app... then your app doesn't go to sleep, it continues to run (again so long as audio is playing).
This is part of the design limitations of iOS multitasking.
Apple has allowed people to get away with having a continuously looping blank audio file (AVAudioPlayer loop = -1 with a 1-sec blank CAF file) if the app is primarily audio related and it is obvious to the user that this has battery life implications (and can be disabled by the user), but YMMV... you app can also be rejected for this.