2

以前は(iOS 6で)、YouTubeプレーヤーに幅と高さを「伝える」ことができ、問題はありませんでした。

このコードは以前は iOS 6 で機能していましたが、iOS 7 ではまったく機能しなくなりました

しかし今では、画面自体よりも高さと幅にはるかに (3 倍) 高い値を入力する必要がありますが、デバイスのサイズが異なるため、もちろんそれは解決策ではありません。

これは、ボタンが押されたときに実行されるコードです

NSString *player = @"<!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.0fpx', height:'%0.0fpx', videoId:'%@',
   playerVars: {playsinline : 1, rel:0, showinfo:0}, events: { 'onReady': onPlayerReady, } });
 } 
   function onPlayerReady(event) { event.target.playVideo(); 
} 
</script> 
</body> 
</html>"; //End of player creation string

//Then I create the html with the size of the screen
NSString *html = [NSString stringWithFormat:player,[[UIScreen mainScreen] bounds].size.width , [[UIScreen mainScreen] bounds].size.height , @"5bCRDI1BESc"];

//And then I load the html into the webview
[self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];

ここから生成される HTML は次のとおりです。

<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> 
<body> 
<iframe id="player" frameborder="0" allowfullscreen="1" title="YouTube video player" width="320px" height="480px" src="http://www.youtube.com/embed/5bCRDI1BESc?playsinline=1&amp;rel=0&amp;showinfo=0&amp;enablejsapi=1"></iframe> <script src="http://s.ytimg.com/yts/jsbin/www-widgetapi-vflvlw_TO.js" async=""></script><script src="http://www.youtube.com/player_api"></script><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:'320px', height:'480px', 
videoId:'5bCRDI1BESc',
playerVars: {playsinline : 1, rel:0, showinfo:0}, 
events: { 'onReady': onPlayerReady, } }); 
} 
function onPlayerReady(event) { event.target.playVideo(); } </script>  </body></html>

しかし結果は、プレイヤーがホワイトスペース (webview) に入力する iOS 6 とは異なります。ここに写真があります

プレイヤーがスペースを埋めていない

次に、HTML に入る幅と高さを 640px と 960px に(画面サイズ!) 2 倍にすると!?

画面サイズを2倍にし、スペースを埋めません

iOS 7 で YouTube API または Safari ブラウザに何が起こったのか知っている人はいますか?

4

5 に答える 5

1

これは私の現在の実装です:

let webView = UIWebView(...)

// get the ID of the video you want to play
let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg

// Replace the height and width of the player here to match your UIWebView's  frame rect
let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='\(self.view.frame.size.width)' height='\(self.view.frame.size.height)' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>"

// Load your webView with the HTML we just set up
webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)

これは a の内部にあり、ビュー コントローラーのフレームにプレーヤーの幅と高さをUIViewController渡しています。viewそれに応じてプレーヤーのサイズを変更する必要があります。

于 2016-02-11T03:50:31.030 に答える
0

私はhtmlYouTubeプレーヤーをから頻繁にUIWebView使用し、このコードを使用しています。

NSString* embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.youtube.com/v/%@\" type=\"application/x-shockwave-flash\" \
width=\"240\" height=\"150\"></embed>\
</body></html>";

NSString* url = [NSURL URLWithString:_detailModel.video];
NSString* html = [NSString stringWithFormat:embedHTML, url];
nurl = [NSURL URLWithString:html];

[_videoWebView loadHTMLString:html baseURL:nil];

それが言うところwidth=\"240\" height=\"150\はサムネイルサイズです。ビデオの実際のサイズは、デフォルトで画面全体に設定されます。これは YouTube 動画では問題なく機能しますが、他の動画 (vimeo など) を取り出す必要がありますtype=\"application/x-shockwave-flash\"。それが役に立てば幸い。

于 2014-03-17T04:43:16.627 に答える
0

考慮事項 -特に 2012 年 7 月以降、 YouTube の iFrame Api にいくつかの変更が加えられています。https: //developers.google.com/youtube/iframe_api_reference を参照

player_api は廃止され、iFrame_api に置き換えられました。

特に

tag.src = \"http://www.youtube.com/player_api\"; 

に置き換えられます

 tag.src = "https://www.youtube.com/iframe_api";

  function onYouTubePlayerAPIReady() 

に置き換えられます

  function onYouTubeIframeAPIReady()

両方がしばらくサポートされることが指定されているため、使用したパラメーターの一部が古い API で使用できず、問題を引き起こしている可能性があると思われます-

playerVars: {playsinline : 1, rel:0, showinfo:0} は、古い API が廃止されてから丸 2 年経った 2014 年頃の比較的新しいものです。

免責事項: 私のアプリの最初のバージョンには YouTube プレーヤーが組み込まれていましたが、IOS 6.0 以降、m.youtube.com を発見し、コーディングしていたものよりもコントロールが少し良くなったので、それを使い続けています。したがって、これをテストすることはできませんが、あなたの場合は試す価値があると思います.

以下は、使用するドキュメントの抜粋です。

2012 年 7 月 19 日

この更新プログラムには、次の変更が含まれています。

要件セクションが更新され、IFrame API を使用するすべての Web ページで onYouTubeIframeAPIReady 関数も実装する必要があることが示されました。以前は、必要な関数の名前が onYouTubePlayerAPIReady であることがセクションに示されていました。ドキュメント全体のコード サンプルも、新しい名前を使用するように更新されています。

注: この変更が既存の実装を壊さないようにするために、両方の名前が機能します。何らかの理由でページに onYouTubeIframeAPIReady 関数と onYouTubePlayerAPIReady 関数がある場合、両方の関数が呼び出され、onYouTubeIframeAPIReady 関数が最初に呼び出されます。

幸運を

于 2014-03-18T16:51:34.970 に答える
0

UIWebView のデフォルトのビューポート幅は 980 です。UIWebView で scalesPageToFit:YES を設定します。

于 2014-08-29T09:09:55.867 に答える