How can I play an ogg file from a website url in c#

471 Views Asked by At

I have this url : https://verses.quran.com/Shuraym/ogg/067001.ogg I need to play this audio on my c# WPF app without having to download it.

I've been using this for url with an mp3 file extension :

            // this code use NAudio
            using (var mf = new MediaFoundationReader(url))
            using (var wo = new WasapiOut())
            {
                wo.Init(mf);
                wo.Play();

                PlayingAudioWasapiOut.Add(wo);
                while (wo.PlaybackState == PlaybackState.Playing)
                {
                    await Task.Delay(1000);
                };
            }

I tried the code that works for the mp3 url, but it does not work with an ogg url (it just exit the method without any error at the line 1). I also tried using this :

            using (var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(url))
            using (var waveOut = new NAudio.Wave.WaveOutEvent())
            {
                waveOut.Init(vorbisStream);
                waveOut.Play();
                PlayingAudioWaveOutEvent.Add(waveOut);

                while (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    await Task.Delay(1000);
                }

            }

But it did not work because it is made for local file audio.

1

There are 1 best solutions below

0
ZoneArisKzae On

I made it

        internal static async Task PlayOggFileFromUrl(string lien)
    {
        var req = System.Net.WebRequest.Create(lien);
        using (Stream stream = req.GetResponse().GetResponseStream())
        {
            using (var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(stream))
            using (var waveOut = new NAudio.Wave.WaveOutEvent())
            {
                waveOut.Init(vorbisStream);
                waveOut.Play();

                while (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    await Task.Delay(1000);
                }

                waveOut.PlaybackStopped += (sender, e) =>
                {
                    waveOut.Dispose();
                    vorbisStream.Dispose();
                };
            }
        }
    }