Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: playing mp3 files directly from the .exe

  1. #1

    playing mp3 files directly from the .exe

    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/objectpas.../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
    The Universe is all right here!!!

  2. #2

    playing mp3 files directly from the .exe

    You could use LoadResource to load the MP3 into memory then play it from memory.

  3. #3

    playing mp3 files directly from the .exe

    Quote Originally Posted by Sly
    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
    The Universe is all right here!!!

  4. #4

    playing mp3 files directly from the .exe

    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.

  5. #5

    playing mp3 files directly from the .exe

    Quote Originally Posted by Sly
    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
    The Universe is all right here!!!

  6. #6

    playing mp3 files directly from the .exe

    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).

  7. #7

    playing mp3 files directly from the .exe

    Quote Originally Posted by Sly
    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/objectpas.../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
    The Universe is all right here!!!

  8. #8
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    playing mp3 files directly from the .exe

    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):

    [pascal]
    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;
    [/pascal]

    it uses the mmsystem unit.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  9. #9

    playing mp3 files directly from the .exe

    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
    The Universe is all right here!!!

  10. #10
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    playing mp3 files directly from the .exe

    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
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •