2012-01-30 2 views
28

iPhone 앱에서 YouTube 동영상을 재생하고 싶습니다. 다음 코드로 내 iPhone 앱에서 YouTube 동영상을 재생 해 보았습니다.UIWebView를 사용하지 않고 iPhone 앱에서 YouTube 동영상을 재생할 수 있나요?

[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)]; 

- (void)playVideo:(NSString *)urlString 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, urlString, frame.size.width, frame.size.height]; 
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame]; 
    [videoView loadHTMLString:html baseURL:nil]; 
    [self.view addSubview:videoView]; 
    [videoView release]; 
    NSLog(@"%@",html); 
} 

하지만이 코드는 저를 위해 작동하지 않습니다. 이것은 또한 나를 위해 작동하지 않습니다

NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"]; 
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
[self.view addSubview:moviePlayerController.view]; 
moviePlayerController.fullscreen = YES; 
[moviePlayerController play]; 

또한 내가, MPMoviePlayerController 코드가 함께 노력했다. 아무도 내가 잘못한 곳을 알려주고 나에게 어떤 아이디어를 제안 할 수 있습니까? UIWebView을 사용하는 대신 MPMoviewPlayerController에서 YouTube 동영상을 재생하고 싶습니다. 미리 감사드립니다.

+0

관련 항목 : http://stackoverflow.com/questions/1779511/play-youtube-videos-with-mpmovieplayercontroller-instead-of-uiwebview –

답변

6

Mr.Yuvaraj.M 위의 코드가 맞습니다. iPhone/iPod touch 장치에서 프로젝트를 실행하십시오. YouTube 동영상은 시뮬레이터에서 지원되지 않거나 작동하지 않을 수 있습니다. 이것에 확인하십시오. 감사.

+0

Mr.Gopinath 님의 답변에 감사드립니다. 나는 당신의 대답을 점검 할 것입니다. 감사. –

1

사용중인 HTML 소스 코드 대신 YouTube 웹 사이트로 이동하여 비디오를 찾고 공유 버튼을 누른 다음 소스 코드 버튼을 사용하여 소스 코드 예제를 얻으십시오. 현재 사용중인 플래시 버전이 아닌 동영상 퍼가기에 해당 패턴을 사용하십시오.

8

그냥 XCDYouTubeVideoPlayerViewController을 사용하십시오. UIWebView이 필요없이 원활하게 앱에 YouTube 비디오를 추가합니다.

점진적 다운로드 + MoviePlayerAVFoundation 프레임 워크를 사용합니다. YouTube 동영상 ID를 제공하면 잘 할 수 있습니다.

전체 화면이 아닌 전체 화면 &과 전체 화면이 아닌 iOS 시뮬레이터에서 작동합니다.

예 : YouTube Data API (v3)에 사용할 수있는 YTPlayerView 클래스는 지금이

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"]; 
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController]; 
+0

이 라이브러리를 pod로 추가하고이 헤더에 사용할 라이브러리를 추가하십시오. –

+0

@ R.Mohan은'#import ' – Hlung

관련 문제