CodecQuery 類別允許你查詢目前裝置上安裝的編解碼器。 不同裝置系列Windows中包含的編解碼器清單可在文章Supported codec列出,但由於使用者和應用程式可以在裝置上安裝額外的編解碼器,你可能想在執行時查詢編碼器支援,以了解目前裝置可用的編解碼器。
透過呼叫建構子初始化 CodecQuery 類別的新實例。
var codecQuery = new CodecQuery();
FindAllAsync 方法會回傳所有已安裝且符合所提供參數的編解碼器。 這些參數包括 CodecKind 值,指定查詢音訊或視訊編解碼器或兩者; CodecCategory 值,表示查詢編碼器或解碼器;以及代表媒體編碼子類型的字串,例如 H.264 影片或 MP3 音訊。
為子型別值指定一個空字串,以回傳所有子型別的編解碼器。 以下範例列出了裝置上安裝的所有影像編碼器。
IReadOnlyList<CodecInfo> result =
await codecQuery.FindAllAsync(CodecKind.Video, CodecCategory.Encoder, "");
foreach (var codecInfo in result)
{
codecResultsTextBox.Text += "============================================================\n";
codecResultsTextBox.Text += string.Format("Codec: {0}\n", codecInfo.DisplayName);
codecResultsTextBox.Text += string.Format("Kind: {0}\n", codecInfo.Kind.ToString());
codecResultsTextBox.Text += string.Format("Category: {0}\n", codecInfo.Category.ToString());
codecResultsTextBox.Text += string.Format("Trusted: {0}\n", codecInfo.IsTrusted.ToString());
foreach (string subType in codecInfo.Subtypes)
{
codecResultsTextBox.Text += string.Format(" Subtype: {0}\n", subType);
}
}
你傳遞到 FindAllAsync 的子型字串可以是系統定義的 GUID 子型的字串表示,或是該子型的 FOURCC 程式碼。 支援的媒體子類型 GUID 集合列於 Audio Subtype GUIDs 與 Video Subtype GUIDs 兩篇文章中,但 CodecSubtypes 類別提供可回傳每個支援子類型的 GUID 值的屬性。 欲了解更多 FOURCC 代碼資訊,請參見 FOURCC 代碼。
以下範例指定 FOURCC 代碼「H264」以判斷裝置是否安裝了 H.264 視訊解碼器。 你可以在嘗試播放 H.264 影片內容前先執行此查詢。 你也可以在播放時處理不支援的編解碼器。 欲了解更多資訊,請參閱 「開啟媒體項目時處理不支援的編解碼器與未知錯誤」。
IReadOnlyList<CodecInfo> h264Result =
await codecQuery.FindAllAsync(CodecKind.Video, CodecCategory.Decoder, "H264");
if (h264Result.Count > 0)
{
codecResultsTextBox.Text = "H264 decoder is present.";
}
以下範例查詢以判斷目前裝置是否安裝了 FLAC 音訊編碼器,若安裝,則會為可用於擷取音訊檔案或將音訊從其他格式轉碼為 FLAC 音訊檔案的子類型建立 MediaEncodingProfile。
IReadOnlyList<CodecInfo> flacResult =
await codecQuery.FindAllAsync(CodecKind.Audio, CodecCategory.Encoder, CodecSubtypes.AudioFormatFlac);
if (flacResult.Count > 0)
{
AudioEncodingProperties audioProps = new AudioEncodingProperties();
audioProps.Subtype = CodecSubtypes.AudioFormatFlac;
audioProps.SampleRate = 44100;
audioProps.ChannelCount = 2;
audioProps.Bitrate = 128000;
audioProps.BitsPerSample = 32;
MediaEncodingProfile encodingProfile = new MediaEncodingProfile();
encodingProfile.Audio = audioProps;
encodingProfile.Video = null;
}