Play audio from memory stream

Started by Hanuman,

Hanuman

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

so far I see managed Bass has 2 Overloads of
CreateStream
public static int CreateStream(string File, long Offset = 0, long Length = 0, BassFlags Flags = BassFlags.Default)
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

#2
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.

radio42

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

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

Quote from: Ian @ un4seenit'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.

Quote from: Ian @ un4seenStreaming 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

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