0xced / XCDYouTubeKit

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

Crash on iOS 13 #445

Closed yoasha closed 5 years ago

yoasha commented 5 years ago

Adding support of iOS 13 to my app, I get an exception when trying to play a Youtube movie. Specifically when calling the following line: [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:param];

This is the Xcode output: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'MPMoviePlayerViewController is no longer available. Use AVPlayerViewController in AVKit.'

Using Xcode 11 Beta 5 (latest version for now)

Any suggestions?

SoneeJohn commented 5 years ago

@yoasha You need to use XCDYouTubeClient class like this:

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

    XCDYouTubeClient.defaultClient().getVideoWithIdentifier(videoIdentifier) { [weak playerViewController] (video: XCDYouTubeVideo?, error: NSError?) in
        if let streamURL = (video?.streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ??
                            video?.streamURLs[XCDYouTubeVideoQuality.HD720.rawValue] ??
                            video?.streamURLs[XCDYouTubeVideoQuality.Medium360.rawValue] ??
                            video?.streamURLs[XCDYouTubeVideoQuality.Small240.rawValue]) {
            playerViewController?.player = AVPlayer(URL: streamURL)
        } else {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
}
yoasha commented 5 years ago

Thanks for the quick response! Do you have by any chance a sample code in objective-c?

SoneeJohn commented 5 years ago

@yoasha Sure:

AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[self presentViewController:playerViewController animated:YES completion:nil];

__weak AVPlayerViewController *weakPlayerViewController = playerViewController;
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:videoIdentifier completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) {
    if (video)
    {
        NSDictionary *streamURLs = video.streamURLs;
        NSURL *streamURL = streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?: streamURLs[@(XCDYouTubeVideoQualityHD720)] ?: streamURLs[@(XCDYouTubeVideoQualityMedium360)] ?: streamURLs[@(XCDYouTubeVideoQualitySmall240)];
        weakPlayerViewController.player = [AVPlayer playerWithURL:streamURL];
        [weakPlayerViewController.player play];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}];
yoasha commented 5 years ago

Thank you!!

GSKoti0007 commented 4 years ago

Thank you soo much dude..

caiquestow commented 4 years ago

hey guys, some one could help me? i have tried to implement this, but i am still failing..

i need to substitute this code in my function "openVideo", on YoutubeVideoPlayer.m. am i right? i am making something like this, but i have errors with "presentViewController" and "Animated":

Many Thanks!

farabis4m commented 4 years ago

@SoneeJohn i am getting this error:-

[XCDYouTubeKit] Video operation finished with error: The operation couldn’t be completed. (XCDYouTubeVideoErrorDomain error -2.) Domain: XCDYouTubeVideoErrorDomain Code: -2 User Info: { }

SoneeJohn commented 4 years ago

@farabis4m Please give me the URL that is causing you issues