dailymotion / dailymotion-swift-player-sdk-ios

Dailymotion Player SDK for iOS in Swift
MIT License
34 stars 15 forks source link

How to use Daily Motion player with Swift UI properly #63

Open jancassio opened 3 years ago

jancassio commented 3 years ago

Hi all,

I been working in a project where we have to use Daily Motion player in a iOS and Android app. I'll focus only in iOS here.

The app is targeted to iOS 14.5 and it use SwiftUI to compose user interface.

I'm getting several problems to handle Daily Motion player using SwiftUI, specially to manage when and how to stop or continue playing a video.

I did a sample project here. That project can be used to explore some of these issues.

First one is related to initializer. There are some params like autoplay that never work. The video always start playing, even if I try to force it to stop after load callback.

Second is related to lifecycle. There's no way to stop playing after a view disappear or another view been presented over another. Even using dismantleUIViewController from UIViewControllerRepresentable, the player doesn't respond to playback commands and sometimes, its reference is lost.

I kept this sample simple as possible to ask you help to figure out what I have to do to make it more functional for SwiftUI apps.

Thanks

floriangbh commented 1 year ago

Work great for me with this :

struct PlayerVideoView: UIViewControllerRepresentable {
    class Coordinator: NSObject, DMPlayerViewControllerDelegate {
        var parent: PlayerVideoView

        init(_ parent: PlayerVideoView) {
            self.parent = parent
        }

        func player(_ player: DMPlayerViewController, didReceiveEvent event: PlayerEvent) {
            //print("\(#function): \(event)")
        }

        func player(_ player: DMPlayerViewController, openUrl url: URL) {
            print("\(#function): \(url)")
        }

        func playerDidInitialize(_ player: DMPlayerViewController) {
            print("\(#function)")
        }

        func player(_ player: DMPlayerViewController, didFailToInitializeWithError error: Error) {
            print("\(#function): \(error.localizedDescription)")
        }
    }

    typealias UIViewControllerType = DMPlayerViewController

    let videoId: String

    init(withId videoId: String) {
        self.videoId = videoId
    }

    func makeUIViewController(context: Context) -> DMPlayerViewController {
        let player = DMPlayerViewController(parameters: [:])
        player.delegate = context.coordinator
        return player
    }

    func updateUIViewController(_ player: DMPlayerViewController, context: Context) {
        let parameters = ["autoplay": false]
        guard let encoded = try? JSONEncoder().encode(parameters),
              let params = String(data: encoded, encoding: .utf8) else { return }
        player.load(videoId: videoId, params: params) {
            print("[DMPlayerView.updateUIViewController] load completion")
        }
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
}