简体   繁体   中英

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.

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 do.net 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM