gpt4 book ai didi

ios - 如何在 iPhone 中播放 Youtube 视频

转载 作者:可可西里 更新时间:2023-11-01 17:02:36 24 4
gpt4 key购买 nike

我想通过应用程序在 iPhone 中播放 Youtube 视频。我尝试使用 MPMoviePlayerController,但没有加载视频。它正在播放我从 bundle 中加载的视频,而不是 you tube 视频。我还尝试通过将 Youtube 视频嵌入到 UIWebView 中来播放它,但这也是徒劳的。谁能建议我应该采取的方法。我正在 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-

- (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];
}

关于ios - 如何在 iPhone 中播放 Youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5818585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com