youtube / youtube-ios-player-helper

Lightweight helper library that allows iOS developers to add inline playback of YouTube videos through a WebView
Other
1.63k stars 674 forks source link

How to add Picture in Picture Mode #424

Open HarishIOSDeveloper opened 3 years ago

HarishIOSDeveloper commented 3 years ago

How to add Picture in Picture Mode using WKYTPlayer in ios swift

HyunjoonKo commented 3 years ago

https://github.com/HyunjoonKo/youtube-ios-player-helper

I simply added the PiP function. After setting AVAudioSession to enable PiP, load the video. The PiP runs after that, and can receive the state value with YTPlayerViewDelegate.

@IBOutlet weak var playerView: YTPlayerView!

...

playerView.pictureInPicture() // PiP ON/OFF

// in YTPlayerViewDelegate
func playerView(_ playerView: YTPlayerView, didChangeToStatePictureInPicture state: String?) {        
        let enabled = (state == "picture-in-picture") ? true : false
        enable(pictureInPicture: enabled) // Update UI.
}
ShubhamSingla125 commented 2 years ago

@HyunjoonKo Can you please share the sample code?

Jitheshxavier commented 1 year ago

@HyunjoonKo How can we track when the player moves to Pip and vice versa.

typedef NS_ENUM(NSInteger, YTPlayerState) { kYTPlayerStateUnstarted, kYTPlayerStateEnded, kYTPlayerStatePlaying, kYTPlayerStatePaused, kYTPlayerStateBuffering, kYTPlayerStateCued, kYTPlayerStateUnknown };