Closed yoasha closed 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)
}
}
}
Thanks for the quick response! Do you have by any chance a sample code in objective-c?
@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];
}
}];
Thank you!!
Thank you soo much dude..
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":
(void)openVideo:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
NSString* videoIdentifier = [command.arguments objectAtIndex:0];
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]; } }]; }
Many Thanks!
@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: { }
@farabis4m Please give me the URL that is causing you issues
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?