Pergunta

I'm using NAudio library and having an issue calling a method. I created a class for playback of the audio files and am assigning a bool value to a button click. Everything works fine with the exception of the IF part of my code will run up until the call to the method (line of the if code). No error is generated, its just not going to the OnPlaybackStopped method. I was outputting some text as well so I could confirm this and also see the PlaybackStopped is in face true in both cases. The ELSE portion runs no problem and actually calls the method without issue once the end of the file is reached. Any thoughts? I know I'm probably just missing something easy here and just need another set of eyes to tell me what I'm doing wrong.

namespace AudioTool
{
    public class Playback
    {
        private IWavePlayer _audioPlayback { get; set; }
        private AudioFileReader _audioReader { get; set; }
        private WaveViewer _waveViewer { get; set; }
        private Button _playButton { get; set; }
        private Button _stopButton { get; set; }
        private bool _stopButtonClick { get; set; }

        public Playback(IWavePlayer audioPlayback, AudioFileReader audioReader, WaveViewer waveViewer, Button playButton, Button stopButton, bool stopButtonClick)
        {
            _audioPlayback = audioPlayback;
            _audioReader = audioReader;
            _waveViewer = waveViewer;
            _playButton = playButton;
            _stopButton = stopButton;
            _stopButtonClick = stopButtonClick;
        }

        public void CreateAudioPlayback(string audioPath)
        {
            string fullAudioPath = (audioPath);
            _audioPlayback = new WaveOut();
            _audioReader = new AudioFileReader(fullAudioPath);
            _audioPlayback.Init(_audioReader);

            if (_stopButtonClick == true)
            {
                Console.WriteLine(_stopButtonClick);
                Console.WriteLine(_audioPlayback.PlaybackState);
                _audioPlayback.PlaybackStopped += new EventHandler<StoppedEventArgs>(OnPlaybackStopped);
            }

            else
            {
                // Get file path and playback
                _audioPlayback.PlaybackStopped += new EventHandler<StoppedEventArgs>(OnPlaybackStopped);
                _audioPlayback.Play();


                // View the waveform of the report
                _waveViewer.WaveStream = new WaveFileReader(fullAudioPath);
            }
        }

        private void OnPlaybackStopped(object sender, StoppedEventArgs e)
        {
            _waveViewer.WaveStream.Dispose();
            _audioPlayback.Stop();
            _audioPlayback.Dispose();
            _audioReader.Dispose();
            _waveViewer.WaveStream = null;
            _audioPlayback = null;
            _audioReader = null;
            _playButton.Enabled = true;
        }
    }
}
Foi útil?

Solução

Figured out what was going on. When I clicked the Play button on one class (we'll call it main.cs) it began playing the audio via WaveOut in one audio stream. Then when I click the stop button in main.cs it was attempting to stop a new audio stream. So the functions related to audio such as stop had seemingly no affect because they were always trying to stop an audio stream that didn't have anything yet playing.

What I did to work around this problem was to add a few more lines of code to the main.cs so that the WaveOut was defined there, then each of the Play and Stop button clicks would then reference the class with the rest of the code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top