2

I'm using NAudio with

WaveOutEvent Klangwiedergabegeraet;

private void Play(string Dateiname)
{
    Klangwiedergabegeraet = new WaveOutEvent();
    Klangwiedergabegeraet.DeviceNumber = comboBox1.SelectedIndex;
    ISampleProvider StueckchenHalter = null;
    StueckchenHalter = CreateInputStreamS(Dateiname);
    Klangwiedergabegeraet.Init(new SampleToWaveProvider(StueckchenHalter));  
    Klangwiedergabegeraet.Play();
}

private void Cancel()
{
    if (Klangwiedergabegeraet != null)
    {
        Klangwiedergabegeraet.Stop();
        Klangwiedergabegeraet.Dispose();
    }
}

When running Cancel(), it won't stop. When I used

WaveOut Klangwiedergabegeraet;

private void Play(string Dateiname)
{
    Klangwiedergabegeraet = new WaveOut();
    ...
}

private void Cancel()
{
    if (Klangwiedergabegeraet != null)
    {
        Klangwiedergabegeraet.Stop();
        Klangwiedergabegeraet.Dispose();
    }
}

It worked. Why is this and what to change?

P.S I am using WaveOutEvent instead of WaveOut because WaveOut does not have the property DeviceNumber.

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Peter Fren
  • 93
  • 3
  • 9
  • WaveOutEvent is more sensitive to crappy audio drivers. WaveOut *does* have a DeviceNumber property so maybe you should first update your version of NAudio. – Hans Passant Nov 03 '12 at 11:02
  • 7
    *`Klangwiedergabegeraet`* - seriously? :/ Sounds like one of the worst german variable names I've ever read... and as a more serious suggestion, using the default names for UI elements such as `comboBox1` is a bad idea. – ThiefMaster Nov 03 '12 at 11:35
  • 1
    It's an object, no variable. And I like `StueckchenHalter` even more - how does that make you feel? – Peter Fren Nov 03 '12 at 11:44
  • @ThiefMaster Jetzt sehe ich erst, daß du Deutscher bist. Da es hier keine PN-Funktion gibt antworte ich eben hier auf deutsch. Ja, ich verwende gerne deutsche Objektnamen, sogar im Originalcode mit ä und ß. – Peter Fren Nov 03 '12 at 19:11

1 Answers1

5

If you are running on a non-GUI thread, then WaveOutEvent is the recommended way to do things, not WaveOut as that will choose function callbacks which can be unreliable on some soundcards. Also, WaveOut does have a DeviceNumber property.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • According to Intellisense, it doesn't. For some reason without changing anything I am aware of, WaveOutEvent is working now. – Peter Fren Nov 03 '12 at 19:07
  • 1
    `WaveOutEvent` has a `DeviceNumber` property as well. This question is from 2012, so maybe things have change since. – tigrou Feb 23 '21 at 11:39