MediaStream未处理的承诺拒绝:[对象DOMError](在Safari 11中)

问题描述:

我在下面的方法中在Safari技术预览版11中获得未处理的承诺拒绝,以初始化WebRTC。具体而言,当我将MediaStream分配给视频元素时会发生这种情况:video.srcObject = event.stream; - 堆栈跟踪显示此行是抛出异常的行。我一直无法使用try/catch.MediaStream未处理的承诺拒绝:[对象DOMError](在Safari 11中)

这个异常只发生在Safari 11中(不会在Chrome中发生)。

这里是方法:

initWebRTC(p){ 
    var self = this; 
    return new Promise((resolve, reject) => { 

     self.state.connection = new RTCMultiConnection(); 
     self.state.connection.socketMessageEvent = 'webrtc-firebase'; 
     self.state.connection.setCustomSocketHandler(FirebaseConnection); 
     self.state.connection.firebase = 'webrtc-firebase'; 
     self.state.connection.enableFileSharing = true; 
     self.state.connection.session = { 
     audio: true, 
     video: true, 
     data: true 
     }; 
     self.state.connection.sdpConstraints.mandatory = { 
     OfferToReceiveAudio: true, 
     OfferToReceiveVideo: true 
     }; 
     self.state.connection.onstream = function(event) { 
      console.log('event.mediaElement',event.mediaElement); 
      console.log('event.stream',event.stream); 

      var videoContainer = document.getElementById('videoContainer'); 
      var video = event.mediaElement; 
      if (!window.safari){ 
      var source = document.createElement("source"); 
      source.srcObject = event.stream; 
      video.appendChild(source); 
      } else { // Safari 
      try{ 
       video.srcObject = event.stream; // this is the line that throws the exception 
      }catch(e){ //unable to catch the exception 
       console.log('exception',e); 
      } 
      } 
      videoContainer.appendChild(video); 

      var playPromise = video.play(); 
      if (playPromise !== undefined) { // not a Promise in some browsers 
      playPromise.catch(function(error) { 
      }); 
      } 
      setTimeout(function() { 
      var playPromise = video.play(); 
      if (playPromise !== undefined) { 
       playPromise.catch(function(error) { 
       }); 
      } 
      }, 5000); 
     }; 
     resolve(); 
    }); 
    } 

不知道这是否会有所帮助,但这里是跟踪:

[Error] Unhandled Promise Rejection: [object DOMError] 
    (anonymous function) 
    rejectPromise 
    onstream (index.js:5787) // this is the video.srcObject = event.stream; line 
    (anonymous function) (RTCMultiConnection.js:4092) 
    getRMCMediaElement (RTCMultiConnection.js:1113) 
    onGettingLocalMedia (RTCMultiConnection.js:4064) 
    onGettingLocalMedia (RTCMultiConnection.js:4984) 
    streaming (RTCMultiConnection.js:3289) 
    (anonymous function) (RTCMultiConnection.js:3358) 
    promiseReactionJob 

任何帮助,将不胜感激。谢谢!

Safari 11阻止自动播放默认声音的任何视频(source)。

相信video元素自带的自动播放属性。当您设置srcObject它,它会尝试播放视频 - 然后得到了阻止Safari浏览器。这就是你看到错误的原因。

您可以尝试从视频元素中删除autoplay,然后您可以在playPromise.catch中看到它。

我不知道这是否可以为你工作,但我也有类似的问题,修复是“静音”属性添加到视频标签,一切都重新工作后,希望它帮助。

+0

谢谢Rigter。它正在工作。但仍然有错误:) –