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

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

Open
jancassio opened this issue Jul 20, 2021 · 1 comment
Open

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

jancassio opened this issue Jul 20, 2021 · 1 comment

Comments

@jancassio
Copy link

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
Copy link

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)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants