简体   繁体   English

如何动态更改html5视频源?

[英]How to dynamically change html5 video source?

I'm using flowplayer to render live videos.我正在使用 flowplayer 来渲染实时视频。 On button click, I want to dynamically change the src (video url) to a different url.单击按钮时,我想将 src(视频 url)动态更改为不同的 url。 This needs to work well on most of the mobile devices (ios and Android).这需要在大多数移动设备(ios 和 Android)上运行良好。 I have tried almost all the answers in this thread but nothing seems to work well on all devices - changing source on html5 video tag .我已经尝试了该线程中的几乎所有答案,但似乎在所有设备上都无法正常工作 - 更改 html5 视频标签上的源

An answer by antonioj1015 works except that I don't see the default Play/Pause buttons on the player. antonioj1015的回答有效,只是我没有在播放器上看到默认的播放/暂停按钮。

Here is my flowplayer container div looks like -这是我的 flowplayer 容器 div 看起来像 -

<div id="player" class="flowplayer">
    <video id="video"><source id="video_src" src="//mydomain.com/video.mp4" type="video/mp4">
    </video>
</div>

Javascript code looks like this - Javascript 代码如下所示 -

document.getElementById('player').innerHTML = '<video id="video"><source src="'+newurl+'" type="video/mp4"></video>';
document.getElementById('video').load();
document.getElementById('video').play();

I have tried to replace the entire video tag.我试图替换整个视频标签。 Also, tried to dynamically change the src but nothing seems to work.此外,尝试动态更改 src 但似乎没有任何效果。 Any push in the right direction will be greatly appreciated.任何朝正确方向的推动将不胜感激。 Thanks.谢谢。

You can use src property.您可以使用src属性。

document.getElementById('video').src = 'https://example.com/othervideo.mp4';
document.getElementById('video').load();
document.getElementById('video').play();

Read more on https://www.w3schools.com/tags/av_prop_src.asp阅读更多https://www.w3schools.com/tags/av_prop_src.asp

You can set flowplayer api to a global variable at the beginning您可以在开始时将 flowplayer api 设置为全局变量

var myApi;
flowplayer(function (api, root) { myApi = api; });

Then on your button click, do:然后单击按钮,执行以下操作:

    $("#yourbutton").on("click", function () {
       myApi.load({
       sources: [{ type: "video/mp4",
                   src:  newurl  }]
      });
   });
let source = document.querySelector('video > source')
source.src = '[the-new-url]'
source.parentNode.load()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM