protected Dispose ( bool disposing ) : void | ||
disposing | bool | True if called from Dispose |
return | void |
// Stops music playback public void Stop() { if (output != null) { if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) { output.Stop(); } } if (reader != null) { reader.Dispose(); reader = null; } }
protected virtual void LoadAudio() { if ((Path.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)) || (Path.EndsWith(".wav", StringComparison.OrdinalIgnoreCase))) { if (audio != null) { audio.Dispose(); } audio = new NAudio.Wave.AudioFileReader(Path); output.Init(audio); } else { throw new FileTypeException("File type must be .mp3 or .wav"); } }
static void Main(string[] args) { var quit = new ManualResetEvent(false); Console.CancelKeyPress += (s, a) => { quit.Set(); a.Cancel = true; }; var device = new DirectSoundOut(); var audio = new AudioFileReader("doowackadoo.mp3"); device.Init(audio); device.Play(); Console.WriteLine("Playing doowackadoo; Ctrl+C to quit"); quit.WaitOne(); if(device != null) device.Stop(); if(audio != null) audio.Dispose(); if(device != null) device.Dispose(); }
public void Run(string[] args) { m_settingsModel = LoadSettings(); m_running = true; m_eyeClients = new Dictionary<string, TcpClient>(); m_controllerClients = new Dictionary<string, ControllerItem>(); m_eyeDataQueue = new Queue<byte[]>(); m_controllerDataQueue = new Queue<ControllerData>(); m_commandsQueue = new Queue<Command>(); m_waveOutDevice = new WaveOut(); m_audioFileReader = new AudioFileReader("alarms/default.mp3"); m_waveOutDevice.Init(m_audioFileReader); var argMode = ArgMode.None; foreach (var arg in args) { if (argMode == ArgMode.None) { if (arg == "-ep" || arg == "--eye-port") argMode = ArgMode.EyePort; else if (arg == "-cp" || arg == "--controller-port") argMode = ArgMode.ControllerPort; else if (arg == "-t" || arg == "--token") argMode = ArgMode.Token; else if (arg == "-tr" || arg == "--treshold") argMode = ArgMode.Treshold; else if (arg == "-aa" || arg == "--armed-alarm") argMode = ArgMode.ArmedAlarm; else if (arg == "-s" || arg == "--silent") argMode = ArgMode.Silent; } else if (argMode == ArgMode.EyePort) { var v = m_settingsModel.EyePort; if (int.TryParse(arg, out v)) m_settingsModel.EyePort = v; argMode = ArgMode.None; } else if (argMode == ArgMode.ControllerPort) { var v = m_settingsModel.ControllerPort; if (int.TryParse(arg, out v)) m_settingsModel.ControllerPort = v; argMode = ArgMode.None; } else if (argMode == ArgMode.Token) { m_settingsModel.Token = arg.Trim(); argMode = ArgMode.None; } else if (argMode == ArgMode.Treshold) { var v = m_settingsModel.Treshold; if (int.TryParse(arg, out v)) m_settingsModel.Treshold = v; argMode = ArgMode.None; } else if (argMode == ArgMode.ArmedAlarm) { if (arg == "y" || arg == "yes" || arg == "t" || arg == "true") m_settingsModel.ArmedAlarm = true; else if (arg == "n" || arg == "no" || arg == "f" || arg == "false") m_settingsModel.ArmedAlarm = false; argMode = ArgMode.None; } else if (argMode == ArgMode.Silent) { if (arg == "y" || arg == "yes" || arg == "t" || arg == "true") m_settingsModel.Silent = true; else if (arg == "n" || arg == "no" || arg == "f" || arg == "false") m_settingsModel.Silent = false; argMode = ArgMode.None; } } m_alarmIsArmed = m_settingsModel.ArmedAlarm; Console.WriteLine("* Connection token: " + m_settingsModel.Token); SpawnEyeListenerTask(); SpawnControllerListenerTask(); SpawnEyeProcessorTask(); SpawnControllerProcessorTask(); SpawnCommandsExecutorTask(); while (m_running) { var line = Console.ReadLine(); if (line == "exit") m_running = false; else if (line == "arm") ArmAlarm(); else if (line == "disarm") DisarmAlarm(); else if (line == "alarm-on") StartAlarm(); else if (line == "alarm-off") StopAlarm(); } lock (m_commandsLock) { if (m_commandsQueue != null) { foreach (var command in m_commandsQueue) command.PerformAction(); m_commandsQueue.Clear(); } } lock (m_eyeClientsLock) { if (m_eyeClients != null) foreach (var eye in m_eyeClients) eye.Value.Close(); } lock (m_controllerClientsLock) { if (m_controllerClients != null) foreach (var controller in m_controllerClients) controller.Value.Client.Close(); } while (true) { lock (m_lock) { if (m_eyeListener == null && m_controllerListener == null && m_eyeDataQueue == null && m_controllerDataQueue == null && m_commandsQueue == null) break; } Thread.Sleep(10); } m_alarmIsPlaying = false; lock (m_alarmLock) { m_waveOutDevice.Stop(); m_audioFileReader.Dispose(); m_audioFileReader = null; m_waveOutDevice.Dispose(); m_waveOutDevice = null; } m_eyeClients = null; m_controllerClients = null; SaveSettings(m_settingsModel); Thread.Sleep(1000); }
private void OnMp3RepositionTestClick(object sender, EventArgs e) { var filename = SelectInputFile(); if (filename == null) return; var wo = new WaveOut(); var af = new AudioFileReader(filename); wo.Init(af); var f = new Form(); var b = new Button() { Text = "Play" }; b.Click += (s, a) => wo.Play(); var b2 = new Button() { Text = "Stop", Left = b.Right }; b2.Click += (s, a) => wo.Stop(); var b3 = new Button { Text = "Rewind", Left = b2.Right }; b3.Click += (s, a) => af.Position = 0; f.FormClosed += (s, a) => { wo.Dispose(); af.Dispose(); }; f.Controls.Add(b); f.Controls.Add(b2); f.Controls.Add(b3); f.ShowDialog(this); }
public void Play(object sender, MouseButtonEventArgs e) { var button = (NumboardButton)sender; if (button == null) return; if (button.Source == null) return; if (!File.Exists(button.Source)) { MessageBox.Show("Cannot Find file '" + button.Source + "'"); return; } var fileType = System.IO.Path.GetExtension(button.Source); var volume = button.Volume; if (volume == null) { volume = 1; } //primary ouput var primaryReader = new AudioFileReader(button.Source); primaryReader.Volume = (float)(volume * MasterVolume); var primaryWaveOut = new WaveOut(); primaryWaveOut.DeviceNumber = SelectedPrimaryOutputDevice; //secondary ouput var secondaryReader = new AudioFileReader(button.Source); secondaryReader.Volume = (float)(volume * MasterVolume); var secondaryWaveOut = new WaveOut(); secondaryWaveOut.DeviceNumber = SelectedSecondaryOutputDevice; primaryWaveOut.PlaybackStopped += StopStream; try { primaryWaveOut.Init(primaryReader); primaryWaveOut.Play(); secondaryWaveOut.Init(secondaryReader); secondaryWaveOut.Play(); PlayingStreams.Add(new PlayingStream { Button = button, PrimaryReader = primaryReader, SecondaryReader = secondaryReader, PrimaryWaveOut = primaryWaveOut, SecondaryWaveOut = secondaryWaveOut }); } catch (MmException ex) { primaryReader.Dispose(); primaryWaveOut.Dispose(); secondaryReader.Dispose(); secondaryWaveOut.Dispose(); if (ex.Result == MmResult.MemoryAllocationError) { StopAllStreams(); } } }
/// <summary> /// Load music from filename. If not a wav file, convert to wav (so we can seek accurately). /// Can call from any thread. /// </summary> void loadFile(string filename) { Status("Loading {0}", filename); AudioFileReader r = new AudioFileReader(filename); if (Path.GetExtension(filename).ToLower() != ".wav") { string wavfilename = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(filename) + ".wav"); Status("Transcoding to {0}", wavfilename); WaveFileWriter.CreateWaveFile(wavfilename, r); r.Dispose(); r = new AudioFileReader(wavfilename); Status("Loading {0}", filename); } if (m_Reader != null) m_Reader.Dispose(); m_Reader = r; }