Beispiel #1
0
        private void bPreview_Click(object sender, EventArgs e)
        {
            if (_audio == null)
            {
                _audio = new NAudio.Wave.WaveOut();

                if (_fileName.EndsWith(".ogg", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (_audioPath != null)
                    {
                        _audio.Init(new NAudio.Vorbis.VorbisWaveReader(_audioPath));
                    }
                    else if (_audioFile != null)
                    {
                        _audio.Init(new NAudio.Vorbis.VorbisWaveReader(_audioFile));
                    }
                }
                else if (_fileName.EndsWith(".mp3", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (_audioPath != null)
                    {
                        _audio.Init(new NAudio.Wave.Mp3FileReader(_audioPath));
                    }
                    else if (_audioFile != null)
                    {
                        _audio.Init(new NAudio.Wave.Mp3FileReader(_audioFile));
                    }
                }
                else
                {
                    return;
                }
                _audio.Play();
            }
            else
            {
                StopAudio();
            }
        }
        internal void PlayPreviewAudio()
        {
            if (_audio == null)
            {
                try
                {
                    if (_audioFileName.EndsWith(".ogg", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!string.IsNullOrWhiteSpace(_audioPath))
                        {
                            _audio      = new NAudio.Wave.WaveOut();
                            _waveReader = new NAudio.Vorbis.VorbisWaveReader(_audioPath);
                            _audio.Init(_waveReader);
                        }
                        else if (_audioFile != null)
                        {
                            _audio      = new NAudio.Wave.WaveOut();
                            _waveReader = new NAudio.Vorbis.VorbisWaveReader(_audioFile);
                            _audio.Init(_waveReader);
                        }
                    }
                    else if (_audioFileName.EndsWith(".mp3", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (!string.IsNullOrWhiteSpace(_audioPath))
                        {
                            _audio     = new NAudio.Wave.WaveOut();
                            _mp3Reader = new NAudio.Wave.Mp3FileReader(_audioPath);
                            _audio.Init(_mp3Reader);
                        }
                        else if (_audioFile != null)
                        {
                            _audio     = new NAudio.Wave.WaveOut();
                            _mp3Reader = new NAudio.Wave.Mp3FileReader(_audioFile);
                            _audio.Init(_mp3Reader);
                        }
                    }
                    else
                    {
                        Logger.Warn($"Unsupported audio file: {_audioFileName}");
                        return;
                    }

                    if (_waveReader != null || _mp3Reader != null)
                    {
                        _audio.PlaybackStopped += PreviewAudio_PlaybackStopped;
                        _audio.Play();
                    }
                    else
                    {
                        Sys.Message(new WMessage($"{ResourceHelper.Get(StringKey.FailedToPlayPreviewAudio)} {_audioFileName}"));
                    }

                    NotifyPropertyChanged(nameof(IsPlayingAudio));
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    Sys.Message(new WMessage($"{ResourceHelper.Get(StringKey.FailedToPlayPreviewAudio)} {_audioFileName}: {e.Message}"));
                }
            }
            else
            {
                StopAudio();
            }
        }