Youtube在webview + iOS中嵌入了视频回调

时间:2017-02-19 06:14:15

标签: ios video webview youtube

我有一个代码,可以通过HTML在网页视图中嵌入YouTube视频。视频播放效果非常好。我想要做的是当用户暂停,关闭,播放视频时,在Objective c中获得回调。任何帮助?还有许多其他问题,但没有一个问题解释了如何修改javascript和Objective C- Callback。作为一个新的我需要一个详细的答案 这是我的代码

- (void)playVideoWithId:(NSString *)videoId {
    static NSString *youTubeVideoHTML = @"<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%0.0f', height:'%0.0f', videoId:'%@', events: { 'onReady': onPlayerReady, } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
    NSString *html = [NSString stringWithFormat:youTubeVideoHTML, self.view.frame.size.width, self.view.frame.size.height, videoId];
    videoWebview.mediaPlaybackRequiresUserAction = NO;
    [self.view bringSubviewToFront:videoWebview];
    [videoWebview loadHTMLString:html baseURL:nil];
}

0 个答案:

没有答案