Link to home
Start Free TrialLog in
Avatar of vitamingoo
vitamingoo

asked on

How to play midi from application's resource?

I want to play the midi music from my application's resource directly , like some keygen program , you open it  , it will play some midi music as background music . I don't want to extract the music from resource and play it directly .
Avatar of kyrlean
kyrlean

If i understood you question, which i think so.. you  just need to do this on your Form's on create event..
PlaySound(PChar(1),HInstance, snd_ASync or snd_Memory or snd_Resource);
# 1 refers to the first resource on your project since you can have as many as you want !
Avatar of vitamingoo

ASKER

I test your method . it's not successfully.But if the resource file is a wav file, it's successfully.
Can you give me some code.
Well.. i'm  sorry . I'm not at home so i can't do it right now, but i will as sooner as i get a chance. Can you tell me how did you bild your .res file? Was it an mp3 ?
If it's an MP3 you can follow the link bellow. There's  a subtopic called Palying MP3s and then you'll get a full explanation.Following such example you have to add a Tmediaplayer component and a lot more need to be done. For my own use i would prefer to convert the mp3 (if it's really the case)to a wav file and use the code i shown you previously .

http://delphi.about.com/od/objectpascalide/l/aa021301c.htm
It's a midi(*.mid) file.

And thank you.
Here is a procedure .

procedure ExtractRes( const ResType, ResName, ExtractedResFileName : String);
var Resur:TResourceStream;
begin
 Resur := TResourceStream.Create( hInstance, ResName, pCHAR(ResType) );
 try
  Resur.SaveToFile(ExtractedResFileName);
 finally
 Resur.Free;
 end;
end;

To call it you'll use ..
//This will extract the .mid file to disk....

ExtractRes( 'MIDI', 'MyMidiRes', 'C:\myfavmidifile.mid');

//and before your application closes, you could use the following to delete the extracted file.

DeleteFile ( ' C:\myfavmidifile.mid ' );

Hope it helps !!!
Thank you . Like the content I mentioned in my question , I know it works if the mid file was extract to disk , but I don't want to do like this , I just want to play the mid music in memory or resourcestream .

I searched in Google , but I can't find the answer .
I understand you don't want to create a file on the computer your app is running that's why i suggest you the delete function ( besides the file conversion ) since it's  not gonna be there when your app closes  but, i'll give it one more try and let you  know !
Thanks !
You must have used some keygen program , in these program , open it , it will play some midi background music , and it don't extract any file to disk .
ASKER CERTIFIED SOLUTION
Avatar of kyrlean
kyrlean

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for accept my answer/suggestion vitamingoo.Coincidence or not i just heard from that friend last night and he told me about a very complicated method someone else used to hold a file(resource) in the memory without it touch the hard disk.But i assume that, it was out of my capability and i could not pass it to you. Thanks again !!