在下面初始化 WebRTC 的方法中,我在 Safari Tech Preview 11 中遇到了未处理的承诺拒绝。具体来说,当我分配MediaStream
像这样的视频元素:video.srcObject = event.stream;
- 堆栈跟踪显示这一行是引发异常的行。我无法使用捕获异常try/catch.
该异常仅发生在 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 http://adage.com/article/digital/apple-blocks-autoplay-video-safari-web-browser-update/309266/).
我相信video
元素带有 autoplay 属性。当你设置srcObject
它会尝试播放视频,然后被 Safari 阻止。这就是您看到错误的原因。
您可以尝试删除autoplay
从视频元素中,那么您也许可以捕捉到它playPromise.catch
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)