Author Topic: Callback or detecting when stream stops playing.  (Read 183 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: 26028
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: 26028
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  :)