Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: two problems with wave playack

$
0
0
Hi,

I'm writing WPF application with the newest NAudio. I have two problems with playing wave file.
First problem is wit not executing PlaybackStopped event. This is my code:
        private void InitializeAudio()
        {
            System.OperatingSystem osInfo = System.Environment.OSVersion;

            if (osInfo.Version.Major >= 6)
            {
                waveOut = new WasapiOut(AudioClientShareMode.Exclusive, false, 300);
            }
            else
            {
                waveOut = new DirectSoundOut(300);
            }
        }


        private void CloseWaveOut()
        {
            if (waveOut != null)
            {
                if (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    waveOut.Stop();
                }
                waveOut.Dispose();
                waveOut = null;
            }

            if (waveStream != null)
            {
                waveStream.Close();
                waveStream.Dispose();
                waveStream = null;
            }

            if (waveReader != null)
            {
                waveReader.Close();
                waveReader.Dispose();
                waveReader = null;
            }
        }


        private void OnWaveOutPlaybackStopped(object sender, StoppedEventArgs stoppedEventArgs)
        {
            log.Debug("Odtwarzanie pliku wav zakończone");

            if (stoppedEventArgs.Exception != null)
            {
                log.Error(stoppedEventArgs.Exception.Message);
            }
            CloseWaveOut();
            //InitializeAudio();
        }


        private void PlayVoiceMessage()
        {
            if (File.Exists(m_waveFile))
            {
                try
                {
                    CloseWaveOut();
                    InitializeAudio();

                    waveReader = new WaveFileReader(m_waveFile);
                    waveStream = new WaveChannel32(waveReader);
#if DEBUG
                    string strFormat = waveStream.WaveFormat.ToString();
                    string strSampleRate = waveStream.WaveFormat.SampleRate.ToString();
                    string strChannels = waveStream.WaveFormat.Channels.ToString();
                    string sMsg = String.Format("Format: {0}, samplerate: {1}, channels: {2}.", strFormat, strSampleRate, strChannels);
                    log.Debug(sMsg);
#endif                                  
                    waveOut.Init(waveStream);
                    waveOut.PlaybackStopped += new EventHandler<NAudio.Wave.StoppedEventArgs>(OnWaveOutPlaybackStopped);
                    log.Info("Rozpoczynam odtwarzanie pliku wav");
                    waveOut.Play();

                    //CloseWaveOut();
                }
                catch (Exception e)
                {
                    string sMsg = String.Format("Błąd odtwarzania! w {0}. Przyczyna: {1}.\nŚcieżka {2}", e.Source, e.Message, e.StackTrace);
                    log.Error(sMsg);
                    AddMessageToLog(sMsg);
                }
            }
            else
            {
                log.Error("Plik Wav nie istnieje");
            }
        }
If I uncomment CloseWaveOut(); just after waveOut.Play(); then sound is immediately cut. If I don't uncomment it playing finishes but event doesn't occur. Can anybody suggest something?

Second problem is knocking in speekers at the beginning and at the end of playing. Is it possible to avoid this knocking?

tegards
Tomek

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>