rtcPlayer.vue 3.3 KB

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