0

I have a audio player, made from AudioTrack, that gets and write byte[] packs of audio data (not the entire this at a time, but smaller pieces). The data is constantly playing, but sometimes - random, I get whie noise over that audio, and if I wait a little it will then play normal. This happens randomly. Is it because the buffer may be to small? I am playing wav files. This is my code:

public void Read()
{
    System.Threading.Tasks.Task.Run(() =>
    {                
        int _bufferSize;
        AudioTrack _output;

        _bufferSize = AudioTrack.GetMinBufferSize(44100, ChannelOut.Stereo, Android.Media.Encoding.Pcm16bit);

        _output = new AudioTrack(Android.Media.Stream.Music, 44100, ChannelOut.Stereo, Android.Media.Encoding.Pcm16bit, _bufferSize, AudioTrackMode.Stream);
        _output.Play();

        while (mmInStream.CanRead)
        {
            try
            {
                byte[] myReadBuffer = new byte[10000];
                mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                _output.Write(myReadBuffer, 0, myReadBuffer.Length);
            }
            catch (System.IO.IOException ex)
            {
                _output.Stop();
                System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
            }
        }
        _output.Stop();
    }).ConfigureAwait(false);
}
MTS
  • 19
  • 2
  • 14

1 Answers1

0

From AudioTrack article, we can see that it said that It allows streaming of PCM audio buffers to the audio sink for playback, wav is not PCm audio, so it will cause some white noise.

I find one article about using AudioTrack to play wav file that you can take a look:

Using AudioTrack in Android to play a WAV file

halfer
  • 19,824
  • 17
  • 99
  • 186
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16