Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Using AudioTrack to play consistent stream of PCm data

$
0
0

I have an application that gets consistent stream of PCm data, in a while loop. The loop works untill the stream has data. I then read the incomming stream into a byte[] and then write it to AudioTrack. The problem is that it plays ok for a couple of seconds, but then you can hear a lot of noise and sometimes audio again, but it works poorly. I tried to increase the buffer size of AudioTrack constructor, but no luck. The commented part was my try to have the data saving into a list in a sepperate thread, and then use that data, remove it and so on, on a different thread also. Like a self made buffer of sort. That works better, but it still has some interuptions, but less of them, and you can hear more audio.

For the first one you can hear like 1-2 seconds of music, than about 5s of noise, then just noise or sometimes audio, but for second one, you can hear a lot of music, then sometimes noise, but it stops playing comleatly.

This is my code:

public void Read()
    {
        System.Threading.Tasks.Task.Run(() =>
        {
            int _bufferSize;
            //List<byte[]> Queued = new List<byte[]>();
            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();

            byte[] myReadBuffer = new byte[8000];
            //bool bufferCheck = true;

            while (mmInStream.CanRead)
            {
                try
                {
                    mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);                        
                    //Queued.Add(myReadBuffer);
                    _output.Write(myReadBuffer, 0, myReadBuffer.Length);

                    /*System.Threading.Tasks.Task.Run(() =>
                    {
                        while (true)
                        {
                            try
                            {
                                /*if (Queued.Count != 0)
                                {
                                    if (bufferCheck)
                                    {
                                        Thread.Sleep(100);
                                        bufferCheck = false;
                                    }
                                    var bytes = Queued[0];
                                    Queued.Remove(bytes);
                                    _output.Write(bytes, 0, bytes.Length);
                                }
                                else
                                {
                                    break;
                                }*/
                                if (bufferCheck)
                                {
                                    Thread.Sleep(100);
                                    bufferCheck = false;
                                }
                                var bytes = Queued[0];
                                Queued.Remove(bytes);
                                _output.Write(bytes, 0, bytes.Length);
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Debug.WriteLine("Writing", ex);
                            }
                        }
                    }).ConfigureAwait(false);*/
                }
                catch (System.IO.IOException ex)
                {
                    System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
                }
            }
            _output.Stop();
        }).ConfigureAwait(false);
    }

Viewing all articles
Browse latest Browse all 91519

Trending Articles