Author Topic: Callback or detecting when stream stops playing.  (Read 501 times)

KevC

  • Posts: 22
Hi, I am playing a stream and need to know when it ends.  Is there a way to register a callback for this?

Here is the code to start the audio.

Code: [Select]
Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_16BITS, IntPtr.Zero);

 int _bassStream = Bass.BASS_StreamCreatePush(44100, 1, BASSFlag.BASS_SAMPLE_FLOAT, IntPtr.Zero);

 Bass.BASS_StreamPutData(_bassStream, buffer, buffer.Length * 4);

 Bass.BASS_ChannelPlay(_bassStream, false);

Thank you.

Ian @ un4seen

  • Administrator
  • Posts: 26223
Re: Callback or detecting when stream stops playing.
« Reply #1 on: 5 Jul '24 - 13:51 »
Yes, you can use BASS_ChannelSetSync with BASS_SYNC_END to request a callback when the end is reached. Please see the BASS_ChannelSetSync documentation for details.

Note you will also need to tell BASS when your stream has no more data coming, by calling BASS_StreamPutData with BASS_STREAMPROC_END. You can do that in the same call as above if there's no more data after that:

Code: [Select]
Bass.BASS_StreamPutData(_bassStream, buffer, (buffer.Length * 4) | BASSStreamProc.BASS_STREAMPROC_END);

KevC

  • Posts: 22
Re: Callback or detecting when stream stops playing.
« Reply #2 on: 5 Jul '24 - 15:39 »
Thanks again Ian  :)

KevC

  • Posts: 22
Re: Callback or detecting when stream stops playing.
« Reply #3 on: 5 Jul '24 - 16:11 »
Hi Ian,

I am getting an error message:

Error (active)   CS0019   Operator '|' cannot be applied to operands of type 'int' and 'BASSStreamProc'   

with this code:

Code: [Select]
Bass.BASS_StreamPutData(_bassStream, buffer, (buffer.Length * 4) | BASSStreamProc.BASS_STREAMPROC_END);

KevC

  • Posts: 22
Re: Callback or detecting when stream stops playing.
« Reply #4 on: 5 Jul '24 - 16:25 »
Think I have it by casting it to (int)

KevC

  • Posts: 22
Re: Callback or detecting when stream stops playing.
« Reply #5 on: 5 Jul '24 - 16:48 »
I have added the code fine, but when the stream ends, its actually ending the whole program!  Is this anything to do with thread management or any kind of known thing?

Thanks.

Ian @ un4seen

  • Administrator
  • Posts: 26223
Re: Callback or detecting when stream stops playing.
« Reply #6 on: 5 Jul '24 - 17:08 »
If you run under the debugger with a breakpoint at the start of your callback function, does it get there before crashing? If not, it could be that your callback delegate is being garbage collected before it's called. Please see the "Callbacks and Delegates" section of the "Interoperating with Unmanaged Code" page in the BASS.Net documentation for info on preventing that.

KevC

  • Posts: 22
Re: Callback or detecting when stream stops playing.
« Reply #7 on: 5 Jul '24 - 17:12 »
Its not getting into the callback function.

Ill have a look at that recommendation, thanks.

KevC

  • Posts: 22
Re: Callback or detecting when stream stops playing.
« Reply #8 on: 5 Jul '24 - 17:55 »
Thank you Ian, now entering callback function on stream end  :)

Ferry

  • Posts: 6
Re: Callback or detecting when stream stops playing.
« Reply #9 on: 30 Sep '24 - 16:56 »
How can I detect when stream has reach  to an end when play via basswasapi? I think BASS_ChannelSetSync will not work in this situation.

Chris

  • Posts: 2221
Re: Callback or detecting when stream stops playing.
« Reply #10 on: 30 Sep '24 - 20:53 »
A EndSync Will also work on wasapi. (remember Bass Output Default will use also BaswasApi (Shared Mode).

Ian @ un4seen

  • Administrator
  • Posts: 26223
Re: Callback or detecting when stream stops playing.
« Reply #11 on: 1 Oct '24 - 14:14 »
With BASSWASAPI output, you'll be using a decoding channel (with BASS_STREAM_DECODE set). BASS_ChannelSetSync (BASS_SYNC_END) does work with them but note the callback will be when the decoder reaches the end, not when BASSWASAPI has finished playing it (which is later due to buffering). If you want the sync to be when playback reaches the end then you could perhaps have your callback trigger a delay/timer equal to the WASAPI buffer length. If the WASAPI buffer is small then the time difference may be small enough to not have to bother with that.

Ferry

  • Posts: 6
Re: Callback or detecting when stream stops playing.
« Reply #12 on: 1 Oct '24 - 15:24 »
With BASSWASAPI output, you'll be using a decoding channel (with BASS_STREAM_DECODE set). BASS_ChannelSetSync (BASS_SYNC_END) does work with them but note the callback will be when the decoder reaches the end, not when BASSWASAPI has finished playing it (which is later due to buffering). If you want the sync to be when playback reaches the end then you could perhaps have your callback trigger a delay/timer equal to the WASAPI buffer length. If the WASAPI buffer is small then the time difference may be small enough to not have to bother with that.

Thanks Ian, I will look into it. Now, in my project, I compare the decoding channel's position and length per 33ms to detect the end.(length - position < 0.1)
It works, but really stupid :)