MediaCapabilities: encodingInfo() 方法
注意:此功能在 Web Workers 中可用。
MediaCapabilities 介面的 encodingInfo() 方法會返回一個 Promise,該 Promise 會解析為測試的媒體配置的編碼媒體能力。這包含三個布林屬性 supported、smooth 和 powerefficient,它們描述了裝置與該媒體型別的相容程度。
語法
encodingInfo(configuration)
引數
configuration-
一個包含
type屬性以及 video 或 audio 屬性(包含相應型別的配置)的物件。type-
正在測試的媒體型別。這有兩個可能的值:
record-
表示用於錄製媒體的配置,例如使用
MediaRecorder。 webrtc-
表示用於透過電子方式傳輸(例如,使用
RTCPeerConnection)的配置。注意: Firefox 在此型別中使用transmission,而webrtc不起作用。 transmission非標準-
Firefox 中使用的
webrtc的同義詞。
video-
影片媒體源的配置物件。它具有以下屬性:
audio-
音訊媒體源的配置物件。它具有以下屬性:
contentType-
一個包含有效的音訊 MIME 型別以及(可選的)
codecs引數的字串。 channels-
音訊軌道使用的聲道數。
bitrate-
用於編碼音訊檔案每秒的位元數。
samplerate-
構成音訊檔案每秒的音訊取樣數。
返回值
一個 Promise,它會解析為一個包含三個布林屬性的物件。
supported-
如果媒體內容根本無法編碼,則為
true。否則為false。 平滑-
如果媒體播放將是流暢的(高質量),則為
true。否則為false。 powerEfficient-
如果媒體播放將是節能的,則為
true。否則為false。
瀏覽器將報告支援的媒體配置為 smooth 和 powerEfficient,直到裝置上的統計資料被記錄為止。所有支援的音訊編解碼器都被報告為節能。
異常
TypeError-
當傳遞給
encodingInfo()方法的configuration無效時會丟擲,原因可能包括以下任何一項:- 型別不是 video 或 audio,
contentType不是有效的編解碼器 MIME 型別,- 傳遞給方法的媒體配置中存在其他錯誤,包括省略了任何
configuration元素。
示例
// Create media configuration to be tested
const mediaConfig = {
type: "record", // or 'transmission'
video: {
contentType: "video/webm;codecs=vp8.0", // valid content type
width: 1920, // width of the video
height: 1080, // height of the video
bitrate: 120000, // number of bits used to encode 1s of video
framerate: 48, // number of frames making up that 1s.
},
};
// check support and performance
navigator.mediaCapabilities.encodingInfo(mediaConfig).then((result) => {
console.log(
`This configuration is ${result.supported ? "" : "not "}supported,`,
);
console.log(`${result.smooth ? "" : "not "}smooth, and`);
console.log(`${result.powerEfficient ? "" : "not "}power efficient.`);
});
規範
| 規範 |
|---|
| Media Capabilities # dom-mediacapabilities-encodinginfo |
瀏覽器相容性
載入中…