Example #1
0
 private void Downloader_Downloaded(SimultaneousStream stream)
 {
     if (SaveToFile)
     {
         using (var fs = File.Create(Path.Combine(AudioCache, AudioList[_currentIndex].Id.Value.ToString())))
         {
             stream.CopyTo(fs);
         }
     }
 }
Example #2
0
        private void Downloader_PreDownloaded(SimultaneousStream stream)
        {
            if (PlayingState != PlaybackState.Stopped)
            {
                OutputDevice.Stop();
            }

            AudioStream?.Dispose();

            stream.Position = 0; // -- for some reason first bytes are skipped.
            #region Not even apologizing for it.
            // -- Some MP3 files have huge attached images. I've done a limit up to 2 MB.
            int           readSize  = 64 * 1024;
            Mp3FileReader mp3Stream = null;
tryRead:    // goto if ya friend!
            try { mp3Stream = new Mp3FileReader(stream); }
            catch (InvalidDataException)
            {
                readSize += 64 * 1024; // 64 KB.

                int attempts = 0;
                while (stream.Length < readSize)
                {
                    attempts++; if (attempts > 20)
                    {
                        goto skipCurrent;
                    }
                    Thread.Sleep(50);
                }                           // -- not using break because it's likely that the download stopped

                if (readSize < 2048 * 1024) // 2 MB.
                {
                    goto tryRead;
                }

skipCurrent:
                if (_currentIndex > 0)
                {
                    PlayPrevious();
                }
                else
                {
                    PlayNext();
                }
            }
            #endregion Not even apologizing for it

            AudioStream = new WaveChannel32(mp3Stream)
            {
                PadWithZeroes = false
            };
            OutputDevice.Init(AudioStream);
            AudioStream.Volume = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia).AudioEndpointVolume.MasterVolumeLevelScalar;
            OutputDevice.Play();
        }