我已經看了一段時間了,這是我想出了: 它涉及添加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,以便隨時創建一個副本。整個事情是[自我loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]];;工作,但從一個字符串加載NSUrlRequest不。
您在html文件中看到的Javascript函數用於控制視頻。如果您需要訪問時間或全時間段,你可以讓他們像這樣:
float VideoDuration = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getDuration()"] floatValue];
float VideoTime = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getTime()"] floatValue];
所以要嵌入實際的視頻到媒體播放器,而不是(說)一個iFrame或其他基於網絡的實施? – 2013-04-05 23:19:51
是的,因爲點擊時的iFrame/Html代碼打開了外部播放器。 – 2013-04-06 08:54:14
嗯。我能想到的唯一方法就是使用帶有無鉻播放器的UIWebView,您可以通過JavaScript保留控制權,也許... https://developers.google.com/youtube/getting_started – 2013-04-06 13:07:12