[英]UWP: Audio media capture in a background task
我是 UWP 的新人。我嘗試通過 MediaCapture API 從后台線程錄制音頻。
我的代碼在這里:
public sealed class Recorder : IBackgroundTask
{
private BackgroundTaskDeferral _deferral;
private readonly MediaCapture mediaCapture = new MediaCapture();
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings()
{
StreamingCaptureMode = StreamingCaptureMode.Audio,
};
await mediaCapture.InitializeAsync(settings);
var profile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Auto);
profile.Audio = AudioEncodingProperties.CreatePcm(16000, 1, 16);
await StartRecordAsync(profile);
_deferral.Complete();
}
private async Task StartRecordAsync(MediaEncodingProfile profile)
{
while(true)
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile storageFile = await storageFolder.CreateFileAsync(Guid.NewGuid() + ".wav", CreationCollisionOption.ReplaceExisting);
await mediaCapture.StartRecordToStorageFileAsync(profile, storageFile);
Task.Delay(10000).Wait();
await mediaCapture.StopRecordAsync();
}
}
}
它記錄每個 .wav 文件 10 秒,但是當我播放這些文件時,我什么也沒聽到。 每個文件都是 310KB+,所以它不是 0 字節。 有人知道為什么會發生嗎?
它記錄每個 .wav 文件 10 秒,但是當我播放這些文件時,我什么也沒聽到。 每個文件都是 310KB+,所以它不是 0 字節。 有人知道為什么會發生嗎?
恐怕您無法在BackgroundTask
中捕獲音頻。 來源於官方文檔。
應從應用的主 UI 線程調用 InitializeAsync。 應用程序必須通過正確清理媒體捕獲資源來處理應用程序暫停或終止。 有關正確關閉 MediaCapture object 的信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.