How to play youtube video while mixing with background music? - ios

I have an app that is used in the gym. The user may want to watch a video demonstrating an exercise without stopping the music they are playing through Spotify or some other app.
I used to do with with a UIWebView, and mix the audio in AVAudioSession, but I can't do this with WKWebView. WKWebView takes over the audio session and stops whatever else is playing, then it doesn't restart the audio it stopped.
My question is, what other ways can I play the Youtube video in my app without stopping the background music? or if it must stop, at least be able to restart the background music.
Music stopping at the gym and being forced to go out of the app to restart it is terrible user experience.
Also, please note that going back to UIWebView is not an option, as Apple is deprecating this class.

Related

How to resume audio playback of other apps after deactiving the audio session and how to start audio playback in the background without mixing?

I am building a iOS app with Ionic and Capacitor. I use AVFoundation inside a Capacitor plugin which provides a public method to play an audio file accessible at a remote URL. The basics work, the audio file is successfully played. Now I want to correctly handle interruptions and background audio. By background audio I mean start playing an audio file while the app is in the background and not continue playing it in the background when it was started in foreground before. To summarize, the app should behave the following way:
If another app (e.g. Spotify) is playing audio and my app wants to play audio, the other app should be interrupted and my app's audio should start playing without mixing.
If my app's audio is finished playing, the other app's audio should resume.
My app must be able to start playing audio without mixing when it is in the background.
I want to show a play/pause button and the audio title on the lockscreen and in the notification center.
I added the background mode audio capability. I use AVPlayer, AVAudioSession, MPNowPlayingInfoCenter and MPRemoteCommandCenter, followed best practices, especially regarding activation and deactivation of an audio session and interruption handling. I followed this question and this question. I tried several solutions, but never managed to achieve all of the above goals. What I found out so far is the following:
Goal 1. above is easy to achieve. Simply activate an audio session without mixing and start playing the audio.
After the audio of my app is finished, I deactivate the audio session. Unfortunately, I can only achieve goal 2. above if I don't use remote commands nor notification info. This means, my app then has no audio control UI on the lockscreen nor in the notification center. Somehow the configurations of MPNowPlayingInfoCenter and MPRemoteCommandCenter seem to affect the interruption behavior. As far as I could see, Apple documentation doesn't say anything about this.
I only manage to start playing audio in the background when setting the option .mixWithOthers. Apple documentation doesn't say anything about this, either.
Any help would be greatly appreciated.
Rony

How can I play music successively in the background with UIWebView or WKWebView

The website implemented inside my app would play a series of mp3 files,it works well when the app is active,but when I press HOME button or lock the phone,it cannont play more than ONE song in the background,after that,app suspended.
I've already tried to set different kind of AVAudioSession Category,but of no use.
I assume you don't have audio background mode enabled. This is what Apple documentation says:
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.
So it seems like you just need to enable audio background mode. Here is Ray Wenderlich's tutorial on background modes

Phonegap media stops system music

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.

Preventing my app's audio from interfering with other background music

I think what I'm looking for is the combination of existing settings that I can't quite put together in the right order. I have an app that plays sound clips using AVAudioPlayer. There is a start/stop button.
I would like any other apps' audio to continue until my app actually needs to play something.
When my app is launched with some iTunes music playing, that music continues correctly. When I press play in my app, the other audio correctly stops, and my app plays. I stop my player, switch back to the music app, and press play again. But now when I switch back to my own app, the music stops immediately.
I would like it to again wait until it needs to play something before killing the music app.
My best guess is that once I've used the player, it is in some kind of 'I need audio' state, and remains in that state. How do I resign this state?

How to continue playing a youtube video at background when switch from my app back to home screen

Seems the default action for iOS 6 is to stop the video from playing after I switch to HOME screen. Then if I want to listen to the audio I have to go back to the app and press the 'Play' button again. I am using youtube API.
How can I continue playing the video when the application is in UIApplicationStateInactive and UIApplicationStateBackground. I just want to be able to listen to the audio even if I am on HOME screen or the screen is locked. Should I use a background thread or restart playing the video at the previous position as it was before turning the app inactive?
Take a look at the app 'Jasmine'. They managed to do something similar to what you are seeking. However, UIApplicationStateInactive/UIApplicationStateBackground will cause the video to pause. Double tapping the home button and then pressing the audio play button will cause the video's audio to resume without keeping the app active.
Take a look at the new iOS 7. You may have some luck there.
Inter-App Audio
Now your apps can make beautiful music together. With Inter-App Audio,
apps can register their audio streams to share with other apps. For
example, a series of apps could publish audio streams of instrument
tracks while another uses the combination of these streams to compose
a song. Inter-App Audio also provides for MIDI control of audio
rendering, remotely launching other registered Inter-App Audio apps
and more.

Resources