videojs集成--播放rtmp流

时间:2023-03-09 20:20:04
videojs集成--播放rtmp流

之前说到已经把流推送过来了,这时候就可以使用videojs来进行显示播放。

首先要先有一个文件,那就是video-js.swf

因为,这种播放方式html已经不能很好的进行播放了,需要用到flash来播放,videojs在这个地方就用到了这个。

代码就是下面这样。

里面一些细节注释都有。

重点就是看<video>标签里面的内容

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Access-Control-Allow-Origin" content="*">
  5. <meta charset="utf-8">
  6. <meta name="description" content="Opencast Media Player">
  7. <meta name="author" content="Opencast">
  8. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  9. <title>RTMP播放</title>
  10. <link rel="stylesheet" type="text/css" href="css/bootstrap/css/bootstrap.css">
  11. <link rel="stylesheet" type="text/css" href="css/core_global_style.css">
  12. <script src="videojs/jquery.js"></script>
  13. <script src="videojs/video.js"></script>
  14. <link href="videojs/video-js.css" rel="stylesheet">
  15. <script>
  16. videojs.options.flash.swf = "videojs/video-js.swf";//flash路径,有一些html播放不了的视频,就需要用到flash播放。这一句话要加在在videojs.js引入之后使用
  17. </script>
  18. </head>
  19. <body >
  20. <div id="engage_view" style="display: block;">
  21. <div id="engage_content">
  22. <div id="engage_resize_container">
  23. <div id="engage_video">
  24. <!-- theodul video videojs plugin desktop mode  controls preload="auto"
  25. vjs-big-play-centered 播放按钮居中
  26. poster默认的显示界面,就是还没点播放,给你显示的界面
  27. controls
  28. preload="auto" 是否提前加载
  29. autoplay 自动播放
  30. loop=true 自动循环
  31. data-setup='{"example_option":true}' 可以把一些属性写到这个里面来,如data-setup={"autoplay":true}
  32. -->
  33. <video id="my-video" class="video-js vjs-default-skin vjs-big-play-centered"
  34. poster="videojs/eguidlogo.png" width="800" height="600"
  35. >
  36. <!--
  37. <source src="../output.mp4" type='video/mp4'>  mp4格式
  38. <source src='rtmp://127.0.0.1/hls/test' type='rtmp/flv'/> rtmp格式
  39. <source src='http://127.0.0.1/hls/test.m3u8' type='application/x-mpegURL'/>  m3u8格式
  40. <source src='http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8' type='application/x-mpegURL'/> m3u8格式,可用
  41. -->
  42. <source src='rtmp://live.hkstv.hk.lxdns.com/live/hks' type='rtmp/flv'/>
  43. </video>
  44. <!-- http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8 可用的m3u8地址    -->
  45. <!-- rtmp://live.hkstv.hk.lxdns.com/live/hks  可用的rtmp地址-->
  46. </div>
  47. </div>
  48. <!-- timeline plugin container -->
  49. </div>
  50. </div>
  51. <div class="clear"></div>
  52. <div id="information_wrapper">
  53. <!-- description plugin container -->
  54. <div id="engage_description"><!-- theodul description plugin desktop mode -->
  55. <div id="engage_basic_description"></div>
  56. </div>
  57. </div>
  58. <div class="tab-pane" id="engage_Slide_text_tab"><!-- theodul tab slidetext plugin embed mode -->
  59. </div>
  60. <script src="videojs/videojs-media-sources.min.js"></script>
  61. <!-- <script src="videojs/videojs-contrib-hls.min.js"></script>-->
  62. <script src="videojs/videojs.hls.min.js"></script>
  63. <script>
  64. //播放的话,就直接使用下面2行
  65. //后面注释的那些其实也是可用用的,不过刚开始集成,越简单越好
  66. var player = videojs('my-video');
  67. player.play();
  68. /*
  69. (function ($) {
  70. var resetVideoSize = function (myPlayer) {
  71. var videoJsBoxWidth = $(".video-js").width();
  72. var ratio = 1440 / 900;
  73. var videoJsBoxHeight = videoJsBoxWidth / ratio;
  74. myPlayer.height(videoJsBoxHeight);
  75. }
  76. $(window).on("resize", function () {
  77. resetVideoSize(myPlayer);
  78. });
  79. myPlayer.play();
  80. })(jQuery)
  81. function changeSrc() {
  82. var src = "http://127.0.0.1/hls/test.m3u8";
  83. var myPlayera = videojs("my-video");
  84. $("#my-video").attr("src", "rtmp://live.hkstv.hk.lxdns.com/live/hks")
  85. myPlayera.src("rtmp://live.hkstv.hk.lxdns.com/live/hks"); //重新初始化视频地址
  86. myPlayera.load("rtmp://live.hkstv.hk.lxdns.com/live/hks"); //重新加载
  87. }
  88. */
  89. function changeSrc(src) {
  90. var myPlayera = videojs("my-video");
  91. //$("#videojs_videodisplay_presentation_html5_api").attr("src", "rtmp://live.hkstv.hk.lxdns.com/live/hks")
  92. myPlayera.src(src); //重新初始化视频地址
  93. myPlayera.load(src); //重新加载
  94. }
  95. </script>
  96. </body>
  97. </html>