Mega Code Archive

 
Categories / Delphi / Multimedia
 

Build an MP3 Player

Title: Build an MP3 Player Question: The mciSendString function sends a command string to an MCI device. The device that the command is sent to is specified in the command string; but how to map the function ? Answer: The operations are found in Winmm.lib and as a DLL in Winmm.dll using a simple wav you call also the winmm.dll //function PlaySound(s: pchar; flag, syncflag: integer): boolean; //external 'PlaySound@winmm.dll stdcall'; But we want to play mp3 with the MCI. The Media Control Interface (MCI) provides standard commands for playing multimedia devices and recording multimedia resource files. These commands are a generic interface to nearly every kind of multimedia device. I map at first 3 functions and 1 procedure to call the mciSendString (say it a command string): procedure playMP3(mp3path: string); begin mciSendString(PChar('open "' + mp3path + '" alias MP3'), NIL, 0, 0); //player.handle mciSendString('play MP3', NIL, 0, 0); //mciSendString('close MP3', 0,0,0); end; procedure stopMP3; begin mciSendString('stop MP3', 0,0,0); end; procedure closeMP3; begin mciSendString('close MP3', 0,0,0); end; function lengthmp3(mp3path: string):integer; var nlength: string; begin mciSendString(PChar('open "' + mp3path + '" alias MP3'), NIL, 0, 0); setlength(nlength, 255); mciSendString('status MP3 length', pchar(nlength), length(nlength), 0); result:= StrToInt(nlength); end; The first parameter is always a pointer to a null-terminated string that specifies an MCI command string. For a list, see Multimedia Command Strings at: http://msdn.microsoft.com/en-us/library/dd743572(v=VS.85).aspx Next I put some code around the mapped procedures: for i:= 0 to lstbox.items.count - 1 do begin mp3time:= lengthmp3(lstbox.items[i]) writeln(Format('%s',[lstbox.items[i]+MSecToTime(mp3time)])); LetLogoDraw(inFrm.Canvas, 10, 5, 3, clpurple); playmp3(lstbox.items[i]) sleep(mp3time) closemp3; end; The whole player is found at: http://www.softwareschule.ch/download/88_pas_soundbox2_mp3.txt the GUI of the Player in the box: http://www.softwareschule.ch/download/maxboxgui29.png its a script which runs in maXbox but the concept is ready to run in Delphi or FreePascal too. http://sourceforge.net/projects/maxbox/ by the way: mp3 and shuffle is close behind, the best shuffle code is from Don Knuth (elegant and efficient): procedure Shuffle(vQ: TStringList); var j, k: integer; tmp: String; begin randomize; for j:= vQ.count -1 downto 0 do begin k:= Random(j+1); tmp:= (vQ[j]); vQ[j]:= vQ[k]; vQ[k]:= tmp; end; end;