donglizuo8892 2018-09-16 15:37
浏览 191
已采纳

使用JS或PHP = MediaElementJS更改视频的SRC

good afternoon. I am trying to change the video source of my video player MediaElement.Js when the user click on a div with a img, Example:

<a href="http://dns:port/hls/125.m3u8"><div id="change_channel><img src="https://www.example.com/img_chh/fox.png"></div></a>

I found some answers of other users that wanted to change the source of the html5 video but only with two stream. The problem that i have it is that i have +400 streams and the code line of the answers of the other users are not working any more...

I tried to run this code but it did not work:

<a href="http://dns:port/live/123.m3u8" onclick="
  playerObject.pause();
  playerObject.setSrc(this.href);
  playerObject.media.load();
  playerObject.load();
  return false
"><div id="change_channel><img src="https://www.example.com/img_chh/fox.png"></div></a>

I will leave the complete code in codepen to debug it easier than downloading all the files to run MediaElement.js. https://codepen.io/Hacker_D/pen/vzzKgM If someone knows how to fix this, i will be thankful. Regards, DiX

  • 写回答

1条回答 默认 最新

  • dtz63853 2018-09-16 15:55
    关注

    Well first, of course the given code won't work since playerObject is not referring to anything, and if it was meant to refer to the <video> element then the line playerObject.media.load(); is invalid since there is no such property on the Audio/Video DOM element. At least initialize it somewhere (give and id to your <video> tag and then do playerObject = document.getElementById(<the id of your video element>); somewhere.

    I forked the link you put int the question and fixed the javascript (https://codepen.io/anon/pen/gddogL), but since the video url is not valid it is not playing, but it does not have any javascript errors any more. So if you have valid video url's you can at least tinker with that.

    As for he amount of videos, since you tagged php, just create a generic javascript function which takes the source and sets it to the player, and when generating the page, just put the correct source in each click handler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程