We are using SuperPowered library in our iOS app written in Swift and we encountered the problem that the sound is not released by the library and we can not find any way of fixing this.
The problem occurs when entering the view that instantiates the SuperPowered object and playing sound. This is the only place in the app that sound should be claimed. If the user is somewhere else in the app the sound should be released. The problem is that if we navigate to another place in the app and put our app in background and open another app like Spotify (that supports music in background), when switching back to our app the sound (from Spotify) will stop. The problem here is that SuperPowered library doesn't release the sound. Since our app is not a pure music app we do not want to stop other apps from playing sound while our app is in foreground (only when the user is in that view that is supposed to play sound as mentioned above).
Related
How to check iOS APP [music play app] is on Background when APP is launched by the headset wire control?
or quick music play like this
When we click on the quick song to play, in fact, the app has started and played the song, but the iOS system does not actively tell us that the program is in the background. In this case, how exactly do we know that the app is in the background and is separate from the normal home button background switch。
Or how do you distinguish this type of program startup
If you know when your app has started playing music (i.e. via a delegate method or callback or something), you can check the current state of the app by using UIApplication's applicationState property.
When running my app in background, I would get MediaError.MEDIA_ERR_NONE_SUPPORTED (4) when trying to create and play an audio file.
While most suggestions for this error relates to file format, file existence or audio recording, my audio files were playing perfectly when the app was in foreground.
Note: I am already using the Cordova Background Mode plugin.
It turns out that the Cordova Music Controls is causing this interference.
Without manipulating the music controls, audio plays perfectly even when the app enters background.
The problem came from destroying the music controls, which makes iOS prevent your app from playing audio until the user resumes the app to foreground.
Turns out you don't have to destroy music controls before creating them again, to update the track label.
TLDR; Don't kill music controls while playing audio in the background.
With iOS 10.3 Apple released new APIs in MediaPlayer framework that brings more control/power to the developer that want to play music stored on Apple Music/iTunes Match. I’m talking about new applicationQueuePlayer and applicationMusicPlayer of MPMusicPlayerApplicationController.
The problem is of course with the multitasking. I'm building basically an alternative to Apple's own music player. But also I need to execute some code (mostly network connections) when the music is playing. Those new APIs requires to turn on background modes for the app (Audio, AirPlay and Picture in Picture), so music is playing when the user goes to home screen, but the app itself is suspended.
I've figured out that I can play silent audio so the app itself stays alive and maybe, maybe the app will be approved (as technically it's playing music in background and background mode capability must be turned on anyway). But I don't want to consume user's battery when the music is paused.
Even funnier: it seems, that new API is designed for the apps that stay alive in background, because on iOS 10.3.3 after changing the track via Control Center and tapping on the name of the song the system music app is opened instead of my app.
Imagine, that user pause the music from control center before arriving to work, then resumes it from control center. I don't want to eat user's battery when he's at work.
My question is: is there any way to keep the app live and respond to actions taken by the user?
"The music player does not affect the Music app’s state. When your app moves to the background, the music player stops playing the current media."
https://developer.apple.com/documentation/mediaplayer/mpmusicplayercontroller/2817540-applicationqueueplayer
systemMusicPlayer will continue in the background, BUT, you have no access, or I think now (iOS11) limited access to the queue.
I think you still need to use some form of AVPlayer ( https://developer.apple.com/documentation/avfoundation/avplayer ) if you want granular control over what goes on, but then you will need to implement all the things that update the lock screen etc on your own.
So I'm not sure if it's possible to make the native music player on iOS flip to next song from the request of another app.
How would I call from my app for the music app to flip to the next song?
You cannot tell the music app or any other apps to skip a song.
Each app has its own instance of whatever music playing controller it may be using, and so there's no way you can interact directly with it.
Phonegap media play method has a magic option playAudioWhenScreenIsLocked = false that does good job:
Prevents app music from playing when app is in background;
Makes app music obey the hardware "mute" button: without this option your app will ignore if user device is muted and play sound anyway.
But there is third hidden magic in this option. It stops current playing song from native Music app. Most of all it's annoying when I want to play short single sound.
Actually I would like to implement this scenario:
Music: Check if system music is playing on the app start. If it is not, play my app music, otherwise let user enjoy his own media.
Sound FX: Just play my little sound once and don't spoil anything.
And never play anything in background and when device is muted! Even if system Music app can let itself do so.
Has anyone managed to do something similar?
Thank you.
Ended up using https://github.com/floatinghotpot/cordova-plugin-nativeaudio (https://build.phonegap.com/plugins/2291)
Be aware: simple sound effects are not working in Simulator.