Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Playing MP3 and MIDI Sequences from TMemoryStream 1

Status
Not open for further replies.

Destruktion

Programmer
Mar 18, 2011
28
0
0
GB
Hi, a question for you experts. I have asked in other places but get not answers, this forum seems to be much more helpful so hopefully I can get some help and advice here :)

I have a hobby program Im making (a basic music mixer type program) that allows to add tree nodes at runtime and load a WAV file, the WAV file is saved to a TStream via the Data pointer of the node. I can then play my WAV sound by finding the sound in my stream and assigning it to a temp TMemoryStream, i can then play it, here is an extract:

Code:
procedure TfrmMain.actPlayExecute(Sender: TObject);
var
  Data: TSoundData;
  MemStream: TMemoryStream;
begin
  Data:= TSoundData(tvwMain.Selected);

  if Data <> nil then
  begin
    MemStream:= TMemoryStream.Create;
    try
      Data.Stream.Seek(0, soBeginning);
      Data.Stream.CopyFrom(MemStream, MemStream.Size);
      MemStream.LoadFromStream(Data.Stream);

      sndPlaySound(MemStream.Memory, SND_MEMORY or SND_ASYNC);

      MemStream.Free;
    except on exception do
      MemStream.Free;
    end;
  end;
end;

This works good, I am also saving my stream to file so it can be read at any other time from my app.

Now the question, I want to be able to play other media types as WAV is not as common as say MP3. I would like to play Midi sequences, MP3 and as a bonus other formats, but mainly Midi and MP3.

Now I know I could simply read the stream and save the file to disk, play the file and delete after, but if I can do this directly from a Memory stream it would be much faster and better.

So, How to play MP3 and Midi from TMemoryStream?

Thanks!
 
Thanks whosrdaddy,

I looked at some components but did not want to pay for the packages I saw as I only need small added support to play different files from the memory stream. I did not come across the component site you posted, but they are Free and Open Source.

Will try them out.

Thanks :)
 
Ok I got the components installed and have been trying to figure out how to initialize the streams, there does not seem to be a demo showing how to play from stream.

I added ACS_Classes, ACS_Streams to uses clause but cant seem to put my stream into NewAC stream, like my example above sndPlaySound.

Appreciate some guidance.

PS, in the help i read:

Stream

"Use this property to set the input data stream for the input component. Any TStream descendant may be used as a data source. Note that if you set Stream, you own it, that is you have to create, destroy and position the stream explicitly (the data playback will be started from the current position within the stream). In TAuFileIn descendants the value assigned to this property takes over the FileName property, i. e. if both Stream and FileName properties are assigned, the stream and not the file will be used for the actual input. To unassign this property set it to nil. If the stream is seekable it will be reset to the beginning at the end of the playback."


But i see know way of implementing this?

Thanks
 
What it's saying is that you have to handle all the I/O regarding the stream. Really nothing different than what you already are trying.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
I got no errors using the stream components from the audio package by passing it to my stream, but I got no playback.

I will play around with the components some more when I can.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top