I am in the process of writing a music application to replace a manual coin operated jukebox, but I have a few problems with the mediaplayer component and some other things. I have tried other components like Bass etc, but it gives me a access violation error everytime I try yo use the component. I would also like to have a kind of VU meter if possible with mediaplayer.
1.How do I stop the current song after 5 minutes or after a specified time and start with the next song on the playlist ?
2.How can I display the track number, title and total song length in my playlist 1?
3.I must communicate with the serial port to turn volume up/down, how do I read from ceratin pins? Eg: pin 4&5 turns up and 1&2 turns down?
4.Use a image for my selection in the listbox? I want to use just an arrow and not the whole blue selection bar?
I am including the source code of what I have at the moment.
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, MPlayer, HTMListB, MMSystem;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
MP: TMediaPlayer;
PlayList1: THTMListBox;
PlayList2: TListBox;
CreditLabel: TLabel;
Timer1: TTimer;
ProgressTimer: TTimer;
Label1: TLabel;
TimeLabel: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure PlayList1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ProgressTimerTimer(Sender: TObject);
private
{ Private declarations }
MusicDir : String;
DataDir : String;
TotalDir : Integer;
CDNr : Integer;
CurrentCredit : Integer;
public
{ Public declarations }
procedure PlayMusic;
procedure NextSong;
end;
var
Form1: TForm1;
Result : String;
implementation
{$R *.dfm}
function Prog_Dir: String;
Var
x,y : String;
begin
x := ParamStr(0);
y := ExtractFileName(ParamStr(0));
Result := copy(x, 0, length(x) - length(y));
end;
function CountFolders(dir: String): Integer;
var
sr: TSearchRec;
iFound: Integer;
begin
Result := 0-1; //Subtracts the . and .. directory
iFound := FindFirst(dir+'*.*', faDirectory, sr);
while iFound = 0 do
begin
if (sr.Attr and faDirectory > 0) then
Inc(Result);
iFound := FindNext(sr);
end
end;
//Search for mp3 files
procedure SearchMP3(dir : string);
var
sr : TSearchRec;
result : integer;
TmpString : String;
begin
result := findFirst(dir+'*.mp3',faAnyfile,sr);
while result = 0 do
begin
if (sr.name <> '.')and(sr.name <> '..')then
begin
if (sr.attr and faDirectory > 0) then
SearchMP3(dir+sr.name+'\');
TmpString := ChangeFileExt(ExtractFilename(sr.Name),'');
Form1.PlayList1.Items.Add(TmpString);
end;
result := findNext(sr);
end;
findClose(sr);
end;
procedure TForm1.PlayMusic;
Var
MpFileName : String;
begin
MpFileName := Playlist2.Items[PlayList2.ItemIndex];
Label1.Caption := MPFilename;
If MP.Mode = mpStopped then
begin
If PlayList2.ItemIndex < PlayList2.Items.Count -1 then
begin
PlayList2.Items.Delete(0);
PlayList2.ItemIndex := 0;
end
else
//exit;
Playlist2.Clear;
end;
If MP.Mode = mpPlaying then Exit;
MP.FileName := MPFilename;
MP.Close;
MP.Open;
MP.Play;
MP.Notify := True;
ProgressTimer.Enabled := True;
end;
procedure TForm1.NextSong;
begin
// If PlayList2.ItemIndex < PlayList2.Items.Count - 1 then
// begin
// PlayList2.Items.Delete(0);
// PlayList2.ItemIndex := 0;
// end;
PlayMusic;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//Maximize Screen
Form1.Left := (Screen.Width div 2) - (Screen.Width div 2);
Form1.Top := (Screen.Height div 2) - (Screen.Height div 2);
BorderStyle := bsNone;
WindowState := wsmaximized;
ClientWidth := Screen.Width;
ClientHeight := Screen.Height;
Refresh;
SetForegroundWindow(Handle);
SetActiveWindow(Application.Handle);
TotalDir := CountFolders(MusicDir);
//Adds files to playlist
CDNr := 1;
MusicDir := Prog_Dir+'Music\';
// Label1.Caption := MusicDir;
SearchMP3(MusicDir+'CD'+IntToStr(CDNr)+'\');
PlayList1.ItemIndex := 0;
end;
procedure TForm1.PlayList1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
Var
CurrentDir : String;
begin
If Key = VK_Escape then
begin
CurrentCredit := CurrentCredit + 1;
CreditLabel.Caption := IntToStr(CurrentCredit);
end;
If Key = VK_Return then
//Checks to see if there is enough credit
If CurrentCredit = 0 then
begin
// CoinLabel.Visible := True;
// CoinLabel.BehaviorOptions.Active := True;
end
else
If CurrentCredit > 0 then
begin
CurrentCredit := CurrentCredit -1;
CreditLabel.Caption := IntToStr(CurrentCredit);
PlayList2.Items.Add(MusicDir+'CD'+IntToStr(CDNr)+'\'+PlayList1.Items[PlayList1.ItemIndex]+'.mp3');
PlayList2.ItemIndex := 0;
If PlayList2.Items.Count = 0 then Playlist2.Clear;
PlayMusic;
end;
If Key = VK_Right then
If CDNr <= TotalDir -1 then
begin
PlayList1.Items.Clear;
CDNr := CDNr + 1;
SearchMP3(MusicDir+'CD'+IntTostr(cdnr)+'\');
CurrentDir := MusicDir+'CD'+IntTostr(cdnr)+'\';
// CoverImageFile := ExtractFilePath(Application.Exename)+'Music\CD'+IntToStr(CDNr)+'\'+'cover.jpg';
// If FileExists(CoverImageFile) then CoverImage.Picture.LoadFromFile(CoverImageFile);
end;
If Key = VK_Left then
If CDNr > 1 then
begin
Playlist1.Items.Clear;
CDNr := CDNr -1;
SearchMP3(MusicDir+'CD'+IntTostr(cdnr)+'\');
// CoverImageFile := ExtractFilePath(Application.Exename)+'Music\CD'+IntToStr(CDNr)+'\'+'cover.jpg';
// If FileExists(CoverImageFile) then CoverImage.Picture.LoadFromFile(CoverImageFile);
CurrentDir := MusicDir+'CD'+IntTostr(cdnr)+'\';
end;
end;
procedure TForm1.ProgressTimerTimer(Sender: TObject);
Var
t, min, sec : integer; //minutes, seconds in calculations!!
// s1, s2, result : string; // minutes(s1), seconds(s2) when displayed as result
s1, s2 : string; // minutes(s1), seconds(s2) when displayed as result
tmp : string;
begin
t:=MP.Position;
// t := MP.Length - MP.Position;
sec := t div 1000;
min := sec div 60;
sec := (sec - min * 60);
if min >9 then s1 := intToStr(min)
else s1 := '0' + intToStr(min);
if sec >9 then s2 := intToStr(sec)
else s2 := '0' + intToStr(sec);
result := S1 + ':' + S2;
TimeLabel.Caption := Result;
If MP.Mode = mpStopped then NextSong;
end;
end.