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

Add method YouTubePlayer.toggleFullscreen #936

Merged
Show file tree
Hide file tree
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 @@ -113,6 +113,8 @@ class ChromecastYouTubePlayer internal constructor(private val chromecastCommuni
chromecastCommunicationChannel.sendMessage(message)
}

override fun toggleFullscreen() { }

override val listeners: Collection<YouTubePlayerListener> get() = youTubePlayerListeners
override fun addListener(listener: YouTubePlayerListener): Boolean = youTubePlayerListeners.add(listener)
override fun removeListener(listener: YouTubePlayerListener): Boolean = youTubePlayerListeners.remove(listener)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.pierfrancescosoffritti.androidyoutubeplayer.core.sampleapp.examples.fullscreenExample

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.FullScreenListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
import com.pierfrancescosoffritti.aytplayersample.R

class FullscreenExampleActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fullscreen_example)

val youTubePlayerView = findViewById<YouTubePlayerView>(R.id.youtube_player_view)
val fullScreenViewContainer = findViewById<FrameLayout>(R.id.full_screen_view_container)

val iFramePlayerOptions = IFramePlayerOptions.Builder()
.controls(1) // enable full screen button
.fullscreen(1)
.build()

// we need to initialize manually in order to pass IFramePlayerOptions to the player
youTubePlayerView.enableAutomaticInitialization = false

youTubePlayerView.addFullScreenListener(object : FullScreenListener {
override fun onEnterFullScreen(fullScreenView: View, exitFullScreen: Function0<Unit>) {
// the video will continue playing in fullScreenView
youTubePlayerView.visibility = View.GONE
fullScreenViewContainer.visibility = View.VISIBLE
fullScreenViewContainer.addView(fullScreenView)

// optionally request landscape orientation
// requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}

override fun onExitFullScreen() {
// the video will continue playing in the player
youTubePlayerView.visibility = View.VISIBLE
fullScreenViewContainer.visibility = View.GONE
fullScreenViewContainer.removeAllViews()
}
})

youTubePlayerView.initialize(object : AbstractYouTubePlayerListener() {
override fun onReady(youTubePlayer: YouTubePlayer) {
super.onReady(youTubePlayer)
youTubePlayer.loadVideo("7NK_JOkuSVY", 0f)

val enterFullscreenButton = findViewById<Button>(R.id.enter_fullscreen_button)
enterFullscreenButton.setOnClickListener {
youTubePlayer.toggleFullscreen()
}
}
}, iFramePlayerOptions)

lifecycle.addObserver(youTubePlayerView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/enter_fullscreen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="toggle full screen"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ interface YouTubePlayer {

fun setPlaybackRate(playbackRate: PlayerConstants.PlaybackRate)

/**
* Tries to enter or exit fullscreen in the player.
*
* Might require setting the `origin` parameter to "https://www.youtube.com".
*/
fun toggleFullscreen()

fun addListener(listener: YouTubePlayerListener): Boolean
fun removeListener(listener: YouTubePlayerListener): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class IFramePlayerOptions private constructor(private val playerOptions: JSONObj
private const val CONTROLS = "controls"
private const val ENABLE_JS_API = "enablejsapi"
private const val FS = "fs"
const val ORIGIN = "origin"
internal const val ORIGIN = "origin"
private const val REL = "rel"
private const val SHOW_INFO = "showinfo"
private const val IV_LOAD_POLICY = "iv_load_policy"
Expand Down Expand Up @@ -129,8 +129,11 @@ class IFramePlayerOptions private constructor(private val playerOptions: JSONObj
}

/**
* Controls domain as the origin parameter value.
* @param origin your domain
* This parameter specifies the domain from which the player is running.
* Since the player in this library is not running from a website there should be no reason to change this.
* Using "https://www.youtube.com" (the default value) is recommended as some functions from the IFrame Player are only available
* when the player is running on a trusted domain.
* @param origin your domain.
*/
fun origin(origin: String): Builder {
addString(ORIGIN, origin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ internal class WebViewYouTubePlayer constructor(
mainThreadHandler.post { loadUrl("javascript:setPlaybackRate(${playbackRate.toFloat()})") }
}

override fun toggleFullscreen() {
loadUrl("javascript:toggleFullscreen()")
}

override fun destroy() {
youTubePlayerListeners.clear()
mainThreadHandler.removeCallbacksAndMessages(null)
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/res/raw/ayp_youtube_player.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,9 @@
player.setPlaybackRate(playbackRate);
}

function toggleFullscreen() {
player.toggleFullscreen();
}

</script>
</html>