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

Streaming PCm bytes to AudioTrack causes white noise

$
0
0

I have an app, that gets every mp3 file on the device, then it decodes it using MP3Sharp library to PCm bytes. They are then send to another device via Bluetooth. The other device then assembles AudioTrack class, and starts playing it, then it starts writing to the AudioTrack while the Bluetooth stream is available. The problem is that every couple seconds, the there is a 1 - 2 s of white noise and than the player start the next part of the song. How would I remove that. Is it something about the file or the player?

This is how I send the file to dependency service:

`private void SendData(object sender, EventArgs e)
    {
        System.Threading.Tasks.Task.Run(() =>
        {
            // open the mp3 file.
            MP3Stream stream = new MP3Stream(pathList[ShowSongFile.SelectedIndex]);

            // Create the buffer.
            byte[] buffer = new byte[4096];

            // read the entire mp3 file.
            int bytesReturned = 1;

            while (bytesReturned > 0)
            {
                bytesReturned = stream.Read(buffer, 0, buffer.Length);
                DependencyService.Get<BluetoothManager>().Write(buffer);
            }

            // close the stream after we're done with it.
            stream.Close();

        }).ConfigureAwait(false);
    }`

And this is how I receive the file on another device:

    `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[2000];
                    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);
    }`

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>