2011-09-07 74 views
1

我正在嘗試使用javascript更改mediaelement.js中的視頻。視頻不會在mediaelement.js上更改

我的功能在首次啓動選擇視頻時完美運行。但是,如果我再次調用該函數,它似乎忽略該調用,但新路徑是正確的。當我直接使用HTML5時,會重新加載視頻(請參閱評論的player1.src等),但這不會使用mediaelement.js的優點來實現回退和格式設置。

我忘了什麼?我需要清除以前的實例嗎?如果是這樣的話?

謝謝

周杰倫

<script type='text/javascript'> 

function playVideo(videopath) 
{ 
    var myVideo = document.getElementById(videopath).value; 
    var myVideopath = 「/Library/"+encodeURIComponent(myVideo); 
    // player1.src = myVidiopath; 
    // player1.load(); 
    // player1.play(); 


     var v = document.getElementsByTagName("video")[0]; 
    var player2 = new MediaElementPlayer 
    (v, 
     { 
     success: function(media, domObject) 
     { 
      media.setSrc(myVidiopath); 
       media.load(); 
       media.play(); 
     } 
     } 
    ); 
} 

</script> 


<table> 
    <tr><td width="500"> 
     <div id="avplayer" class="container"> 
    <video id="player1" width="480" height="320" poster="/acds/files/logo_cube.png" 
     controls="controls" preload="none"> 
     <!-- MP4 for Safari, IE9, iPhone, iPad, Android, and Windows Phone 7 --> 
     <source type="video/mp4" src="" /> 
     <!-- Flash fallback for non-HTML5 browsers without JavaScript --> 
     <object width="480" height="320" type="application/x-shockwave-flash" 
      data="/acds/scripts/videoPlayer/flashmediaelement.swf"> 
      <param name="movie" value="/acds/scripts/videoPlayer/flashmediaelement.swf" /> 
      <param name="flashvars" 
       value="controls=true&poster=/acds/files/logo_cube.png" /> 
      <!-- Image as a last resort --> 
      <img src="/acds/files/logo_cube.png" width="480" height="320" title="No video 
     playback capabilities" /> 
     </object> 
    </video> 
     </div> 
    </td><td width="200"> 
     <table> 
      <tr><td><button id='PlayA' style='WIDTH: 160px; HEIGHT: 24px' type='button' 
      onclick='playSound("ShowVidA")' >Play Lesson VideoA</button></td></tr> 
      <tr><td><button id='PlayB' style='WIDTH: 160px; HEIGHT: 24px' type='button' 
      onclick='playSound("ShowVidB")' >Play Lesson VideoB</button></td></tr> 
      <tr><td><button id='PlayC' style='WIDTH: 160px; HEIGHT: 24px' type='button' 
      onclick='playSound("ShowVidC")' >Play Lesson VideoC</button></td></tr> 
      <tr><td><button id='PlayD' style='WIDTH: 160px; HEIGHT: 24px' type='button' 
       onclick='playSound("ShowVidD")' >Play Lesson VideoD</button></td></tr> 
     </table> 
    </td></tr> 
</table> 

回答

0

你應該重用player2對象不能創造新的每次...

<script type='text/javascript'> 
var player2=false; 
    function playVideo(videopath) 
    { 
     var myVideo = document.getElementById(videopath).value; 
     var myVideopath = 「/Library/"+encodeURIComponent(myVideo); 

     var v = document.getElementsByTagName("video")[0]; 
     if (typeof(player2)!='object') 
     { 
     player2 = new MediaElementPlayer(v, { 
      success: function(media, domObject) 
      { 
       media.setSrc(myVidiopath); 
        media.load(); 
        media.play(); 
      } 
     }); 
     } 
     else 
     { 
      player2.setSrc(myVidiopath); 
      player2.load(); 
      player2.play(); 
     } 
    } 
</script> 
相關問題