0xced / XCDYouTubeKit

YouTube video player for iOS, tvOS and macOS
MIT License
2.92k stars 626 forks source link

Stop streaming of video when using AVPlayer #332

Closed chenghaoc closed 7 years ago

chenghaoc commented 7 years ago

I use following code mentioned in https://github.com/0xced/XCDYouTubeKit/issues/157 to get the stream of videos.

struct YouTubeVideoQuality {
    static let hd720 = NSNumber(value: XCDYouTubeVideoQuality.HD720.rawValue)
    static let medium360 = NSNumber(value: XCDYouTubeVideoQuality.medium360.rawValue)
    static let small240 = NSNumber(value: XCDYouTubeVideoQuality.small240.rawValue)
}

func playVideo(videoIdentifier: String?) {
    let playerViewController = AVPlayerViewController()
    self.present(playerViewController, animated: true, completion: nil)

    XCDYouTubeClient.default().getVideoWithIdentifier(videoIdentifier) { [weak playerViewController] (video: XCDYouTubeVideo?, error: Error?) in
        if let streamURLs = video?.streamURLs, let streamURL = (streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?? streamURLs[YouTubeVideoQuality.hd720] ?? streamURLs[YouTubeVideoQuality.medium360] ?? streamURLs[YouTubeVideoQuality.small240]) {
            playerViewController?.player = AVPlayer(url: streamURL)
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
}

But I cannot stop the stream when I dismiss or stop the player, is there related functions exposed?

chenghaoc commented 7 years ago

just pause the avplayer, remove player layer and set player to nil.