简体   繁体   中英

White noise while playing audio in c#

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);
}

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

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