rtcPlayer.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <div id="rtcPlayer">
  3. <video id='webRtcPlayerBox' controls autoplay style="text-align:left;">
  4. Your browser is too old which doesn't support HTML5 video.
  5. </video>
  6. </div>
  7. </template>
  8. <script>
  9. let webrtcPlayer = null;
  10. export default {
  11. name: 'rtcPlayer',
  12. data() {
  13. return {
  14. timer: null
  15. };
  16. },
  17. props: ['videoUrl', 'error', 'hasaudio'],
  18. mounted () {
  19. let paramUrl = decodeURIComponent(this.$route.params.url)
  20. this.$nextTick(() =>{
  21. if (typeof (this.videoUrl) == "undefined") {
  22. this.videoUrl = paramUrl;
  23. }
  24. console.log("初始化时的地址为: " + this.videoUrl)
  25. this.play(this.videoUrl)
  26. })
  27. },
  28. watch:{
  29. videoUrl(newData, oldData){
  30. this.pause();
  31. this.play(newData);
  32. },
  33. immediate:true
  34. },
  35. methods: {
  36. play: function (url) {
  37. webrtcPlayer = new ZLMRTCClient.Endpoint({
  38. element: document.getElementById('webRtcPlayerBox'),// video 标签
  39. debug: true,// 是否打印日志
  40. zlmsdpUrl: url,//流地址
  41. simulecast: false,
  42. useCamera: false,
  43. audioEnable: true,
  44. videoEnable: true,
  45. recvOnly: true,
  46. usedatachannel: false,
  47. })
  48. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_ICE_CANDIDATE_ERROR,(e)=>{// ICE 协商出错
  49. console.error('ICE 协商出错')
  50. this.eventcallbacK("ICE ERROR", "ICE 协商出错")
  51. });
  52. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_ON_REMOTE_STREAMS,(e)=>{//获取到了远端流,可以播放
  53. console.log('播放成功',e.streams)
  54. this.eventcallbacK("playing", "播放成功")
  55. });
  56. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED,(e)=>{// offer anwser 交换失败
  57. console.error('offer anwser 交换失败',e)
  58. this.eventcallbacK("OFFER ANSWER ERROR ", "offer anwser 交换失败")
  59. if (e.code ==-400 && e.msg=="流不存在"){
  60. console.log("流不存在")
  61. this.timer = setTimeout(()=>{
  62. this.webrtcPlayer.close();
  63. this.play(url)
  64. }, 100)
  65. }
  66. });
  67. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_ON_LOCAL_STREAM,(s)=>{// 获取到了本地流
  68. // document.getElementById('selfVideo').srcObject=s;
  69. this.eventcallbacK("LOCAL STREAM", "获取到了本地流")
  70. });
  71. },
  72. pause: function () {
  73. if (webrtcPlayer != null) {
  74. webrtcPlayer.close();
  75. webrtcPlayer = null;
  76. }
  77. },
  78. eventcallbacK: function(type, message) {
  79. console.log("player 事件回调")
  80. console.log(type)
  81. console.log(message)
  82. }
  83. },
  84. destroyed() {
  85. clearTimeout(this.timer);
  86. },
  87. }
  88. </script>
  89. <style>
  90. .LodingTitle {
  91. min-width: 70px;
  92. }
  93. #rtcPlayer{
  94. width: 100%;
  95. }
  96. #webRtcPlayerBox{
  97. width: 100%;
  98. max-height: 56vh;
  99. background-color: #000;
  100. }
  101. /* 隐藏logo */
  102. /* .iconqingxiLOGO {
  103. display: none !important;
  104. } */
  105. </style>