UIWebView 대신 MPMoviePlayerController로 YouTube 동영상 재생
MPMoviePlayerController를 사용하여 일부 youTube 비디오를 스트리밍하려고하는데 문제가 있습니다. 내가 사용하는 코드는 매우 간단하며 URL을 initWithContentURL에 전달하여 .m4v 동영상을 재생할 수 있습니다. 동영상 플레이어를 실행하면 플레이어가 나오지만 약 20 초 후에 사라집니다. 시뮬레이터에서 시도하면 서버가 올바르게 구성되지 않았다는 경고보기가 나타납니다. Google에서 특정 유형의 비디오 피드를 가져 오기 위해 URL과 함께 전달해야하는 인수가 있습니까?
NSURL *videoURL = [NSURL URLWithString:@"http://www.youtube.com/v/HGd9qAfpZio&hl=en_US&fs=1&"];
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer play];
다음 URL의 http://www.youtube.com/watch?v=HGd9qAfpZio 도 시도했습니다 .
나는 또한 & format = 1 인수를 보았고 그것을 두 문자열의 끝에 추가하려고했지만 운이 없었습니다.
자신의 앱에서 YouTube 동영상을 재생하는 유일한 방법 UIWebView
은 UIWebView's
콘텐츠 로 재생하려는 영화에 대해 Youtube의 embed 태그 를 사용하여를 만드는 것 입니다 . UIWebView
삽입 된 개체가 Youtube 링크임을 감지하고 웹보기의 콘텐츠가 동영상의 YouTube 미리보기가됩니다. 사용자가 미리보기를 클릭하면 비디오가 MPMoviePlayerController에 표시됩니다. 이것은 Muxecoid가 제공 한 링크 ( 애플리케이션 내에서 YouTube 비디오를 재생하는 방법)에 설명 된 기술 이며, 이것이 내가 아는 한 애플리케이션 내에서 YouTube 비디오를 재생하는 유일한 방법입니다. Youtube URL을에 전달하여 Youtube 응용 프로그램을 시작할 수 -[UIApplication openURL:]
있지만 물론 이것은 종종 바람직하지 않은 자체 응용 프로그램을 닫습니다.
불행히도 MPMoviePlayerController
YouTube는 비디오 파일에 대한 직접 링크를 노출하지 않기 때문에 YouTube 비디오를 직접 재생할 방법 이 없습니다.
코드를 사용하는 경우 :
- (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];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
출처:
시뮬레이터가 아닌 장치에서 테스트해야합니다. 시뮬레이터는 항상 파란색 상자로 물음표를 표시하기 때문에 (퀵타임 플레이어가 없습니다).
위에서 언급 한대로 작업중인 프로젝트에서이 작업을 수행해야했습니다. 이 문제를 해결하고 github에 코드를 제출했습니다.
기본적으로 YouTube URL을 사용하고 모든 iOS 호환 비디오 URL을 가져옵니다.
이것이 도움이되기를 바랍니다!
건배
.NET 파일에 YouTube
동영상 을 넣을 수있는 사람을 발견 했습니다 MPMoviePlayerController
. 지금은 가능해 보입니다.
To play you tube videos you need to extract the url before passing the url into MPMoviePlayer
. You can't play it directly.
My github repo has a demo implementation of this.
See:
https://github.com/DpzAtMicRO/IOSYoutubePlayer
Try it, this makes it possible to load and play youtube video directly in MPMoviePlayer
like any other video and is pretty good approach too.
EDIT: Make sure that you go through the Readme.md well before using this in your project
It's as simple as taking the src element of the iframe for the new embed code and calling
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
Check the following link. I think it will help you.
https://mobiletechfeast.blogspot.in/2014/08/youtube-player-for-ios.html
There is an official way to embed YouTube video in iOS application which is provided by Google.
Install library via cocoapods: pod "youtube-ios-player-helper"
import by: #import “YTPlayerView.h”
Use UIView
and set class to YTPlayerView
or create object of YTPlayerView
programatically.
Load video: [self.playerView loadWithVideoId:@"M7lc1UVf-VE"];
There is also playVideo
and stopVideo
methods available. For more info visit this Link
'Programing' 카테고리의 다른 글
명령 줄 매개 변수에서 "-"(대시)의 마법은 무엇입니까? (0) | 2020.09.08 |
---|---|
require 문없이 웹팩을 사용하여 디렉토리의 모든 파일을로드하는 방법 (0) | 2020.09.08 |
Octave / Matlab : 벡터를 확장하여 자체적으로 반복되도록 하시겠습니까? (0) | 2020.09.08 |
여러 항목에 의한 mysql 쿼리 순서 (0) | 2020.09.08 |
'std :;' (0) | 2020.09.08 |