TiMeJuMp
TiMeJuMp

Reputation: 433

In UWP saving HTML5 Audio stream to mp3 file

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

Upvotes: 0

Views: 339

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

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

enter image description here

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.

Upvotes: 2

Related Questions