如何使用Windows MediaCapture API捕获“仅音频”?

时间:2012-09-10 13:26:32

标签: c# windows audio-streaming audio-recording

我试图通过Windows MediaCapture API捕获“仅音频”。我使用以下代码但获得异常(HRESULT:0xC00D36D5)。

    MediaCapture captureMgr = new MediaCapture();   
    MediaCaptureInitializationSettings captureSettings = new MediaCaptureInitializationSettings();
    captureSettings.StreamingCaptureMode = StreamingCaptureMode.Audio;
    await captureMgr.InitializeAsync(captureSettings);
    this.CaptureVideoElement.Source = captureMgr;// exception is thrown here...
    await captureMgr.StartPreviewAsync();
请告诉我我做错了什么?或者请建议一个更好的出路。

1 个答案:

答案 0 :(得分:3)

我正在尝试做类似的事情,这是我通过MSDN / Samples查找的内容......

private async void OnStartRecordingBtnClick(object sender, RoutedEventArgs e)
{
    try
    {
        m_mediaCaptureMgr = new MediaCapture();
        var settings = new MediaCaptureInitializationSettings();
        settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
        await m_mediaCaptureMgr.InitializeAsync(settings);

    }
    catch (Exception exception)
    {
        // Do Exception Handling
    }
}

private async void OnStopRecordingBtnClick(object sender, RoutedEventArgs e)
{
    try
    {
        String fileName;
        if (!m_bRecording)
        {
            fileName = "audio.mp4";
            m_recordStorageFile = await Windows.Storage.KnownFolders.VideosLibrary.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
            MediaEncodingProfile recordProfile = null;
            recordProfile = MediaEncodingProfile.CreateM4a(Windows.Media.MediaProperties.AudioEncodingQuality.Auto);
            await m_mediaCaptureMgr.StartRecordToStorageFileAsync(recordProfile, this.m_recordStorageFile);
            m_bRecording = true;
        }
        else
        {
            await m_mediaCaptureMgr.StopRecordAsync();
            m_bRecording = false;
            if (!m_bSuspended)
            {
                var stream = await m_recordStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
                playbackElement.AutoPlay = true;
                playbackElement.SetSource(stream, this.m_recordStorageFile.FileType);
            }
        }
    }
    catch (Exception exception)
    {
        // Do Exception Handling...
        m_bRecording = false;
    }
}

希望这会有所帮助。 这是MSDN链接: enter link description here