Author Topic: Play audio from memory stream  (Read 223 times)

Hanuman

  • Posts: 113
Play audio from memory stream
« on: 21 Apr '24 - 23:02 »
With MediaPlayerUI.Bass, someone is asking how to play a file from a memory stream (embedded resource).

I thought it would be very trivial. I can load a Stream from the file and create BASS from the Stream as separate steps.

Looking at ManagedBass.Bass.CreateStream though, it doesn't have a Stream overload. It takes a file, URL, byte array, or IntPtr pointer. I don't want to load the entire file into memory either, nor manage buffers manually.

What's the right way to play an audio from a memory stream, such as for a resource embedded into the EXE?

Chris

  • Posts: 2203
Re: Play audio from memory stream
« Reply #1 on: 22 Apr '24 - 12:05 »
so far I see managed Bass has 2 Overloads of
CreateStream
Code: [Select]
public static int CreateStream(string File, long Offset = 0, long Length = 0, BassFlags Flags = BassFlags.Default)
Code: [Select]
public static int CreateStream(IntPtr Memory, long Offset, long Length, BassFlags Flags = BassFlags.Default)
the second one is to create a stream   from a memory stream.

Hanuman

  • Posts: 113
Re: Play audio from memory stream
« Reply #2 on: 22 Apr '24 - 13:05 »
There doesn't seem to be any easy way to convert a .NET Stream object into a IntPtr.

That overload seems more as a pointer to a byte array.

A .NET Stream actually needs to read the file chunk by chunk into a buffer.
« Last Edit: 22 Apr '24 - 13:20 by Hanuman »

radio42

  • Posts: 4837
Re: Play audio from memory stream
« Reply #3 on: 22 Apr '24 - 15:33 »
An unmanaged C IntPtr is an address of a memory location.
Whereas a .Net Stream is an abstract class (like a concept or interface) for any kind of data stream. I.e. an abstract description of sequences of bytes...

As such,the two are not related and you can not convert the one into the other.

Ian @ un4seen

  • Administrator
  • Posts: 25780
Re: Play audio from memory stream
« Reply #4 on: 22 Apr '24 - 17:09 »
Streaming a file from somewhere other than the local filesystem or a URL would usually require the use of BASS_StreamCreateFileUser (and callbacks), but it's also usually possible to play a resource in an EXE/DLL directly from memory with BASS_StreamCreateFile instead. The native functions of interest are FindResource/LoadResource/SizeofResource. If .Net doesn't have its own equivalent, perhaps you can call them directly? There's some code in this thread:

   www.un4seen.com/forum/?topic=12731

Hanuman

  • Posts: 113
Re: Play audio from memory stream
« Reply #5 on: 23 Apr '24 - 21:00 »
it's also usually possible to play a resource in an EXE/DLL directly from memory with BASS_StreamCreateFile instead. The native functions of interest are FindResource/LoadResource/SizeofResource. If .Net doesn't have its own equivalent, perhaps you can call them directly? There's some code in this thread:
I could if it was for my own needs; but this is for a generic 3rd party control. I don't know where their Stream is going to come from. It could very well be an internet audio stream.

Streaming a file from somewhere other than the local filesystem or a URL would usually require the use of BASS_StreamCreateFileUser (and callbacks)
So this would be the only solution? This is not looking simple at all. Especially considering the fact that the method is not exposed by ManagedBass.

Ian @ un4seen

  • Administrator
  • Posts: 25780
Re: Play audio from memory stream
« Reply #6 on: 24 Apr '24 - 13:35 »
ManagedBass appears to include support for BASS_StreamCreateFileUser via a CreateStream overload that takes a "FileProcedures" class (containing the callbacks).

   https://github.com/ManagedBass/ManagedBass/blob/5856a8baf538e7d91fa0b4ce6ba5de45d91bb016/src/Bass/Shared/Bass/PInvoke/CreateStream.cs#L264