Choppy audio playback from memory

110 views
Skip to first unread message

ratshasa

unread,
Oct 30, 2012, 12:01:31 PM10/30/12
to newac...@googlegroups.com
Hello!

I've coded a project in Delphi 7 in which I'm trying to play an MP3 file of about 2 MB stored as an embedded resource. I can hear the sound playing, but it's very choppy (it plays for a short while, then pauses a bit, then continues playing and so on). What do I have to do to make the sound playback continuous?

Here is the code (the MP3 is embedded with resource ID 1):

procedure Form1.OnFormCreate(Sender: TObject);
var
  ResStream: TResourceStream;
  DXAudioOut: TDXAudioOut;
  MP3In: TMP3In;
begin
  ResStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);

  MP3In := TMP3In.Create(Form1);
  MP3In.Stream := ResStream;

  DXAudioOut := TDXAudioOut.Create(Form1);
  DXAudioOut.Input := MP3In;
  DXAudio.Run;
end;

As I mentioned above, the sound does play, but not continuously. What gives?

ratshasa

unread,
Oct 30, 2012, 5:26:26 PM10/30/12
to newac...@googlegroups.com
Actually, it seems that I can't play any audio file properly. Whether loading an MP3 from a file or from a memory stream, the playback is choppy. If I modify the code above and do:

MP3In.FileName := 'C:\sound.mp3';

And try to play that, it doesn't work properly.

ratshasa

unread,
Oct 31, 2012, 3:27:18 AM10/31/12
to newac...@googlegroups.com
I managed to get it working properly, thanks to the AudioPlayer demo (I only looked at the MP3Player demo before, and that didn't play audio smoothly either). I added a TAudioCache component between the input and output components. So the sample code above now becomes:


procedure Form1.OnFormCreate(Sender: TObject);
var
  ResStream: TResourceStream;
  DXAudioOut: TDXAudioOut;
  MP3In: TMP3In;
  AudioCache: TAudioCache;

begin
  ResStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);

  MP3In := TMP3In.Create(Form1);
  MP3In.Stream := ResStream;

  AudioCache := TAudioCache.Create(Form1);
  AudioCache.Input := MP3In;

  DXAudioOut := TDXAudioOut.Create(Form1);
  DXAudioOut.Input := AudioCache;
  DXAudioOut.Run;
end;

Ross Levis

unread,
Oct 31, 2012, 5:28:50 AM10/31/12
to newac...@googlegroups.com

You could have likely also increased the directsound buffer size to give you a similar result.

 

Ross.

--
You are recieving this message because you are subscribed to "NewAC users" Google group.
To post message to this group send it to
newac...@googlegroups.com
To unsubscribe from the group send a mail to: newac-users...@googlegroups.com
Additional variants are available at the groups page http://groups.google.com/group/newac-users?hl=en

Reply all
Reply to author
Forward
0 new messages