swift YoutubePlayer.swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift YoutubePlayer.swift相关的知识,希望对你有一定的参考价值。

import UIKit

// YouTubePlayer.playYoutubeID("dk8aUNobxGY", viewController: self)

class YouTubePlayer: NSObject {

    static var youtubePlayerViewController: YoutubePlayerViewController?
    static var youtubePlayerDelegate: PlayerDelegate?

    class func playYoutubeID(_ youtubeId: String, viewController: UIViewController) {
        if self.youtubePlayerViewController == nil {
            self.youtubePlayerViewController = YoutubePlayerViewController()
            self.youtubePlayerDelegate = PlayerDelegate()
            self.youtubePlayerViewController!.delegate = youtubePlayerDelegate
        }
        youtubePlayerViewController!.playVideo(youtubeId, viewController: viewController)
    }
}

class PlayerDelegate: YoutubePlayerDelegate {
    func playerDidFinish() {
    }
}
import UIKit
import XCDYouTubeKit

protocol YoutubePlayerDelegate: class {
    func playerDidFinish()
}

class YoutubePlayerViewController: UIViewController {

    weak var delegate: YoutubePlayerDelegate?
    var videoPlayerViewController: XCDYouTubeVideoPlayerViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func playVideo(_ youtubeId: String, viewController: UIViewController) {
        self.videoPlayerViewController = XCDYouTubeVideoPlayerViewController(videoIdentifier: youtubeId)
        NotificationCenter.default.addObserver(self, selector: #selector(YoutubePlayerViewController.moviePlayerPlaybackDidFinish(_:)),
                                               name: NSNotification.Name.MPMoviePlayerPlaybackDidFinish,
                                               object: self.videoPlayerViewController!.moviePlayer)
        viewController.present(self.videoPlayerViewController!, animated: true) {
        }
    }

    @objc func moviePlayerPlaybackDidFinish(_ notification: Notification) {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.MPMoviePlayerPlaybackDidFinish, object: nil)
        if let finishReason: MPMovieFinishReason = (notification.userInfo![MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]! as AnyObject).int32Value as? MPMovieFinishReason {
            if finishReason == .playbackError {
                if let error = notification.userInfo![XCDMoviePlayerPlaybackDidFinishErrorUserInfoKey] {
                    print(error)
                }
            }
        }
        delegate?.playerDidFinish()
    }
}

以上是关于swift YoutubePlayer.swift的主要内容,如果未能解决你的问题,请参考以下文章

Swift 反射

Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

swift 示例BS swift.swift

swift swift_bug.swift

ios 整理(一)swift和oc的区别

swift swift_extension5.swift