__UPDATE: I found the problem. In my Play() function, when playback is started, it causes a NullReferenceException which is sent in the PlaybackStopped event. However, the file continues playing fine.__
I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}
public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}
public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}
public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}
public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}
public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}
public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}
private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```
Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.
Thanks in advance for the help!
Comments: OronDF343, I had a similar issue with WaveOutEvent when running in an ASP.NET thread. Is your program running in a Windows Form? or something else? Jake
I'm trying to make an application which plays files on a playlist. Whenever the user clicks "next", it will start playing the next song, and immediately move to the next one before it ends. This seems to be caused by the PlaybackStopped event.
Here is the code: (I removed some irrelevant stuff)
```
public class BackgroundPlayer
{
private IWavePlayer myWaveOut;
private WaveStream myAudioFileReader;
public PlaybackStatus Status { get; private set; }
private int nowPlayingID;
public int NowPlayingID
{
get { return nowPlayingID; }
private set
{
nowPlayingID = value;
if (Status == PlaybackStatus.Playing)
Play();
else
{
Unload();
Load();
}
}
}
public void Next()
{
if (NowPlayingID < Playlist.Count - 1)
++NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = 0;
}
}
public void Previous()
{
if (NowPlayingID > 0)
--NowPlayingID;
else
{
Status = PlaybackStatus.Stopped;
NowPlayingID = Playlist.Count - 1;
}
}
public void Play()
{
Unload();
Load();
myWaveOut.Play();
Status = PlaybackStatus.Playing;
}
public void Stop()
{
if (myWaveOut != null)
myWaveOut.Stop();
Status = PlaybackStatus.Stopped;
}
public void Load()
{
myWaveOut = new DirectSoundOut();
myAudioFileReader = new AudioFileReader(NowPlaying.FilePath);
myWaveOut.PlaybackStopped += myWaveOut_PlaybackStopped;
myWaveOut.Init(myAudioFileReader);
}
public void Unload()
{
if (myWaveOut != null)
myWaveOut.Stop();
if (myAudioFileReader != null)
{
myAudioFileReader.Dispose();
myAudioFileReader = null;
}
if (myWaveOut != null)
{
myWaveOut.Dispose();
myWaveOut = null;
}
Status = PlaybackStatus.Stopped;
}
private void myWaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
Next();
}
}
```
Should I be using something other than DirectSoundOut?
This code had worked, until I changed something with the Play, Load, and Unload functions.
Thanks in advance for the help!
Comments: OronDF343, I had a similar issue with WaveOutEvent when running in an ASP.NET thread. Is your program running in a Windows Form? or something else? Jake