Dart onYouTubeIframeAPIReady

安稳与你 提交于 2019-12-08 07:52:37

问题


How can i create function onYouTubeIframeAPIReady from this example
https://developers.google.com/youtube/iframe_api_reference#Getting_Started in Dart? The API will call this function when the page has finished downloading the JavaScript for the player API.


回答1:


Thank you, Günter! It's working in in Chrome.

<!DOCTYPE html>
<html>
<body>   
  <div id="player"></div>
  <script type="application/dart" src="ytube_iframe.dart"></script>
  <!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
  <script src="packages/browser/dart.js"></script> 
    <script src="packages/browser/interop.js"></script>
</body>
</html>

ytube_iframe.dart

import 'dart:html';
import 'dart:js' as js;
void main() {
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  js.context['onYouTubeIframeAPIReady'] = onYouTubeIframeAPIReady;
  //js.context['onPlayerReady'] = onPlayerReady;
  //js.context['onPlayerStateChange'] = onPlayerStateChange;
}

var player; 
void onYouTubeIframeAPIReady() {
  var params = {
                'height': '390',
                'width': '640',
                'videoId': 'M7lc1UVf-VE',
                /*'events': {
                  'onReady': onPlayerReady,
                  'onStateChange': onPlayerStateChange
                }*/
              };
  var jsParams = new js.JsObject.jsify(params);
  player  = new js.JsObject(js.context['YT']['Player'], ['player',jsParams]);
}


来源:https://stackoverflow.com/questions/22429185/dart-onyoutubeiframeapiready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!