2013-04-05 90 views
2

我試圖在我的應用程序中實現嵌入式Youtube視頻。 我設法做到這一點的唯一方法是使用框架如LBYouTubeViewHCYotubeParser。從我讀過的內容來看,他們反對Youtube TOS,因爲他們基本上從http獲得類似this的視頻鏈接。如何嵌入不會在iPhone上啓動外接播放器的YouTube視頻?

這確實可以在MPMoviePlayerController中播放,沒有任何問題(也不需要離開應用程序)。

從我的搜索有兩個應用程序設法做到這一點VodioFrequency所以我想知道是否有一個特殊的開發人員或我可能錯過的聯盟計劃的SDK。

從我也讀過Youtube工程師迴應這個問題,如果他們用youtube-API標記,所以我希望你能清楚這一點。

我已閱讀了有關UIWebView的實現,但該場景實際上打開了我無法控制的外部視頻播放器。

+0

所以要嵌入實際的視頻到媒體播放器,而不是(說)一個iFrame或其他基於網絡的實施? – 2013-04-05 23:19:51

+0

是的,因爲點擊時的iFrame/Html代碼打開了外部播放器。 – 2013-04-06 08:54:14

+0

嗯。我能想到的唯一方法就是使用帶有無鉻播放器的UIWebView,您可以通過JavaScript保留控制權,也許... https://developers.google.com/youtube/getting_started – 2013-04-06 13:07:12

回答

0

我已經看了一段時間了,這是我想出了: 它涉及添加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]; 
相關問題