Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with stopping playback #345

Merged
merged 2 commits into from
May 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
Expand Down Expand Up @@ -61,7 +60,7 @@ internal class LegacyYouTubePlayerView(context: Context, attrs: AttributeSet? =
// stop playing if the user loads a video but then leaves the app before the video starts playing.
youTubePlayer.addListener(object : AbstractYouTubePlayerListener() {
override fun onStateChange(youTubePlayer: YouTubePlayer, state: PlayerConstants.PlayerState) {
if(state == PlayerConstants.PlayerState.PLAYING && !canPlay)
if(state == PlayerConstants.PlayerState.PLAYING && !isEligibleForPlayback())
youTubePlayer.pause()
}
})
Expand Down Expand Up @@ -198,6 +197,15 @@ internal class LegacyYouTubePlayerView(context: Context, attrs: AttributeSet? =
canPlay = false
}

/**
* Checks whether the player is in an eligible state for playback in
* respect of the {@link WebViewYouTubePlayer#isBackgroundPlaybackEnabled}
* property.
*/
internal fun isEligibleForPlayback(): Boolean {
return canPlay || youTubePlayer.isBackgroundPlaybackEnabled
}

/**
* Don't use this method if you want to publish your app on the PlayStore. Background playback is against YouTube terms of service.
*/
Expand Down Expand Up @@ -225,4 +233,4 @@ internal class LegacyYouTubePlayerView(context: Context, attrs: AttributeSet? =

fun removeFullScreenListener(fullScreenListener: YouTubePlayerFullScreenListener): Boolean =
fullScreenHelper.removeFullScreenListener(fullScreenListener)
}
}