Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".AndroidYoutubePlayerActivity">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="@+id/third_party_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".WebViewActivity">
<WebView
android:id="@+id/webview_player_view"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".YouTubePlayerAPIActivity">
<fragment
android:id="@+id/official_player_view"
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</RelativeLayout>
class AndroidYoutubePlayerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_android_youtube_player)
third_party_player_view.getPlayerUiController().showFullscreenButton(true)
third_party_player_view.addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {
override fun onReady(@NonNull youTubePlayer: YouTubePlayer) {
val videoId = "YE7VzlLtp-4"
youTubePlayer.cueVideo(videoId, 0f)
}
})
third_party_player_view.getPlayerUiController().setFullScreenButtonClickListener(View.OnClickListener {
if (third_party_player_view.isFullScreen()) {
third_party_player_view.exitFullScreen()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
// Show ActionBar
if (supportActionBar != null) {
supportActionBar!!.show()
}
} else {
third_party_player_view.enterFullScreen()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Hide ActionBar
if (supportActionBar != null) {
supportActionBar!!.hide()
}
}
})
}
}
dependencies {
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.3'
}
@SuppressLint("SetJavaScriptEnabled")
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
webview_player_view.settings.javaScriptEnabled = true
webview_player_view.loadUrl("https://www.youtube.com/embed/YE7VzlLtp-4")
}
}
class YouTubePlayerAPIActivity : AppCompatActivity(), YouTubePlayer.OnInitializedListener {
private val RECOVERY_DIALOG_REQUEST = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_youtube_player_api)
val youTubePlayerFragment = supportFragmentManager.findFragmentById(R.id.official_player_view) as YouTubePlayerSupportFragment?
youTubePlayerFragment?.initialize("YOUR_API_KEY", this)
}
override fun onInitializationSuccess(provider: YouTubePlayer.Provider,youTubePlayer: YouTubePlayer,wasRestored: Boolean) {
if (!wasRestored) {
youTubePlayer.cueVideo("YE7VzlLtp-4")
}
}
override fun onInitializationFailure(provider: YouTubePlayer.Provider,youTubeInitializationResult: YouTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError) {
youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show()
} else {
val errorMessage = String.format(
"There was an error initializing the YouTubePlayer (%1\$s)",
youTubeInitializationResult.toString()
)
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.