Exemplo n.º 1
0
 public void StopAudio()
 {
     Application.UseWaitCursor = false;
     StopAudioTimer.Stop();
     WaveOut.Stop();
     AudioUrl = null;
 }
Exemplo n.º 2
0
        public async Task PlayMp3FromUrl(string url, bool withTimer = true)
        {
            AudioUrl = url;
            Stream ms     = new MemoryStream();
            Stream stream = WebRequest.Create(url).GetResponse().GetResponseStream();

            byte[] buffer = new byte[32768];
            int    read;

            Application.UseWaitCursor = true;
            await Task.Run(() =>
            {
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
            });

            Application.UseWaitCursor = false;
            stream.Close();
            ms.Position = 0;
            WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms)));

            WaveOut.Init(blockAlignedStream);
            WaveOut.Play();
            if (withTimer)
            {
                StopAudioTimer.Start();
            }
            while (WaveOut.PlaybackState == PlaybackState.Playing)
            {
                await Task.Delay(100);
            }
            blockAlignedStream.Close();
            ms.Close();
        }