这里是@零的梦幻般的答案供你使用一个完整的类:
@interface YouTubeWebView() <UIWebViewDelegate>
@end
@implementation YouTubeWebView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self == nil) return nil;
self.mediaPlaybackRequiresUserAction = NO;
self.delegate = self;
self.alpha = 0;
return self;
}
- (void)loadVideo:(NSString *)videoId
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"youtube" ofType:@"html"];
// if (filePath == nil)
NSError *error;
NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
// TODO: error check
string = [NSString stringWithFormat:string, videoId];
NSData *htmlData = [string dataUsingEncoding:NSUTF8StringEncoding];
// if (htmlData == nil)
NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube.html"];
[htmlData writeToFile:targetPath atomically:YES];
[self loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:@"callback"]) {
[self removeFromSuperview];
NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube.html"];
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:targetPath error:&error];
// TODO: error check
}
return YES;
}
只要使用这个版本youtube.html的,它取代视频ID的有一个替换代码(%@
) :
<html>
<head><style>body{margin:0px 0px 0px 44px;}</style></head>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '320',
width: '480',
videoId: '%@',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
if (event.data == YT.PlayerState.ENDED) {
window.location = "callback:anything";
};
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
唯一的主要障碍,我不得不克服实现这个被加载文件作为一个字符串进行替换。不幸的是,它必须再次写成自动播放工作的文件。如果那是没有必要为你的使用情况,随时对HTML直接作为字符串加载到Web视图来代替。
但它取决于如果YouTube播放器支持这样的事件,不是吗? 你成功了吗? – user1105951 2012-05-07 12:53:22
@ user1105951是,YouTube播放器支持结束事件称为“onStateChange”,你要添加一个侦听它符合YouTube的JavaScript API [YouTube上的JavaScript API - 活动(https://developers.google.com/youtube/js_api_reference#Events ) – ZeR0 2012-05-07 15:46:32
@Zero,我检查你的链接,我记得alreay测试它。 尝试运行这个演示https://developers.google.com/youtube/youtube_player_demo-ON THE IPAD- Safari网络浏览器,你会看到:“您需要Flash Player 9+并启用javascript才能观看此视频”。 – user1105951 2012-05-07 19:43:25