要播放YouTube视频,我正在使用
-(void)playYoutubeVideo:(NSString *)youtubeUrl {
NSString *videoId = [self extractYoutubeIdFromLink:youtubeUrl];
NSLog(@"video Id %@",videoId);
[self playVideoWithId:videoId];
}
- (NSString *)extractYoutubeIdFromLink:(NSString *)link {
NSString *regexString = @"((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:regexString
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *array = [regExp matchesInString:link options:0 range:NSMakeRange(0,link.length)];
if (array.count > 0) {
NSTextCheckingResult *result = array.firstObject;
return [link substringWithRange:result.range];
}
return nil;
}
- (void)playVideoWithId:(NSString *)videoId {
NSString *youTubeVideoHTML = @"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%f', height:'%f', videoId:'%@', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML,300,200, videoId];
[webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
}
我从手表网址获取了YouTube视频ID。
它适用于https://www.youtube.com/watch?v=p9mddDM43wE
等网址但是,以下youtube视频无法在UIWebView中播放
https://www.youtube.com/watch?v=CT-fPP1fjDA
显示以下错误,
可能有什么问题?
有人可以澄清一下吗?
答案 0 :(得分:0)
最近,我在iOS应用程序中遇到了同样的问题,我们正在result = urlfetch.fetch('{}/users/{}.json?api_key={}&api_username={}'.format(domain, username, discourse_api_key, discourse_api_username))
内加载YouTube视频链接。
长时间打破头后,原来是设备“日期和时间”设置问题。
设备日期设置为非常旧。更新设置后,YouTube视频开始加载到网络视图中。