Quantcast
Channel: naudio Work Item Rss Feed
Viewing all articles
Browse latest Browse all 738

Created Unassigned: PlaybackStopped event fires at start of playback [16441]

$
0
0
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 : IDisposable
{
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();
}
}
```

Could the problem be that this is in a seperate thread from the Form?
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!

Viewing all articles
Browse latest Browse all 738

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>