//Stop without next song public bool stopSound() { PlaybackStopType = PlaybackStopTypes.PlaybackStoppedByUser; if (playing) { outputDevice?.Dispose(); outputDevice = null; try { audioFile?.Dispose(); } catch (Exception ex) { Debug.Print("exception: " + ex.ToString()); } audioFile = null; playing = false; return(true); } else { outputDevice?.Stop(); audioFile?.Dispose(); audioFile = null; return(false); } }
private void BtnDump_Click(object sender, EventArgs e) { outputDevice.Dispose(); outputDevice = null; audioFile.Dispose(); audioFile = null; }
public void LoadFile(bool mode, out int time, string fileName = "") { remove(); reader?.Dispose(); speedControl?.Dispose(); reader = null; speedControl = null; string file; if (fileName == "") { file = SelectFile(); } else { file = fileName; } time = 0; if (file == null) { return; } reader = new AudioFileReader(file); time = (int)(reader.TotalTime.TotalSeconds + 0.5); var useTempo = mode; speedControl = new VarispeedSampleProvider(reader, 100, new SoundTouchProfile(useTempo, false)); }
public void CanBeDisposedMoreThanOnce() { var reader = new AudioFileReader(@"..\..\..\SampleData\Drums\closed-hat-trimmed.wav"); reader.Dispose(); Assert.DoesNotThrow(() => reader.Dispose()); }
public void Refresh() { if (Wave != null) { Wave.PlaybackStopped -= Wave_PlaybackStopped; Wave.Stop(); Wave.Dispose(); } if (Audio != null) { Audio.Dispose(); } Wave = null; Audio = null; Wave = new WaveOutEvent(); Wave.PlaybackStopped += Wave_PlaybackStopped; IsEnabled = false; Preview.Source = null; SongPosition.Value = 0; SongLength.Text = "00:00"; TextPosition.Text = "00:00"; SongName.Text = "Песня"; IsDrag = false; IsPause = true; PlayIcon.Kind = MaterialDesignThemes.Wpf.PackIconKind.Play; }
public void Play(Model.Song song) { if (song == null) { return; } if (audioFileReader != null) { audioFileReader.Dispose(); audioFileReader = null; } var t = Mp3Frame.LoadFromStream(null); NAudio.Wave.AcmMp3FrameDecompressor dec = new AcmMp3FrameDecompressor(new Mp3WaveFormat(t.SampleRate, 2, t.FrameLength, t.BitRate)); audioFileReader = new AudioFileReader(song.mp3Url); if (waveOutDevice != null) { waveOutDevice.Stop(); waveOutDevice.Dispose(); waveOutDevice = null; } waveOutDevice = new WaveOut(); waveOutDevice.Init(audioFileReader); waveOutDevice.Play(); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (_dsOne != null) { afrOne.Dispose(); loopOne.Dispose(); _dsOne.Dispose(); } if (_dsTwo != null) { afrTwo.Dispose(); loopTwo.Dispose(); _dsTwo.Dispose(); } if (driverOut != null) { driverOut.Dispose(); } //if (_WaveOutEventOne != null) //{ // _WaveOutEventOne.Dispose(); // _WaveOutEventOne = null; //} //if (_WaveOutEventTwo != null) //{ // _WaveOutEventTwo.Dispose(); // _WaveOutEventTwo = null; //} }
//========================================================= // Events //========================================================= /// <summary> /// Occurs when the _waveOutDevice sound playback is stopped. /// </summary> /// <remarks> /// This is used to ensure that the object is disposed after playback has stopped /// to prevent any type of memory leak and to also allow users to test sounds /// again if they choose to test sound immediatly after stopping playback /// </remarks> /// <param name="sender"></param> /// <param name="e"></param> private void _waveOutDevice_PlaybackStopped(object sender, StoppedEventArgs e) { if (_audioFileReader != null) { _audioFileReader.Dispose(); _audioFileReader = null; } this.TestButtonText = "Test Sound"; }
private void waveOutStopped(object sender, StoppedEventArgs e) { wo.Dispose(); afr.Dispose(); EnableButtons(); volume = 0; }
public void Load(string filename) { _player?.Stop(); _player?.Dispose(); _reader?.Dispose(); _reader = new AudioFileReader(filename); _player = new WaveOutEvent(); _player.Init(this); }
private void music1Start(object sender, EventArgs e) { wavePlayer.Stop(); wavePlayer.Dispose(); audioReader.Dispose(); screenRunning = false; PublicDataManager.currentTaskQueue.addTask(new Game(drawer, "music1")); wavePlayer.Stop(); wavePlayer.Dispose(); audioReader.Dispose(); screenRunning = false; }
private void OnPlaybackStopped(object sender, StoppedEventArgs e) { if (afr != null) { afr.Dispose(); afr = null; } if (waveout != null) { waveout.Dispose(); waveout = null; } }
public void CanBeDisposedMoreThanOnce() { var path = @"..\..\..\SampleData\Drums\closed-hat-trimmed.wav"; if (!File.Exists(path)) { Assert.Ignore("test file not found"); } var reader = new AudioFileReader(path); reader.Dispose(); Assert.DoesNotThrow(() => reader.Dispose()); }
private void initializeStream() { if (_audioFileReader != null) { _audioFileReader.Dispose(); _audioFileReader = null; } _audioFileReader = new AudioFileReader(_filePath) { Volume = _currentVolume }; }
/// <summary> /// 关闭音频播放,释放资源。 /// </summary> public static void AudioCleanUp() { if (AudioFileReader != null) { AudioFileReader?.Dispose(); AudioFileReader = null; } if (WavePlayer != null) { WavePlayer?.Stop(); WavePlayer?.Dispose(); WavePlayer = null; } }
public void startPlaying(int num) { if (!File.Exists(Application.StartupPath + '\\' + num + ".wav")) { return; } Console.WriteLine(" Begin playback of " + num + ".wav"); var audioFile = new AudioFileReader(num + ".wav"); if (device != -1) { var audioFile2 = new AudioFileReader(num + ".wav"); var player2 = new WaveOutEvent(); player2.DeviceNumber = -1; player2.Volume = volume / 100; player2.Init(audioFile2); // begin playback player2.Play(); player2.PlaybackStopped += (s, a) => { if (audioFile != null) { audioFile.Dispose(); } audioFile = null; player2.Dispose(); }; } var player = new WaveOutEvent(); player.DeviceNumber = device; player.Volume = volume / 100; player.Init(audioFile); // begin playback player.Play(); player.PlaybackStopped += (s, a) => { Console.WriteLine(" Playback ended"); if (audioFile != null) { audioFile.Dispose(); } audioFile = null; player.Dispose(); }; }
/// <summary> /// Play the music /// </summary> private void Play() { //Initialize Player if (Player == null) { Player = new WaveOut(); } if (SelectedItem == null) { return; } //if(SelectedItem.FullPath == Reader != null ? Reader. ) switch (Player.PlaybackState) { case PlaybackState.Playing: Player.Pause(); if (!Reader.Equals(new AudioFileReader(SelectedItem.FullPath))) { try { Reader.Dispose(); } catch { } Reader = new AudioFileReader(SelectedItem.FullPath); Player.Init(Reader); Player.Play(); } break; case PlaybackState.Paused: Player.Play(); break; case PlaybackState.Stopped: { try { Reader.Dispose(); } catch { } Reader = new AudioFileReader(SelectedItem.FullPath); Player.Init(Reader); Player.Play(); } break; } }
// SOUND // Play Sound public void playSound(HKSound sound) { if (waveOut != null) { debugLog("[Sound] Disposed"); waveOut.Dispose(); } waveOut = new WaveOut(); waveOut.DeviceNumber = selectedDevice; checkSoundState.Stop(); debugLog("[Action] Sound " + sound.filename); // VorbisWaveReader is not in AudioFileReader but is a ISampleProvider if (sound.extension == ".ogg") { if (currentOGG != null) { currentOGG.Dispose(); } currentOGG = new VorbisWaveReader(sound.completeFilename); waveOut.Init(currentOGG); waveOut.Play(); waveOut.Volume = sound.volume; checkSoundState.Start(); currentSound = sound; debugLog("[Sound] Volume : " + sound.volume); debugLog("[Sound] OGG Start"); } else if (".mp3.mp4.wav".Contains(sound.extension)) { if (audioFile != null) { audioFile.Dispose(); } audioFile = new AudioFileReader(sound.completeFilename); waveOut.Init(audioFile); waveOut.Play(); waveOut.Volume = sound.volume; checkSoundState.Start(); currentSound = sound; debugLog("[Sound] Volume : " + sound.volume); debugLog("[Sound] MP3/MP4/WAV Start"); } else { debugLog("[Sound] Not MP3, MP4 nor WAV"); } }
protected override void OnHandleDestroyed(EventArgs e) { State = LooperState.Waiting; reader1?.Dispose(); reader1 = null; reader2?.Dispose(); reader2 = null; if (parent != null) { parent.GetRecorder().RemoveTrack(sampleProvider1); parent.GetRecorder().RemoveTrack(sampleProvider2); } base.OnHandleDestroyed(e); }
void tryStop() { try { if (null != output) { output.Dispose(); output = null; } } catch { } try { if (null != meter) { meter = null; } } catch { } try { if (null != source) { source.Dispose(); source = null; } } catch { } }
public override AudioEffectResult ApplyEffect(ref AudioFileReader Reader) { if (!StartTime.HasValue) { StartTime = new TimeSpan(0); } if (!EndTime.HasValue) { EndTime = Reader.TotalTime; } string FileN = GetCacheFile($"mid_audio_trim_{DateTime.Now.Ticks}.mp3"); string CurFile = Reader.FileName; Console.WriteLine("Reader File: " + CurFile); Console.WriteLine("Closing current reader..."); Reader.Dispose(); if (TrimAudio(CurFile, FileN, StartTime, EndTime)) { Console.WriteLine("Trimmed"); Reader = new AudioFileReader(FileN); return(AudioEffectResult.Completed | AudioEffectResult.PreEncoded); } else { Console.WriteLine("Failed, re-creating reader..."); Reader = new AudioFileReader(FileN); Console.WriteLine("Cannot trim"); return(AudioEffectResult.Failed); } }
private void OnPlaybackStopped(object sender, StoppedEventArgs args) { outputDevice.Dispose(); outputDevice = null; audioFile.Dispose(); audioFile = null; }
private void BAddFile_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Sound Files(*.wav;*.mp3)|*.wav;*.mp3|WAVE Files (*.wav)|*.wav|MP3 Files (*.mp3)|*.mp3"; ofd.Multiselect = true; ofd.Title = "Select Sound File(s)..."; if (ofd.ShowDialog() == true) { foreach (String file in ofd.FileNames) { try { AudioFileReader afr = new AudioFileReader(file); string memeName = System.IO.Path.GetFileNameWithoutExtension(file); string memePath = file; TimeSpan memeLength = afr.TotalTime; memeList.Add(new MemeSound { Name = memeName, Length = memeLength, Path = memePath }); afr.Dispose(); addedFileList.Add(memePath); // TODO: Avoid Duplicates Properties.Settings.Default.LastFileList = String.Join(";", addedFileList.ToArray()); Properties.Settings.Default.Save(); } catch (Exception ex) { MessageBox.Show($"An error has occured while trying to add {file}. Error Message:\n\n{ex.ToString()}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } }
private void DisposeAll() { _cts?.Cancel(); _cts?.Dispose(); _reader?.Dispose(); DisposeDevice(); }
/// <summary> /// 截取音频后,对每个小音频获语音转文字 /// </summary> /// <param name="startMilliSecond">开始时间戳</param> /// <param name="endMilliSecond">结束时间戳</param> /// <param name="reader">音频流</param> /// <param name="i">用于创建新的文件</param> /// <param name="language">语言(zh,en)</param> /// <param name="splitTime">时间间隔</param> private void GetBaiduSpeech1(TimeSpan startMilliSecond, TimeSpan endMilliSecond, string sound_path, int i, string language, double splitTime) { try { var reader = new AudioFileReader(sound_path); //开始时间往前取startMilliSecond一半的偏移,结束时间往后取间隔时间的一半的偏移 if (i == 0) { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(startMilliSecond.TotalMilliseconds / 2); } else { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(splitTime / 2); } if (endMilliSecond < reader.TotalTime)//最后一次不用取偏移 { endMilliSecond = endMilliSecond + TimeSpan.FromMilliseconds(splitTime / 2); } TimeSpan span = endMilliSecond - startMilliSecond; if (span.TotalSeconds > 60)//超过60s,只取60秒 { span = TimeSpan.FromSeconds(60); } var trimed = reader.Skip(startMilliSecond).Take(endMilliSecond - startMilliSecond); //保存新的音频文件 string fileName = Path.GetFileNameWithoutExtension(sound_path) + "_" + i + Path.GetExtension(sound_path);//重命名文件 string newFolder = System.AppDomain.CurrentDomain.BaseDirectory + "NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/"; //重新存储到一个新的文件目录 if (!System.IO.Directory.Exists(newFolder)) { System.IO.Directory.CreateDirectory(newFolder); } string newFile = newFolder + fileName; WaveFileWriter.CreateWaveFile16(newFile, trimed); //绝对路径 string path_absolute = context.Server.MapPath("/NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/" + fileName); voiceFiles.Add(UploadFile.PostFile(path_absolute)); if (span == TimeSpan.FromSeconds(60))//音频大于60s { //小音频 var _reader = new AudioFileReader(sound_path); var _trimed = _reader.Skip(startMilliSecond).Take(span); //保存新的音频文件 string _fileName = "_" + Path.GetFileNameWithoutExtension(sound_path) + "_" + i + Path.GetExtension(sound_path);//重命名文件 string _newFile = newFolder + _fileName; WaveFileWriter.CreateWaveFile16(_newFile, _trimed); baidu_text.Add(BaiduSpeech.BaiduTranslateToText(_newFile, language, _trimed.WaveFormat.SampleRate.ToString())); } else { baidu_text.Add(BaiduSpeech.BaiduTranslateToText(newFile, language, trimed.WaveFormat.SampleRate.ToString())); } reader.Close(); reader.Dispose(); } catch (Exception ex) { LogHelper.Error(ex.Message); } }
private void Output_PlaybackStopped(object sender, StoppedEventArgs e) { reader.Dispose(); output.Dispose(); //Aqui es donde seria mas prudente detener el timer porque lo haria al terminar lo que se guarda en el ultimo buffer timer.Stop(); }
private void Output_PlaybackStopped(object sender, StoppedEventArgs e) { timer.Stop(); reader.Dispose(); output.Dispose(); }
private void AlarmWindow_Closed(object sender, EventArgs e) { IsClosed = true; device.Stop(); file.Dispose(); device.Dispose(); }
/// <summary> /// 获取mp3时长 整数秒 /// </summary> /// <param name="filePath">mp3路径</param> /// <returns></returns> public static string GetMp3Time(string filePath) { AudioFileReader autoFile = new AudioFileReader(filePath); autoFile.Dispose(); return(Math.Round(autoFile.TotalTime.TotalSeconds, 0).ToString()); }
private void LoadAudio() { try { if (!File.Exists(Song.Location)) { var error = string.Format("Cannot find file {0}", Song.Location); throw new Exception(error); } _audioFileReader = new AudioFileReader(Song.Location); ChannelLength = _audioFileReader.TotalTime.TotalSeconds; Song.Milliseconds = (int)_audioFileReader.TotalTime.TotalMilliseconds; ChannelPosition = 0; } catch (Exception e) { Logger.Error("Unable to load audio for {0}: {1}", Song.SongTitle, e.Message); if (_audioFileReader != null) { _audioFileReader.Dispose(); } _audioFileReader = null; Song.IsBadSong = true; } }