Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Has anybody found a robust way to change the source of a video element dynamically and make it work on Iphone? My testing works fine in Chrome, but I can not make it work consistently. Any suggestions on strategy or player is welcome, so it will work on any device.

I have a video element:

<video id="player1" width="320" height="240" preload controls>
    <source src="http://dev.swinginsam.com/_files/testvid_01.mp4" />
    <source src="http://dev.swinginsam.com/_files/testvid_01.webm" type='video/webm; codecs="vp8, vorbis"' />
    <source src="http://dev.swinginsam.com/_files/testvid_01.ogv" type='video/ogg; codecs="theora, vorbis"' />
    <object width="320" height="240" type="application/x-shockwave-flash"
    data="flowplayer-3.2.1.swf">
        <param name="movie" value="flowplayer-3.2.1.swf" />
        <param name="allowfullscreen" value="true" />
        <param name="flashvars" value='config={"clip": {"url":     "http://dev.swinginsam.com/_files/testvid_01.mp4", "autoPlay":false, "autoBuffering":true}}' />
         <p>Download video as
            <a href="http://dev.swinginsam.com/_files/testvid_01.mp4">MP4</a>,
            <a href="http://dev.swinginsam.com/_files/testvid_01.webm">WebM</a>, or <a href="http://dev.swinginsam.com/_files/testvid_01.ogv">Ogg</a>.</p>
    </object>
</video>

And a snippet of code for initalizing the player I use mediaelementjs - it works in the sence that the player is correcly initialized and skinned and works for the first video:

$('#one').live("pageinit", function (event, data) {

 var player1 = new MediaElementPlayer('#player1',{});

 $('#button1').click(function(){
    player1.pause();
    player1.setSrc([
       {src:"http://dev.swinginsam.com/_files/testvid_01.mp4"},
       {src:"http://dev.swinginsam.com/_files/testvid_01.webm", type:'video/webm; codecs="vp8, vorbis"'},
       {src:"http://dev.swinginsam.com/_files/testvid_01.ogv", type:'video/ogg; codecs="theora, vorbis"'}
 ]);



 $('#button2').click(function(){
     player1.pause();
     player1.setSrc([
         {src:"http://dev.swinginsam.com/_files/testvid_02.mp4"},
         {src:"http://dev.swinginsam.com/_files/testvid_02.webm", type:'video/webm; codecs="vp8, vorbis"'},
         {src:"http://dev.swinginsam.com/_files/testvid_02.ogv", type:'video/ogg; codecs="theora, vorbis"'}
      ]);
      player1.load();
 });
});

Problems begin when you press the buttons and begin to change the source. It works in Chrome but Safari just continues to play the first video.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
125 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are missing the closing parenthesis }); for your $('#button1').click(function(){. The javascript should be :

$('#one').live("pageinit", function(event, data) {

    var player1 = new MediaElementPlayer('#player1', {});

    $('#button1').click(function() {
        player1.pause();
        player1.setSrc([
            {
            src: "http://dev.swinginsam.com/_files/testvid_01.mp4"},
        {
            src: "http://dev.swinginsam.com/_files/testvid_01.webm",
            type: 'video/webm; codecs="vp8, vorbis"'},
        {
            src: "http://dev.swinginsam.com/_files/testvid_01.ogv",
            type: 'video/ogg; codecs="theora, vorbis"'}
        ]);

    });

    $('#button2').click(function() {
        player1.pause();
        player1.setSrc([
            {
            src: "http://dev.swinginsam.com/_files/testvid_02.mp4"},
        {
            src: "http://dev.swinginsam.com/_files/testvid_02.webm",
            type: 'video/webm; codecs="vp8, vorbis"'},
        {
            src: "http://dev.swinginsam.com/_files/testvid_02.ogv",
            type: 'video/ogg; codecs="theora, vorbis"'}
        ]);
        player1.load();
    });

});?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...