2013-09-26 49 views
12

Tôi có một lớp con UIWebView mà tôi đã sử dụng để phát cả video trên youtube và video cục bộ. Làm việc hoàn hảo dưới iOS6. Khi nâng cấp lên iOS7, tôi đã gặp sự cố Tôi không thực sự biết bắt đầu từ đâu. Trong khi lớp con dường như vẫn làm việc youtube chơi tốt và video cục bộ trên giả lập iOS7, khi tôi chơi một youtube trên thiết bị tôi nhận được các thông điệp sau đăng nhập:ios7 UIWebView Youtube Video

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextSetBlendMode: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextSetAlpha: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextTranslateCTM: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextScaleCTM: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextGetCTM: invalid context 0x0. 
This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextDrawImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

Sep 26 10:17:27 JRR-IPad SVOx[558] <Error>: CGContextGetCTM: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

===

này chỉ xảy ra trên thiết bị, không phải trên trình mô phỏng và không xuất hiện trong iOS6. Tôi đã nhìn thấy một số bài viết ở đây nói về các vấn đề tương tự với các lớp khác nhau trên bản beta, nhưng tôi đã không nhìn thấy ý kiến ​​về phiên bản phát hành iOS7.

Tôi không sử dụng bất kỳ phương pháp nào trong số các phương pháp này trong mã của mình, vì vậy các thư này có nguồn gốc trong thư viện.

Bất kỳ ai? TIA.

+0

Tôi cũng đang đối mặt với vấn đề này. Bạn đã nộp lỗi chưa? – san

+3

Tôi vừa gửi báo cáo lỗi # 15106115. – RegularExpression

+0

Bạn có tìm thấy giải pháp nào cho vấn đề này không. Hãy chia sẻ nếu bạn đã làm. Thanx –

Trả lời

1

Đây là mã thử nghiệm trên iOS7, làm việc của mình tốt

[self embedYouTube:[self getYTUrlStr:@"https://www.youtube.com/watch?v=Zq4o_Ca14aw"] frame:CGRectMake(0,40,self.view.frame.size.width, self.view.frame.size.height-40)]; 


- (NSString*)getYTUrlStr:(NSString*)url 
{ 
    if (url == nil) 
     return nil; 

    NSString *retVal = [url stringByReplacingOccurrencesOfString:@"watch?v=" withString:@"v/"]; 

    NSRange pos=[retVal rangeOfString:@"version"]; 
    if(pos.location == NSNotFound) 
    { 
     retVal = [retVal stringByAppendingString:@"?version=3&hl=en_EN"]; 
    } 
    return retVal; 
} 



- (void)embedYouTube:(NSString*)url frame:(CGRect)frame { 

    NSString* embedHTML = @"\ 
    <html><head>\ 
    <style type=\"text/css\">\ 
    body {\ 
    background-color: transparent;\ 
    color: white;\ 
    }\ 
    </style>\ 
    </head><body style=\"margin:0\">\ 
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ 
    width=\"%0.0f\" height=\"%0.0f\"></embed>\ 
    </body></html>"; 
    NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height]; 
    if(videoView == nil) { 
     videoView = [[UIWebView alloc] initWithFrame:frame]; 
     videoView.delegate=self; 
     [self.view addSubview:videoView]; 
    } 


    videoView.mediaPlaybackAllowsAirPlay=YES; 
    videoView.allowsInlineMediaPlayback=YES; 
    [videoView loadHTMLString:html baseURL:nil]; 

} 
+0

Nó không hiệu quả với tôi. Tôi đã tạo một ứng dụng mẫu chỉ với mã trong tính năng nhúng của bạnYouTube: frame: và sử dụng url NSString = @ "http://images.apple.com/movies/us/hd_gallery/gl1800/480p/warren_miller_m480p.mov" cho url . Nó vẫn cho cùng một lỗi khi nó bắt đầu chơi. – Yoga

0

Tôi đã sử dụng sau đây cách thức ứng dụng của tôi và làm việc của mình trong cả hai iOS6 và iOS 7. Có thể là tác phẩm của mình cho bạn.

NSString *youTubeID = [self extractYoutubeID:url]; // url is youtube url 
NSString *embedHTML =[NSString stringWithFormat:@"\ 
          <html><head>\ 
          <style type=\"text/css\">\ 
          body {\ 
          background-color: #666666;\ 
          padding:%f %f %f %f;\ 
          color: blue;\ 
          }\ 
          </style>\ 
          </head><body style=\"margin:0\">\ 
          <iframe height=\"%f\" width=\"%f\" title=\"YouTube Video\" class=\"youtube-player\" src=\"http://www.youtube.com/embed/%@\" ></iframe>\ 
          </body></html>",paddingTop,paddingRight,paddingBottom,paddingLeft,videoHeight,videoWidth,youTubeID]; 
[self.webView loadHTMLString:embedHTML baseURL:nil]; 




- (NSString *)extractYoutubeID:(NSString *)youtubeURL 
    { 
     //NSLog(@"youtube %@",youtubeURL); 
     NSError *error = NULL; 
     NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"; 
     NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error]; 
     NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])]; 
     if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) 
     { 
      NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch]; 
      return substringForFirstMatch; 
     } 
     return nil; 
    } 
0

Hãy thử điều này:

NSString* videoUrl = @"//www.youtube.com/embed/Of_hjbYCx4Y"; 
if([videoUrl hasPrefix:@"//"]) { 
    videoUrl = [NSString stringWithFormat:@"https:%@", videoUrl]; // or http 
    // if its a local file add proper scheme like "file:///" 
} 
NSString* iframeWidth = @"100%"; 
NSString* iframeHeight = @"240px"; 
NSString *iframeHTML =[NSString stringWithFormat:@"<iframe width=\"%@\" height=\"%@\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>", iframeWidth, iframeHeight, videoUrl]; 
// you can directly use iframeHTML below or u can wrap it up in body and span tags and then use it if u need to add any extra stuff or customize the page itself 
[videoWebView loadHTMLString:htmlString baseURL:nil]; 

Hãy chắc chắn rằng trong hai điều: video url bắt đầu với http (chương trình thích hợp) và thay thế /xem v = với nhúng/? nếu bạn không có nó trong url. Kiểm tra các câu trả lời khác được đưa ra ở đây để xem cách thực hiện điều này. Hy vọng điều đó sẽ hữu ích.

4

Sử dụng này, bạn có thể chạy youtube video theo quan điểm của bạn ..

UIWebView * videoView = [[UIWebView alloc] initWithFrame: self.view.bounds];

NSString *strparse = [NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@",youtubeVideoId]; 
    NSRange rng = [strparse rangeOfString: @"watch?v="]; 
    strparse = [strparse substringFromIndex:rng.location]; 
    rng = [strparse rangeOfString:@"&feature="]; 


    if (rng.length >0) 
    { 
     strparse = [strparse substringToIndex:rng.location]; 
    } 
    strparse = [strparse stringByReplacingOccurrencesOfString:@"watch?v=" withString:@""]; 
    //http://www.youtube.com/embed/FJkiwbqlSy0 
    NSString *youTubeVideoHTML =[NSString stringWithFormat:@"<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px; background:#1111}</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:'%0.0f', height:'%0.0f', videoId:'%@', events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } });} function onPlayerReady(event) { event.target.playVideo(); } function onPlayerStateChange(event) {if(event.data === 0) { event.target.destroy(); }} </script> </body> </html>",0.0, 0.0,youtubeVideoId]; 


    videoView.scalesPageToFit=NO; 
    videoView.mediaPlaybackRequiresUserAction=NO; 
    videoView.backgroundColor = [UIColor clearColor]; 
    videoView.opaque= NO; 


    [SharedObj showLoadingWithText:@""]; 

    [videoView loadHTMLString:youTubeVideoHTML baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@",youtubeVideoId]]]; 


    [self.view addSubview:videoView]; 
0

YouTube đã có sẵn một khuôn khổ để giúp chúng tôi tránh khỏi những rắc rối khi sử dụng chế độ xem web trực tiếp. Khuôn khổ có sẵn tại: https://github.com/youtube/youtube-ios-player-helper

Tất cả những gì bạn cần làm là tạo chế độ xem YTPlayerView, thêm nó vào phân cấp chế độ xem với các ràng buộc bạn thích và tải video. Giống như vậy:

youTubePlayer = YTPlayerView() 
    youTubePlayer!.translatesAutoresizingMaskIntoConstraints = false 
    contentView.addSubview(youTubePlayer!) 
    youTubePlayer!.pinAllAnchors(to: contentView) 
    youTubePlayer!.load(withVideoId: url.youTubeVideoId()!) 

Lưu ý rằng pinAllAnchors và youTubeVideoId là những người giúp tôi tạo. Chúng không phải là một phần của khuôn khổ.

Các vấn đề liên quan