In UWP saving HTML5 Audio stream to mp3 file

自闭症网瘾萝莉.ら 提交于 2020-01-16 13:18:32

问题


I currently have a radio streaming App in the Windows 10 Store. Now I want to give my users the possibility to record the current stream to a mp3 File.

Does anyone of you have a suggestion on how to save the Audio Stream? I can't find the property or event that gives me the bytes to save them.

I'm using the Windows.Media.Playback.Mediaplayer class.

Thanks in advance

Christian


回答1:


In general you could not get Audio Stream from Mediaplayer directly. However, you could monitor the audio via Stereo Mix device.

Set Stereo Mix as default record device. And make a audio capture via MediaCapture class.

private async Task<bool> RecordProcess()
{
    if (buffer != null)
    {
        buffer.Dispose();
    }
    buffer = new InMemoryRandomAccessStream();
    if (capture != null)
    {
        capture.Dispose();
    }
    try
    {
        MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings
        {
            StreamingCaptureMode = StreamingCaptureMode.Audio
        };
        capture = new MediaCapture();
        await capture.InitializeAsync(settings);
        capture.RecordLimitationExceeded += (MediaCapture sender) =>
        {
            //Stop
            //   await capture.StopRecordAsync();
            record = false;
            throw new Exception("Record Limitation Exceeded ");
        };
        capture.Failed += (MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs) =>
        {
            record = false;
            throw new Exception(string.Format("Code: {0}. {1}", errorEventArgs.Code, errorEventArgs.Message));
        };
    }
    catch (Exception ex)
    {
        if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UnauthorizedAccessException))
        {
            throw ex.InnerException;
        }
        throw;
    }
    return true;
}

Note that Stereo could only monitor audio that output from the same hardware device. So you need to set available playback device. For code sample you could refer this.



来源:https://stackoverflow.com/questions/50100450/in-uwp-saving-html5-audio-stream-to-mp3-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!