Author Topic: How to stop loading BASS_StreamCreateURL ?  (Read 95 times)

teq

  • Posts: 29
How to stop loading BASS_StreamCreateURL ?
« on: 10 Jan '19 - 13:55 »
Hi there!
I am using Thread for loading tracks
Code: [Select]
loadingPackPreviewTrackThread = new Thread(() => {
                    previewNetStream = Bass.BASS_StreamCreateURL(Uri.EscapeUriString($"{CommonSettings.Instance.assetBundlesURL}{model.clipName.ToLower()}.mp3"), 0, BASSFlag.BASS_DEFAULT, null, IntPtr.Zero);

                    Bass.BASS_ChannelPlay(previewNetStream, false);
                });
If player click another track, while this one still loading, i need somehow to stop loading current, otherwise there will be few simultaneously playing tracks.
« Last Edit: 10 Jan '19 - 15:09 by teq »

Ian @ un4seen

  • Administrator
  • Posts: 21447
Re: How to stop loading BASS_StreamCreateURL ?
« Reply #1 on: 10 Jan '19 - 15:03 »
What you can do is have a "request" counter that increments with each new URL opening thread that you create, and then have the thread free (instead of playing) its stream if its request isn't still the latest one. A demonstration of this can be found in the NETRADIO.C example that's included in the BASS package. Note the NETRADIO example also disables BASS_StreamCreateURL pre-buffering via the BASS_CONFIG_NET_PREBUF_WAIT option, which I suggest you do too (no point pre-buffering if the stream may be freed).

teq

  • Posts: 29
Re: How to stop loading BASS_StreamCreateURL ?
« Reply #2 on: 10 Jan '19 - 15:46 »
What you can do is have a "request" counter that increments with each new URL opening thread that you create, and then have the thread free (instead of playing) its stream if its request isn't still the latest one. A demonstration of this can be found in the NETRADIO.C example that's included in the BASS package. Note the NETRADIO example also disables BASS_StreamCreateURL pre-buffering via the BASS_CONFIG_NET_PREBUF_WAIT option, which I suggest you do too (no point pre-buffering if the stream may be freed).
Can't find this example (
Do you mean next approach ?
Code: [Select]

private int latestPreviewReqCount = 0;

...some method

latestPreviewReqCount++;
loadingPackPreviewTrackThread = new Thread(() => {
int currentReqCount = latestPreviewReqCount;
previewNetStream = Bass.BASS_StreamCreateURL(Uri.EscapeUriString($"{CommonSettings.Instance.assetBundlesURL}{model.clipName.ToLower()}.mp3"), 0, BASSFlag.BASS_DEFAULT, null, IntPtr.Zero);

if (currentReqCount != latestPreviewReqCount)
    Bass.BASS_StreamFree(previewNetStream);
  else
    Bass.BASS_ChannelPlay(previewNetStream, false);
});

And there is still problem with bandwidth usage of many BASS_StreamCreateURL, so it would be nice have some method to directly kill/stop this processes.
« Last Edit: 10 Jan '19 - 16:11 by teq »

Ian @ un4seen

  • Administrator
  • Posts: 21447
Re: How to stop loading BASS_StreamCreateURL ?
« Reply #3 on: 11 Jan '19 - 16:22 »
Can't find this example (

The NETRADIO example can be found in the C\NETRADIO folder. Note I am referring to the BASS package, not the BASS.Net package (which you appear to be using).

Do you mean next approach ?
Code: [Select]

private int latestPreviewReqCount = 0;

...some method

latestPreviewReqCount++;
loadingPackPreviewTrackThread = new Thread(() => {
int currentReqCount = latestPreviewReqCount;
previewNetStream = Bass.BASS_StreamCreateURL(Uri.EscapeUriString($"{CommonSettings.Instance.assetBundlesURL}{model.clipName.ToLower()}.mp3"), 0, BASSFlag.BASS_DEFAULT, null, IntPtr.Zero);

if (currentReqCount != latestPreviewReqCount)
    Bass.BASS_StreamFree(previewNetStream);
  else
    Bass.BASS_ChannelPlay(previewNetStream, false);
});

That is basically the right idea but the counter incrementing should be done in the thread, and the thread needs to use a local variable to receive the stream handle from BASS_StreamCreateURL, which it only copies to the global variable (previewNetStream) if it's still the latest request. Some synchronization is also needed. In essence the NETRADIO example's thread does this:

Code: [Select]
DWORD c, r;
EnterCriticalSection(&lock); // make sure only 1 thread at a time can do the following
r=++req; // increment the request counter for this request
LeaveCriticalSection(&lock);
c=BASS_StreamCreateURL(url, 0, BASS_STREAM_BLOCK|BASS_STREAM_STATUS|BASS_STREAM_AUTOFREE, StatusProc, (void*)r); // open URL
EnterCriticalSection(&lock);
if (r!=req) { // there is a newer request, discard this stream
LeaveCriticalSection(&lock);
if (c) BASS_StreamFree(c);
return;
}
chan=c; // this is now the current stream
LeaveCriticalSection(&lock);
if (chan) BASS_ChannelPlay(chan, FALSE); // play it!

And there is still problem with bandwidth usage of many BASS_StreamCreateURL, so it would be nice have some method to directly kill/stop this processes.

Unfortunately, there isn't currently any way to cancel a BASS_StreamCreateURL call, but disabling pre-buffering via the BASS_CONFIG_NET_PREBUF_WAIT option will minimize extra bandwidth usage.

teq

  • Posts: 29
Re: How to stop loading BASS_StreamCreateURL ?
« Reply #4 on: 13 Jan '19 - 13:47 »
Thanks a lot! Seems that's working fine!