【发布时间】:2020-08-20 09:01:10
【问题描述】:
我是 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 字节。 有人知道为什么会这样吗?
【问题讨论】:
标签: c# audio uwp background-task mediacapture