Ejemplo n.º 1
0
        public async Task PlayUrlAsync(Uri url, IAudioClient client)
        {
            _client = client;
            _client.SpeakingUpdated += OnSpeakingUpdated;
            _isPlaying = true;

            using (var ms = new MemoryStream())
            {
                using (Stream stream = WebRequest.Create(url.ToString())
                                       .GetResponse().GetResponseStream())
                {
                    byte[] buffer = new byte[32768];
                    int    read;
                    while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                }

                ms.Position = 0;
                using (WaveStream blockAlignedStream =
                           new BlockAlignReductionStream(
                               WaveFormatConversionStream.CreatePcmStream(
                                   new Mp3FileReader(ms))))
                {
                    var audioStream = _client.CreatePCMStream(AudioApplication.Mixed, 98304);
                    await blockAlignedStream.CopyToAsync(audioStream);
                }
            }
        }