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

问题描述

我是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();
            }
        }
    }

它记录每个10秒钟的.wav文件,但是播放这些文件时,我什么也听不到。每个文件均为310KB +,因此不是0个字节。 有人知道为什么会发生吗?

解决方法

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

恐怕您无法在<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:UP="urn:UP:Standard" exclude-result-prefixes="UP"> <xsl:output method="xml" indent="yes" /> <xsl:template match="UP:AuditFile"> <root> <xsl:apply-templates select="UP:Header"/> </root> </xsl:template> <xsl:template match="UP:Header"> <FileVersion> <xsl:value-of select="UP:FileVersion" /> </FileVersion> </xsl:template> </xsl:stylesheet> 中捕获音频。来自官方document

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