i think i have a solution that works for me. i first tried with AutoResetEvent/ManualResetEvent, but the WaitOne method blocks indefinitely in certain cases
and i couldn't figure out why. so i did it with BASS_ChannelIsActive.
There are 2 methods one sync method which blocks and one async method which doesn't. the bevavior ist very similar to ``BOOL WINAPI PlaySound''
in winmm.dll with flags SND_SYNC and SND_ASYNC. here some code lines:
private static int stream_sync;
private static int stream_async;
//init sound device
internal static void init_bass_device(int hdev)
{
stream_sync = 0;
stream_async = 0;
//dont init more than one device
Bass.BASS_Free();
if (Bass.BASS_Init(hdev, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
{
}
else
_wrln("Bass error: {0}" + Bass.BASS_ErrorGetCode());
}
//ASYNC
internal static void play_bass_device_async(string soundfile, BASSFlag flags)
{
//i dont want to play sounds at the same time, so i just cancel the synchronous stream
if(Bass.BASS_ChannelIsActive(stream_async) != BASSActive.BASS_ACTIVE_STOPPED)
Bass.BASS_ChannelStop(stream_async);
stream_async = Bass.BASS_StreamCreateFile(soundfile, 0L, 0L, flags);
if (stream_async != 0)
{
// play the channel
Bass.BASS_ChannelPlay(stream_async, false);
}
else
{
// error
Console.WriteLine("Stream error: {0}", Bass.BASS_ErrorGetCode());
}
}
//SYNC
internal static void play_bass_device_sync(string soundfile, BASSFlag flags)
{
//wait until stream_sync has finished
while (Bass.BASS_ChannelIsActive(stream_sync) != BASSActive.BASS_ACTIVE_STOPPED)
{
Thread.Sleep(20);
}
//i dont want to play sounds at the same time, so i just cancel the asynchronous stream
if (Bass.BASS_ChannelIsActive(stream_async) != BASSActive.BASS_ACTIVE_STOPPED)
Bass.BASS_ChannelStop(stream_async);
// create a stream channel from a file
stream_sync = Bass.BASS_StreamCreateFile(soundfile, 0L, 0L, flags);
if (stream_sync != 0)
{
// play the channel
Bass.BASS_ChannelPlay(stream_sync, false);
}
else
{
// error
Console.WriteLine("Stream error: {0}", Bass.BASS_ErrorGetCode());
}
//wait until stream_sync has finished
while (Bass.BASS_ChannelIsActive(stream_sync) != BASSActive.BASS_ACTIVE_STOPPED)
{
Thread.Sleep(20);
}
}
//calling method
private void plays(string str, main.PlaySoundFlags flgs)
{
//play sound only on rear/surround speakers
BASSFlag spk_flag = BASSFlag.BASS_SPEAKER_REAR;
string cur_dir = Directory.GetCurrentDirectory();
string soundfile = @cur_dir + @"\Media\" + str + ".wav";
if(!File.Exists(soundfile))
return;
main._DEBUG("playSound(): " + soundfile);
if(flgs == main.PlaySoundFlags.SND_SYNC)
main.play_bass_device_sync(soundfile, spk_flag);
else if(flgs == main.PlaySoundFlags.SND_ASYNC)
main.play_bass_device_async(soundfile, spk_flag);
}
thx for your help!