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.

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

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败