SvenTiigi / YouTubePlayerKit

A Swift Package to easily play YouTube videos 📺
https://sventiigi.github.io/YouTubePlayerKit/
MIT License
717 stars 64 forks source link

Inline YouTube video is not playing in Picture-in-Picture mode even after setting allowsPictureInPictureMediaPlayback and playsinline properties to true #88

Open Dhrumil-Dhameliya opened 7 months ago

Dhrumil-Dhameliya commented 7 months ago

What happened?

I have enabled background modes in capabilities and set player configuration properties 'playsinline' and 'allowsPictureInPictureMediaPlayback' to true. However, when the app goes into the background, the video is not playing in Picture-in-Picture mode, and the app stops playing the video completely.

Picture-in-Picture mode is working properly when the app is playing the video in fullscreen mode. Please have a look at the code and the attached screenshot.

It seems that setting 'allowsPictureInPictureMediaPlayback' to true or false is not making any difference in the way the library plays the video.

please have a look at the code and attachement.

If there are any missing settings in the configuration or if any other settings are needed in the code, please let me know. Additionally, if Picture-in-Picture (PIP) is not possible even after setting above properties to true, please inform me as well.

Thank you for the great library.

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback)
            try AVAudioSession.sharedInstance().setActive(true, options: [])
        } catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed.")
        }
        return true
    }
}

override func viewDidLoad() {
        super.viewDidLoad()
        let configuration = YouTubePlayer.Configuration(
            // Define which fullscreen mode should be used (system or web)
            allowsPictureInPictureMediaPlayback : true,
            fullscreenMode: .system,
            // Custom action to perform when a URL gets opened
            // Enable auto play
            autoPlay: true,
            // Hide controls
            showControls: true,
            // Enable loop
            loopEnabled: true,
            playInline: true

        )
        let source = YouTubePlayer.Source.video(id: "tlfmyWpI9yA")
        let player = YouTubePlayer.init(source: source, configuration: configuration)
        let hostingView = YouTubePlayerHostingView.init(player: player)
        ytPlayerView.addSubview(hostingView)

        hostingView.snp.makeConstraints { make in
            make.edges.equalTo(ytPlayerView)
        }
        player.play()
    }

Screenshot 2024-04-18 at 7 28 37 PM

What are the steps to reproduce?

Enable Background modes for Picture-in-Picture (PIP) playback, set 'playsinline' and 'allowsPictureInPictureMediaPlayback' to true, start playing the video, then go to background mode.

What is the expected behavior?

As per your comments regarding Picture-in-Picture issues, after enabling Picture-in-Picture mode and setting 'playsinline' and 'allowsPictureInPictureMediaPlayback' properties, the app should automatically play the video in Picture-in-Picture mode when it goes into the background.

666bsw commented 1 month ago

hi! was the problem solved? if yes, tell me how? Best regards