コード例 #1
0
 public bool Stop()
 {
     try
     {
         if (soundOut == null)
         {
             return(false);
         }
         if (soundOut.PlaybackState != CSCore.SoundOut.PlaybackState.Stopped)
         {
             soundOut.Stop();
         }
         if (waveSource != null)
         {
             waveSource.Dispose(); waveSource = null;
         }
         GC.WaitForPendingFinalizers();
         if (soundOut != null)
         {
             soundOut.Dispose(); soundOut = null;
         }
         GC.WaitForPendingFinalizers();
         return(true);
     }
     catch (Exception ex)
     {
         if (IsToShowMessageBoxOfExceptions)
         {
             MessageBox.Show(ex.Message);
         }
         return(false);
     }
 }
コード例 #2
0
        public bool StartAsync(string audioFilePath)
        {
            try
            {
                Stop();

                if (string.IsNullOrWhiteSpace(audioFilePath))
                {
                    throw new ArgumentNullException(audioFilePath);
                }
                var fullPath = System.IO.Path.GetFullPath(audioFilePath);
                if (System.IO.File.Exists(fullPath) == false)
                {
                    throw new System.IO.FileNotFoundException("Could not find the file", audioFilePath);
                }

                var ext = System.IO.Path.GetExtension(fullPath).ToLower();
                switch (ext)
                {
                case ".wav":
                    break;

                default:
                    throw new InvalidOperationException("Extension must be .wav or .ogg");
                }
                waveSource = CSCore.Codecs.CodecFactory.Instance.GetCodec(fullPath);
                if (waveSource == null)
                {
                    throw new ArgumentException("Could not open the file", fullPath);
                }
                AudioFileFullPath = fullPath;
                if (CSCore.SoundOut.WasapiOut.IsSupportedOnCurrentPlatform)
                {
                    soundOut = new CSCore.SoundOut.WasapiOut();
                }
                else
                {
                    soundOut = new CSCore.SoundOut.DirectSoundOut();
                }
                soundOut.Initialize(waveSource);
                soundOut.Play();
                return(true);
            }
            catch (Exception ex)
            {
                if (IsToShowMessageBoxOfExceptions)
                {
                    MessageBox.Show(ex.Message);
                }
                return(false);
            }
        }
コード例 #3
0
        private void LoadSoundOut()
        {
            if (_waveSource != null)
            {
                _soundOut = new CSCore.SoundOut.WasapiOut(true, CSCore.CoreAudioAPI.AudioClientShareMode.Shared,
                                                          100, System.Threading.ThreadPriority.Highest)
                {
                    Device = OutputDevice.ActualDevice
                };

                _soundOut.Initialize(_waveSource.ToSampleSource().ToWaveSource(16));
                // set defaults
                _soundOut.Volume = Volume;
            }
        }
コード例 #4
0
        public void Dispose()
        {
            if (_waveSource != null)
            {
                _waveSource.Dispose();
                _waveSource = null;
            }

            if (_soundOut != null)
            {
                _soundOut.Dispose();
                _soundOut = null;
            }

            if (_sampleSource != null)
            {
                _sampleSource.Dispose();
                _sampleSource = null;
            }
        }