2

我正在尝试在我的应用程序中实现嵌入式 Youtube 视频。我设法做到这一点的唯一方法是使用LBYouTubeViewHCYotubeParser等框架。从我读到的内容来看,他们反对 Youtube TOS,因为他们基本上从 http 中剥离了视频链接,得到了类似的东西

这确实可以在 MPMoviePlayerController 中毫无问题地播放(并且无需离开应用程序)。

从我搜索的内容来看,有两个应用程序成功地完成了VodioFrequency,所以我想知道是否有针对开发人员的特殊 sdk 或我可能错过的附属计划。

从我还读到的内容中,如果 Youtube 工程师被标记为 youtube-api,他们会回答这个问题,所以我希望你能解决这个问题。

我已经阅读了有关 UIWebView 实现的信息,但该场景实际上打开了一个我无法控制的外部视频播放器。

4

1 回答 1

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。

整个事情就是 [self loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:targetPath]]]; 有效,但从字符串加载 NSUrlRequest 无效。

您在 html 文件中看到的 Javascript 函数用于控制视频。如果您需要访问时间或完整持续时间,您可以像这样获得它们:

float VideoDuration = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getDuration()"] floatValue];
float VideoTime = [[YT_WebView stringByEvaluatingJavaScriptFromString:@"getTime()"] floatValue];
于 2013-06-13T12:41:05.080 回答