uwpsignalraudio-streamingmediaelement

Play audio from byte array in UWP from SignalR


I have a project based on https://github.com/Guille1878/VideoChat that I created and posted here https://github.com/flaubertlekhem/UWPVideoAudioCall.

The purpose of this project is to be able to play audio from byte[]. I used the following code to get the byte[] then play it. Unfortunately, it does not work. I would like to know if anyone can help me on this matter.

THE CODE USED

private void InitAudioStream()
{
    SignalRConn.connection.On<byte[]>("DownloadAudioStream", async (stream) =>
    {
        try
        {
            if (stream != null)
            {
                InMemoryRandomAccessStream memoryBuffer = await ConvertTo(stream);
                Play(memoryBuffer);
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    });
}

internal static async Task<InMemoryRandomAccessStream> ConvertTo(byte[] arr)
{
    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
    await randomAccessStream.WriteAsync(arr.AsBuffer());
    randomAccessStream.Seek(0);
    return randomAccessStream;
}
public void Play(InMemoryRandomAccessStream memoryBuffer)
{
    /*
        * THIS CODE DOES NOT WORKS
        * THE ISSUE IDENTIFY IS THE FOLLOWING
        * THE AUDIO IS NOT PLAYED ON ANY TYPE OF SPEAKERS
        */
    MediaElement playbackMediaElement = new MediaElement();
    playbackMediaElement.SetSource(memoryBuffer, "MP3");
    playbackMediaElement.Play();
}

Thank you for your contribution.


Solution

  • It is now possible to stream the audio. I found the solution and updated the project on https://github.com/flaubertlekhem/UWPVideoAudioCall You can freely test the solution.

    To do so follow the instruction.

    1. Launch dotnet run with cmd on Chathub project
    2. Launch both sender and receiver projects
    3. In the sender project, select the microphone and the camera icons
    4. In the receiver project, select the camera icon to receive audio and video

    NOTE: There is a temporal gap between audio and video than I was not able to solve. Feel free to comment the github project if you can help me to correct it.