When I try to answer peony3000, I found some strange code.
I try to write this:
FileStream stream;
private void Play()
{
stream = new FileStream(@ "\SD\Sounds\Cesarica.mp3", FileMode.Open);
matjazMusic.MusicFinished += new MatjazMusic.MusicFinishedPlayingEventHandler(matjazMusic_MusicFinished);
matjazMusic.Play(stream);
}
void matjazMusic_MusicFinished(MatjazMusic sender)
{
matjazMusic.Play(stream);
}
Of course it is not working, because drivers after calling finished dispose stream.
So i try:
private void Play()
{
matjazMusic.MusicFinished += new MatjazMusic.MusicFinishedPlayingEventHandler(matjazMusic_MusicFinished);
matjazMusic.Play(new FileStream(@ "\SD\Sounds\CesaricaCut.mp3", FileMode.Open));
}
void matjazMusic_MusicFinished(MatjazMusic sender)
{
matjazMusic.Play(new FileStream(@ "\SD\Sounds\Cesarica.mp3", FileMode.Open));
}
Two different files. But i get null exception. After reading the code, that sounds logic.
First MusicFinished is raised. Here you call new play. In Play method, the m_playblackStream is set to new stream. And then new thread is started. After that the MusicFinished event is finished, and the code continue in drivers where it dispose the new stream and raise null exception.
private void PlayBackStreamThreadFunction()
{
byte[] block = new byte[32];
m_SPI.Config = m_dataConfig;
while (m_playblackStream.Read(block, 0, 32) > 0)
{
if (_stopPlayingRequested)
break;
while (m_dreq.Read() == false)
Thread.Sleep(1); // wait till done
m_SPI.Write(block);
}
this.OnMusicFinished(this); //!!! here new play is called, so the m_playblackStream is set to new value.
if (m_playblackStream != null)
{
m_playblackStream.Close();
m_playblackStream = null;
}
Reset();
_isPlaying = false;
}
If you can wait wait a week and i will fix this. Probably by starting one thread and add some ResetEvent.
Something like this:
private void PlayBackStreamThreadFunction()
{
while (true)
{
resetevent.WaitOne();
byte[] block = new byte[32];
m_SPI.Config = m_dataConfig;
while (m_playblackStream.Read(block, 0, 32) > 0)
{
if (_stopPlayingRequested)
break;
while (m_dreq.Read() == false)
Thread.Sleep(1); // wait till done
m_SPI.Write(block);
}
if (m_playblackStream != null)
{
m_playblackStream.Close();
m_playblackStream = null;
}
Reset();
_isPlaying = false;
this.OnMusicFinished(this); ///maybe add event parameter to reply or AutoCloseStream (true/false)
}
}
Meanwhile you can fix the drivers by putting this.OnMusicFinished(this); after closing the stream, and use my second example with 2 different FileStreams.