How to play audio byte array in C#

mtsABCmtsABC Member ✭✭

I am trying to play an incomming stream of bytes comming from Bluetooth. I tried MediaPlayer, but the documentation says, it can only play full byte arrays(whole file), but not streaming bytes. The bytes are being read like this.

example:

123

123456

123456789

Also found a AudioTrack, but that has a couple of seconds of nothing and then something like music for a second and than only "fart" sounds from there. If anybody knows something, it would be appreciated.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited December 4

    I tried MediaPlayer, but the documentation says, it can only play full byte arrays(whole file)

    1.Try to write the bytes to a file and then play the audio file.

    string _file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), file_name);
    byte[] bytes = getBytes();// get the bytes
    
    File.WriteAllBytes(_file , bytes);
    

    2.You can also use DependencyService to pass the byte array to each platform to play it on the specific platform.
    Check the link:
    https://forums.xamarin.com/discussion/174042/how-to-play-audio-from-byte

Sign In or Register to comment.