UWP:在后台任务中捕获音频媒体



我是UWP的新手。我尝试通过MediaCaptureneneneba 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();
}
}
}

它每10秒记录.wav文件,但当我播放这些文件时,我什么也听不到。每个文件都是310KB+,所以它不是0字节有人知道为什么会发生这种事吗

它每个记录.wav文件10秒,但当我播放这些文件时,我什么也听不到。每个文件都是310KB+,所以它不是0字节。有人知道为什么会发生这种事吗?

恐怕您无法在BackgroundTask中捕获音频。源自官方文件。

InitializeAsync应该从应用程序的主UI线程调用。应用程序必须通过正确清理媒体捕获资源来处理应用程序暂停或终止。有关正确关闭MediaCapture对象的信息

最新更新