I am working on YouTubePlayerFragment integration. while the initialize YouTubePlayer in YouTubePlayerFragment it acquires audio from another app and that particular app stops playing audio ie. PlayMusic.
As User does not touch play button in my app's youtube player it would not acquire audio from another app. so how to avoid such issue and let another app plays the audio?
Here is my Fragment code, written in Kotlin.
class MyVideoFragment : YouTubePlayerFragment() {
lateinit var mPlayer: YouTubePlayer
companion object {
fun newInstance(url: String): TutorialVideoFragment {
val v = TutorialVideoFragment()
val b = Bundle()
b.putString("url", url)
v.init()
v.arguments = b
return v
}
}
private fun init() {
initialize(DEVELOPER_KEY,
object : YouTubePlayer.OnInitializedListener {
override fun onInitializationSuccess(arg0: YouTubePlayer.Provider,
player: YouTubePlayer, wasRestored: Boolean) {
if (!wasRestored) {
player.cueVideo(arguments.getString("url"))
if (player.isPlaying) {
player.pause();
}
player.setShowFullscreenButton(true)
mPlayer = player
}
}
override fun onInitializationFailure(provider: YouTubePlayer.Provider,
errorReason: YouTubeInitializationResult) {
if (errorReason.isUserRecoverableError) {
// errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
} else {
val errorMessage = String.format(
getString(R.string.error_player), errorReason.toString())
toast(errorMessage)
}
}
})
}
}
I faced the same issue. setManageAudioFocus(false)
is what you need.
You can disable auto gain of audio focus inside your onInitializationSuccess
by doing player.setManageAudioFocus(false)
but remember you need to manage audio focus manually. You can read about managing audio focus here