攝像頭視頻流顯示報錯Failed to execute 'createObjectURL' on 'URL'

研究即時通信的過程中需要調用攝像頭,發現報錯,原來是谷歌棄用了這個方法,根據官方提示修改即可

1. 報錯信息

Uncaught (in promise) TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
    at ofwebrtc.js:143

2. 官方說明

URL.createObjectURL()使用說明
原因是 Chrome 升級後,新版本不再支持該用法。
在這裏插入圖片描述
在這裏插入圖片描述

3. 解決方案

所以原先的代碼:

video.src = URL.createObjectURL(stream);

需要被修改爲:

video.srcObject = stream;

一個兼容的寫法如下:

try {
	this.srcObject = stream;
} catch (error) {
	this.src = window.URL.createObjectURL(stream);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章