问题描述
我想在应用程序中在iPhone中播放Youtube视频。我尝试使用MPMoviePlayerController,但没有加载视频。它正在播放我从视频集中加载的视频,但不播放视频。我也尝试通过将它嵌入UIWebView来播放Youtube视频,但这也是徒劳的。任何人都可以建议我采取一种方法。我在IOS 4.2中测试它
I wanted to play a Youtube video in the iPhone from within the application. I tried using the MPMoviePlayerController, but was not loading the video. It is playing the video which I loaded from the bundle but not the you tube video. I also tried to play the Youtube video by embedding it in the UIWebView, but that also was in vain. Can anybody suggest me an approach that i should take. i am testing it in the IOS 4.2
这是我使用的代码。
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if ([mp respondsToSelector:@selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else
{
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
推荐答案
在iOS上播放youtube视频的唯一方法是使用默认的YouTube播放器播放它们。您应该将视频嵌入到uiwebview中,当您点击/点击它时,默认播放器将打开,请参阅此代码以在uiwebview中嵌入视频 -
The only way to play youtube videos on iOS is to play them using default youtube player. You should embed the video in a uiwebview and when you tap/click on it the default player will open refer this code to embed video in uiwebview-
- (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];
}
这篇关于如何在iPhone中播放Youtube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!