PDA

View Full Version : playing mp3 files directly from the .exe



atozix
05-05-2005, 10:32 PM
Hi all...:)

Now then = I want to be able to play an mp3 file directly from within the exe file with Delphi5....
WITHOUT creating the mp3 file on the users machine...

There is an exccellent article at
http://delphi.about.com/od/objectpascalide/l/aa021301c.htm
by Zarko Gajic
FROM THE ARTICLE
"You can store more that just code in the application executable file. An exe file can contain a WAV, AVI or even an MP3 file. The "trick" is in working with resources."

HOWEVER "One minor problem is that the application creates a mp3 song on a user machine. You could add a code that deletes that file before the application is terminated. "

------
another method is to put a wave file in an .rc file thus
MYSOUND WAVE "sound.wav"
create the res file as usual and then play the sound directly from the exe using

PlaySound(PChar('MYSOUND'), hInstance, snd_ASync or snd_Resource);

note = add MMSystem to uses to use PlaySound
This does not create another copy on users machine

Could this method be adopted to play an mp3 ???????

Any ideas and help welcomed..... but I want it simple without adding components etc....

cheeeeeerrrrrrrrrsssssssss atozix

Sly
05-05-2005, 11:11 PM
You could use LoadResource to load the MP3 into memory then play it from memory.

atozix
06-05-2005, 02:42 AM
You could use LoadResource to load the MP3 into memory then play it from memory.

Hi Sly,,,

hmmmm = sounds good = BUT how exactly do I do that ?????

cheeeerrrrrrsssssss ato

Sly
06-05-2005, 03:35 AM
Libraries such as FMOD (http://www.fmod.org/) can play MP3s directly from memory. So use the LoadResource() Win32 API function to load the resource into memory and then use FMOD (or other similar MP3 playback library) to play the MP3 directly from memory.

atozix
06-05-2005, 06:51 AM
Libraries such as FMOD (http://www.fmod.org/) can play MP3s directly from memory. So use the LoadResource() Win32 API function to load the resource into memory and then use FMOD (or other similar MP3 playback library) to play the MP3 directly from memory.

hmmmm = yes but i dont want to use a library etc = just want to write the code and keep it simple...

cheeerrrrrrrssssss a

Sly
06-05-2005, 06:59 AM
You've got some options. You could write your own code to play back MP3 files (very difficult), use DirectShow codecs to play back MP3 files (slightly less difficult), or use a library such as FMOD (easy).

atozix
06-05-2005, 09:45 AM
You've got some options. You could write your own code to play back MP3 files (very difficult), use DirectShow codecs to play back MP3 files (slightly less difficult), or use a library such as FMOD (easy).

All I need is a slight modification to what I already have ===

OK I'll get specific = here is some code that works well
taken from

http://delphi.about.com/od/objectpascalide/l/aa021301c.htm
by Zarko Gajic
= thanks Zarko...
I have written to Zarko with no response = so this post...
================================

of course = first the .rc file and res file are created.... then =

{$R AboutDelphi.RES}

procedure TForm1.Button1Click(Sender: TObject);
var
rStream: TResourceStream;
fStream: TFileStream;
fname: string;
begin
{this part extracts the mp3 from exe}
fname:=ExtractFileDir(Paramstr(0))+'Intro.mp3';
rStream := TResourceStream.Create
(hInstance, 'Intro', RT_RCDATA);
try
fStream := TFileStream.Create(fname, fmCreate);
try
fStream.CopyFrom(rStream, 0);
finally
fStream.Free;
end;
finally
rStream.Free;
end;

{this part plays the mp3}
MediaPlayer1.Close;
MediaPlayer1.FileName:=fname;
MediaPlayer1.Open;
end;

now this uses the resource file that contains an mp3
and plays it with mediaplayer after creating the mp3 on your harddrive..

But can one play it directly from the resource without creating a file on the hd first ????

I actually think it's quite neat that ye olde mediaplayer will play mp3's if given a suitable filename....

I feel there must be an easy elegant solution but I don't know anything about streaming to mediaplayer from the exe...

I feel that the inherent simplicity of the above from Zarko just needs a polishing touch and it would be really useful to a lot of programmers that want to embed and play mp3's from the exe.

cheeeeeerrrrrrrrsssssss a

cairnswm
06-05-2005, 11:53 AM
I found this code useful to play WAV files that were loaded into memory. I have no idea if it will work with MP3s (I dont have a single MP3 that I know of):


procedure LoadWav(const Filename: string; var Ptr: pointer);
var
Strm: TFileStream;
Size: integer;
begin
Strm := TFileStream.Create(Filename, fmOpenRead);
GetMem(Ptr, Strm.Size);
Strm.ReadBuffer(Ptr^, Strm.Size);
Strm.Free;
end;

Procedure PlayTheSound(Ptr : Pointer);
Begin
mmSystem.PlaySound(PChar(Ptr), 0, SND_MEMORY or SND_NODEFAULT or SND_ASYNC);
End;


it uses the mmsystem unit.

atozix
06-05-2005, 12:16 PM
Hi William...

thx = I already have that method = I really want the mediaplayer method altered as stated... as except 4 the extra file created it is ideal..

cheeeeerrrrrrrrsssssss a

cairnswm
06-05-2005, 08:56 PM
Not sure it is possible. I looked at the MediaPlayer component and it has no support for streams :(

You'd need to go down to lower level API calls to the media player to achieve it, if it is possible.

Sorry :(

Paulius
06-05-2005, 09:41 PM
you can't play anything from memory with windows API except wav's, so no MediaPlayer won't help here. fmod is quite simple, try it.

atozix
07-05-2005, 12:08 AM
Hi Paulius..

hmmm = i will try fmod but a thought struck me = a bit long winded but would it work?? =

if one created a virtual drive ,then copied the mp3 to that, as an mp3 file using the above method .... then played it from that file with mediaplayer.... On termination of program destroy the virtual drive...

However
FROM THE ARTICLE
by Zarko Gajic

"You can store more that just code in the application executable file. An exe file can contain a WAV, AVI or even an MP3 file. The "trick" is in working with resources."

HOWEVER "One minor problem is that the application creates a mp3 song on a user machine. You could add a code that deletes that file before the application is terminated. "


THIS seems to be the easiest option... So I will probably settle for that ...

cheeeeeerrrrrrrsssss a

fragle
05-11-2005, 08:15 PM
Umm... I might be reviving an old thread but... but it was on the first page! ;)

Anyway, looks like most people forget that *.wav format file can hold more than only uncompressed data. And this combined with the fact, that acm mp3 codec is with us since Win95 times, allows us to create an mp3 resource player with a line of code!
mmSystem.PlaySound(PCHAR(ResourceID), hInstance, SND_RESOURCE);Just don't forget to add a RIFF header to the mp3 before adding it to the application.

Sure it's hacky and inflexible like hell, but gets the job done really fast..

Hope this helps :)