2014-11-03 47 views
2

我正在嘗試在我的cordova-android項目中使用a YouTube iframe API。當我在計算機上的瀏覽器中運行代碼時,它運行得很完美,但是當我構建應用程序並在手機上運行時,包含iframe的頁面將無法加載,並且在控制檯中出現以下錯誤:Cordova/Phonegap YouTube iframe_api錯誤:XMLHttpRequest無法加載chrome-extension跨域請求僅支持HTTP

XMLHttpRequest cannot load chrome-extension://boadgeojelhgndaghljhdicfkmllpafd/cast_sender.js. Cross origin requests are only supported for HTTP

這裏是我的代碼:

<div class='ui-body ui-body-a'> 
     <!-- 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 = "https://www.youtube.com/iframe_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 onYouTubeIframeAPIReady() { 
       player = new YT.Player('player', { 
        height: '390', 
        width: '640', 
        videoId: 'M7lc1UVf-VE', 
        events: { 
         'onReady': onPlayerReady, 
         'onStateChange': onPlayerStateChange 
        } 
       }); 
      } 

      // 4. The API will call this function when the video player is ready. 
      function onPlayerReady(event) { 
       console.log('Loaded Video) 
      } 

      // 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.PLAYING && !done) { 
        setTimeout(stopVideo, 6000); 
        done = true; 
       } 
      } 
      function stopVideo() { 
       player.stopVideo(); 
      } 
    </script> 
</div> 

提前感謝!

+0

我無法讓它工作,所以我最終使用了glitchbone的cordova插件[YoutubeVideoPlayer](http://plugins.cordova.io/#/package/com.bunkerpalace.cordova.youtubevideoplayer)。但我會把問題留給公衆。 – justahandsomeman 2014-12-13 17:37:56

回答

相關問題