WaveOut stuttering while another media is playing in another environment

257 Views Asked by At

I am working on streaming mp3 in real time using NAudio library. I have prepared a code to do it and it works good in general except while another media is playing in another process (e.g. Youtube, local mp3 players..), . At that time, mp3 is stuttering. I don't know why this is happening. How to solve this problem?

Code to stream in Console Application. Copy paste and you can try it.

    static void Main(string[] args)
    {
        AcmMp3FrameDecompressor decompressor = null;
        BufferedWaveProvider provider = null;
        WaveFormat mp3format = null;
        WaveOut waveOut = new WaveOut();
        long size = 0;
        byte[] decbuffer = new byte[50 * 1024];

        //I am using mp3 links converted by listentoyoutube.com 
        //But links will be expired, so I didnt put any

        string url = "";
        string path = Path.Combine(Path.GetTempPath(), "test.mp3");

        CheckUrlandCreateTools(url, ref decompressor, ref mp3format, ref size);

        FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
        Stream remote = resp.GetResponseStream();
        Mp3Frame frame = null;
        MemoryStream ms = null;
        byte[] buffer = new byte[1024];
        int read = 0;
        long offset = 0;
        provider = new BufferedWaveProvider(decompressor.OutputFormat);
        provider.BufferDuration = TimeSpan.FromSeconds(20);
        waveOut.Init(provider);
        waveOut.Play();
        while (waveOut.PlaybackState == PlaybackState.Playing)
        {
            if((read = remote.Read(buffer, 0, buffer.Length)) > 0)
            fs.Write(buffer, 0, read);
            fs.Flush();
            ms = new MemoryStream(ReadStreamPartially(fs, offset, 100 * 1024));
            try
            {
                frame = Mp3Frame.LoadFromStream(ms);
                if (frame == null)
                    continue;
            }
            catch
            {
                continue;
            }

            offset += ms.Position;
            int decompressed = decompressor.DecompressFrame(frame, decbuffer, 0);
            provider.AddSamples(decbuffer, 0, decompressed);
            if (IsBufferNearlyFull(provider))
                Thread.Sleep(500);
        }
    }

    public static byte[] ReadStreamPartially(System.IO.FileStream stream,
                                        long offset, long count)
    {
        long originalPosition = stream.Position;
        stream.Position = offset;

        byte[] readBuffer = new byte[4096];
        byte[] total = new byte[count];
        int totalBytesRead = 0;
        int byteRead;

        while ((byteRead = stream.ReadByte()) != -1)
        {
            Buffer.SetByte(total, totalBytesRead, (byte)byteRead);
            totalBytesRead++;
            if (totalBytesRead == count)
                break;
        }
        if (totalBytesRead < count)
        {
            byte[] temp = new byte[totalBytesRead];
            Buffer.BlockCopy(total, 0, temp, 0, totalBytesRead);
            stream.Position = originalPosition;
            return temp;
        }
        stream.Position = originalPosition;
        return total;
    }
    public static bool IsBufferNearlyFull(BufferedWaveProvider bufferedWaveProvider)
    {
        return bufferedWaveProvider != null &&
               bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes
               < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4;
    }
    public static void CheckUrlandCreateTools(string url, ref AcmMp3FrameDecompressor decompressor,
        ref WaveFormat format, ref long size)
    {
        HttpWebRequest req = SendRequest(url, 0, 0);
        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
        size = resp.ContentLength;
        Stream str = resp.GetResponseStream();
        byte[] buffer = new byte[1024];
        byte[] storer = new byte[1024 * 100];
        int bytesRead = 0;
        int total = 0;
        while ((bytesRead = str.Read(buffer, 0, buffer.Length)) > 0)
        {
            Buffer.BlockCopy(buffer, 0, storer, total, bytesRead);
            total += bytesRead;

            Mp3Frame frame = Mp3Frame.LoadFromStream(new MemoryStream(storer));
            if (frame == null) continue;

            format = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2,
            frame.FrameLength, frame.BitRate);
            decompressor = new AcmMp3FrameDecompressor(format);

            req.Abort();
            resp.Close();
            str.Close();

            break;
        }
    }

    public static HttpWebRequest SendRequest(string url, long from, long to)
    {
        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
        req.Credentials = CredentialCache.DefaultCredentials;
        req.Accept = "*/*";
        req.KeepAlive = false;
        req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
        req.AllowAutoRedirect = true;
        if (to > 0)
            req.AddRange(from, to);
        return req;
    }
}
0

There are 0 best solutions below