如何使用 jQuery 更改视频 src?

发布于 2024-09-28 05:39:04 字数 444 浏览 2 评论 0原文

如何使用 jQuery 更改 HTML5 视频标签的 src?

我得到了这个 HTML:

<div id="divVideo">
  <video controls>
    <source src="test1.mp4" type="video/mp4" />
  </video>
</div>

这不起作用:

var videoFile = 'test2.mp4';
$('#divVideo video source').attr('src', videoFile);

如果我使用 firebug 检查它,它会更改 src,但实际上不会更改正在播放的视频。

我读过有关 .pause() 和 .load() 的内容,但我不确定如何使用它们。

How do you change the src of a HTML5 video tag using jQuery?

I got this HTML:

<div id="divVideo">
  <video controls>
    <source src="test1.mp4" type="video/mp4" />
  </video>
</div>

This doesn't work:

var videoFile = 'test2.mp4';
$('#divVideo video source').attr('src', videoFile);

It changes the src if I inspect it using firebug, but does not actually change the video being played.

I read about .pause() and .load(), but I'm not sure how to use them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

信仰 2024-10-05 05:39:04

更改 src 属性后尝试 $("#divVideo video")[0].load();

Try $("#divVideo video")[0].load(); after you changed the src attribute.

迷爱 2024-10-05 05:39:04

我尝试过使用 autoplay 标签,并且 .load() .play() 至少仍然需要在 chrome 中调用(也许是我的设置)。

使用您的示例使用 jquery 执行此操作的最简单的跨浏览器方法是

var $video = $('#divVideo video'),
videoSrc = $('source', $video).attr('src', videoFile);
$video[0].load();
$video[0].play();

但是我建议您执行此操作的方式(为了易读性和简单性)是

var video = $('#divVideo video')[0];
video.src = videoFile;
video.load();
video.play();

进一步阅读
http://msdn.microsoft。 com/en-us/library/ie/hh924823(v=vs.85).aspx#ManagingPlaybackInJavascript

附加信息:.load() 仅在视频元素内有 html 源元素时才有效(即 )

非 jquery 方式是:

HTML

<div id="divVideo">
  <video id="videoID" controls>
    <source src="test1.mp4" type="video/mp4" />
  </video>
</div>

JS

var video = document.getElementById('videoID');
video.src = videoFile;
video.load();
video.play();

I've tried using the autoplay tag, and .load() .play() still need to be called at least in chrome (maybe its my settings).

the simplest cross browser way to do this with jquery using your example would be

var $video = $('#divVideo video'),
videoSrc = $('source', $video).attr('src', videoFile);
$video[0].load();
$video[0].play();

However the way I'd suggest you do it (for legibility and simplicity) is

var video = $('#divVideo video')[0];
video.src = videoFile;
video.load();
video.play();

Further Reading
http://msdn.microsoft.com/en-us/library/ie/hh924823(v=vs.85).aspx#ManagingPlaybackInJavascript

Additional info: .load() only works if there is an html source element inside the video element (i.e. <source src="demo.mp4" type="video/mp4" />)

The non jquery way would be:

HTML

<div id="divVideo">
  <video id="videoID" controls>
    <source src="test1.mp4" type="video/mp4" />
  </video>
</div>

JS

var video = document.getElementById('videoID');
video.src = videoFile;
video.load();
video.play();
浪菊怪哟 2024-10-05 05:39:04

我宁愿这样做

<video  id="v1" width="320" height="240" controls="controls">

</video>

,然后使用

$("#v1").html('<source src="test1.mp4" type="video/mp4"></source>' );

I would rather make it like this

<video  id="v1" width="320" height="240" controls="controls">

</video>

and then use

$("#v1").html('<source src="test1.mp4" type="video/mp4"></source>' );
破晓 2024-10-05 05:39:04

查询

<script type="text/javascript">
$(document).ready(function() {
  var videoID = 'videoclip';
  var sourceID = 'mp4video';
  var newmp4 = 'media/video2.mp4';
  var newposter = 'media/video-poster2.jpg';
 
  $('#videolink1').click(function(event) {
    $('#'+videoID).get(0).pause();
    $('#'+sourceID).attr('src', newmp4);
    $('#'+videoID).get(0).load();
     //$('#'+videoID).attr('poster', newposter); //Change video poster
    $('#'+videoID).get(0).play();
  });
});

JQUERY

<script type="text/javascript">
$(document).ready(function() {
  var videoID = 'videoclip';
  var sourceID = 'mp4video';
  var newmp4 = 'media/video2.mp4';
  var newposter = 'media/video-poster2.jpg';
 
  $('#videolink1').click(function(event) {
    $('#'+videoID).get(0).pause();
    $('#'+sourceID).attr('src', newmp4);
    $('#'+videoID).get(0).load();
     //$('#'+videoID).attr('poster', newposter); //Change video poster
    $('#'+videoID).get(0).play();
  });
});

内心荒芜 2024-10-05 05:39:04

对我有用的是在更改源后发出“播放”命令。
奇怪的是,您不能通过 jQuery 实例使用“play()”,因此您只需使用 getElementByID,如下所示:

HTML

<video id="videoplayer" width="480" height="360"></video>

JAVASCRIPT

$("#videoplayer").html('<source src="'+strSRC+'" type="'+strTYPE+'"></source>' );
document.getElementById("videoplayer").play();

What worked for me was issuing the 'play' command after changing the source.
Strangely you cannot use 'play()' through a jQuery instance so you just use getElementByID as follows:

HTML

<video id="videoplayer" width="480" height="360"></video>

JAVASCRIPT

$("#videoplayer").html('<source src="'+strSRC+'" type="'+strTYPE+'"></source>' );
document.getElementById("videoplayer").play();
醉生梦死 2024-10-05 05:39:04
     $(document).ready(function () {     
    setTimeout(function () {
                    $(".imgthumbnew").click(function () {
                        $("#divVideo video").attr({
                            "src": $(this).data("item"),
                            "autoplay": "autoplay",        
                        })
                    })
                }, 2000); 
            }
        });

here ".imgthumbnew" is the class of images which are thumbs of videos, an extra attribute is given to them which have video url. u can change according to your convenient.
i would suggest you to give an ID to ur Video tag it would be easy to handle.
     $(document).ready(function () {     
    setTimeout(function () {
                    $(".imgthumbnew").click(function () {
                        $("#divVideo video").attr({
                            "src": $(this).data("item"),
                            "autoplay": "autoplay",        
                        })
                    })
                }, 2000); 
            }
        });

here ".imgthumbnew" is the class of images which are thumbs of videos, an extra attribute is given to them which have video url. u can change according to your convenient.
i would suggest you to give an ID to ur Video tag it would be easy to handle.
一萌ing 2024-10-05 05:39:04

最简单的方法是使用自动播放。

<video autoplay></video>

当你通过 JavaScript 改变 src 时,你不需要提及 load()。

The easiest way is using autoplay.

<video autoplay></video>

When you change src through javascript you don't need to mention load().

尹雨沫 2024-10-05 05:39:04

这适用于 Flowplayer 6.0.2

<script>
    flowplayer().load({
         sources: [
            { type: "video/mp4",  src: variable }
            ]
        });
</script>

其中 variable 是 javascript/jquery 变量值,
视频标签应该是这个

<div class="flowplayer">
   <video>
      <source type="video/mp4" src="" class="videomp4">
   </video>
</div>

希望它对任何人都有帮助。

This is working on Flowplayer 6.0.2.

<script>
    flowplayer().load({
         sources: [
            { type: "video/mp4",  src: variable }
            ]
        });
</script>

where variable is a javascript/jquery variable value,
The video tag should be something this

<div class="flowplayer">
   <video>
      <source type="video/mp4" src="" class="videomp4">
   </video>
</div>

Hope it helps anyone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文