我环顾四周,这就是我想出的:它涉及使用以下代码将 youtube.html 文件添加到您的项目中:
<html>
<head><style>body{margin:0px 0px 0px 0px;}</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',
{
width: '640',
height: '360',
videoId: '%@',
playerVars: {'autoplay' : 1, 'controls' : 0 , 'vq' : 'hd720', 'playsinline' : 1, 'showinfo' : 0, 'rel' : 0, 'enablejsapi' : 1, 'modestbranding' : 1},
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.ENDED)
{
window.location = "callback:anything";
};
}
function stopVideo()
{
player.stopVideo();
window.location = "callback:anything";
}
function getTime()
{
return player.getCurrentTime();
}
function getDuration()
{
return player.getDuration();
}
function pause()
{
player.pauseVideo();
}
function play()
{
player.playVideo();
}
</script>
</body>
</html>
您还必须创建一个 UiWebView 子类,它也是 UIWebViewDelegate 的委托
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self == nil) return nil;
self.mediaPlaybackRequiresUserAction = NO;
self.delegate = self;
self.allowsInlineMediaPlayback = TRUE;
self.userInteractionEnabled = FALSE;
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) objectAtIndex:0];
NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube2.html"];
[htmlData writeToFile:targetPath atomically:YES];
[self loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]];
File = 0;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:@"callback"])
{
Playing = FALSE;
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *targetPath = [documentsDirectoryPath stringByAppendingPathComponent:@"youtube2.html"];
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:targetPath error:&error];
}
return YES;
}
基本上它会创建一个 UIWebView 并将代码加载到 youtube.html 文件中。因为 youtube.html 是静态的,我需要加载某个 id,所以我在文档文件夹 youtube2.html 中动态创建了一个副本,我在其中添加了字符串 id。
整个事情就是 [self loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]]; 有效,但从字符串加载 NSUrlRequest 无效。
您在 html 文件中看到的 Javascript 函数用于控制视频。如果您需要访问时间或完整持续时间,您可以像这样获得它们:
float VideoDuration = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getDuration()"] floatValue];
float VideoTime = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getTime()"] floatValue];