开发者

Video won't change on mediaelement.js

开发者 https://www.devze.com 2023-04-03 03:42 出处:网络
I am trying to change a video in mediaelement.js using javascript. My function works perfectly the first time starting the video of choice.However if I call the function again, it appears to ignore t

I am trying to change a video in mediaelement.js using javascript.

My function works perfectly the first time starting the video of choice. However if I call the function again, it appears to ignore the call, although the new path is correct. When I did this in using HTML5 directly, the video would reload (see commented player1.src, etc), but this would not use the benefits of mediaelement.js for the fallback and formatting.

What am I forgetting? Do I need to clear out the previous instance, if so How?

Thank you,

Jay

<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 Media开发者_StackOverflow中文版ElementPlayer
     (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>


you should reuse player2 object not create new one every time...

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消