[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

CD Track List

How can I provide a tracklist for audio CD's? I'm fairly new to this and have tried several ways to no avail.
0
xpher
Asked:
xpher
  • 17
  • 11
  • 5
  • +1
1 Solution
 
interCommented:
You can retreive the track list and their positions as follows;

Say we have a list box called listbox1, and mediaplayer1

procedure TForm1.ShowTracksButtonClicked(Sender:TObject);
var
  i : integer;
  M : TMediaPlayer;
  T : Longint;
begin
 M := TMediaPlayer1;
 with M do
   if DeviceType = dtCDAudio then
   begin
     TimeFormat := tfTMSF;
      for i:=0 to M.Tracks - 1 do
      begin
          T :=  M.TrackPosition[i];
          ListBox1.Items.Add(Format('Track %d = %02d:%02d:%02d',
             [mci_TMSF_Hour(T),mci_TMSF_Minute(T),mci_TMSF_Second(T)]);
      end;
   end;
end;

regards, igor
0
 
interCommented:
Sorry change
  ListBox1.Items.Add(Format('Track %d = %02d:%02d:%02d',
                 [mci_TMSF_Hour(T),mci_TMSF_Minute(T),mci_TMSF_Second(T)]);
to
 ListBox1.Items.Add(Format('Track %d = %02d:%02d:%02d',
                 [i, mci_TMSF_Hour(T),mci_TMSF_Minute(T),mci_TMSF_Second(T)]);
0
 
xpherAuthor Commented:
Thanks for the response inter but I can't seem to get it to work.
I am using Delphi 2, does this make a difference?

All I need to stop tearing my hair out is a track list that I can
use to choose which track to play. (Or maybe I'll just give up this Delphi thing!)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
xpherAuthor Commented:
Since my last comment I have looked at it again, but it still doesn,t provide a clean solution. Had to change the Hour from TMSF to HMS. Times are wrong for tracks and it creates a 'Track00'.
Still can't get it to play from a selected track from the list.
Here's hoping.
0
 
viktornetCommented:
You need to drop a TMediaPlayer component on your form B4 you try to run this code... And this code needs to be assigned to an OnClick() of a button, and you also need to have a listbox on your form...

Regards,
Viktor Ivanov
0
 
xpherAuthor Commented:
Thanks viktornet but i've done that. How do I click on a track in the listbox and get it to play?
0
 
interCommented:
Ok then,
If you can able to get the above work(i.e. you can see the tracks and its positions in the list do the following:
NOTE: Clear listbox before calling the routine that fills listbox:

// this one seeks to specified track of the media player M provided
// that an audio CD is loaded
procedure SeekToTrack(M : TMediaPlayer; Trac, Minute, Second: Byte);
begin
  with M do
   if DeviceType = dtCDAudio then
   begin
     // we set the time to Track Minute Second Frame Format
     TimeFormat := tfTMSF;
     // This macro makes up a position from vars Frame is 0
     // for CDs
     Position := mci_Make_TMSF(Trac, Minute, Second,0);
   end;
end;

procedure TForm1.ListBox1Clicked(Sender:TObject);
begin
   MediaPlayer1.Stop;
   if ListBox1.ItemIndex <> -1 then
   begin
      // seek the begining of the track
      SeekToTrack(MediaPlayer1, ListBox1.ItemIndex, 0, 0);
      MediaPlayer1.Play;
   end;
end;

may this works, igor
0
 
xpherAuthor Commented:
Thanks for the help igor, but I think I might give it up as a hobby.
1) The tracklist (ListBox) starts at Track00 (not Track01).
   Track00 time is total CD time. It also plays the same track as Track01. This then means that if I have a CD with 16 tracks it lists from Track00 to Track15, I lose the last track altogether.
 
2) The times next to track are not times i.e they display like
             Track01=01:00:00;
by removing Hour it becomes i.e. Track01=00:00

I've tried various alterations and looked in books but no joy.

Many thanks
Chris.
0
 
viktornetCommented:
Hello Chris.... Just try changing this line->

ListBox1.Items.Add(Format('Track %d = %02d:%02d:%02d',
  [i, mci_TMSF_Hour(T),mci_TMSF_Minute(T),mci_TMSF_Second(T)]);

to this one->

ListBox1.Items.Add(Format('Track %d = %02d:%02d:%02d',
  [i+1, mci_TMSF_Hour(T),mci_TMSF_Minute(T),mci_TMSF_Second(T)]);

Hope this helps you get the tracks right :-)

Regards,
Viktor Ivanov



0
 
xpherAuthor Commented:
Thanks for the help Viktor but I've tried adding +1 here and yes the list starts at Track01 but the problem still persists.
This is the way the list appears:
   Track01=50:66 (This plays the whole CD!, Time depends onCD)
        Track02=00:00 (This is the first track on CD)
            and so on.

This is my code as it stands, it also includes for labels etc.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, MPlayer, ExtCtrls, MMSystem, Buttons;

type
  TForm1 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    ListBox1: TListBox;
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    SpeedButton1: TSpeedButton;
    procedure SeekToTrack(M : TMediaPlayer; Trac, Minute, Second: Byte);
  procedure ListBox1Clicked(Sender:TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure ListBox1Enter(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.SeekToTrack(M : TMediaPlayer; Trac, Minute, Second: Byte);
begin
  with M do
   if DeviceType = dtCDAudio then
   begin
     TimeFormat := tfTMSF;
     Position := mci_Make_TMSF(Trac, Minute, Second,0);
   end;
end;

procedure TForm1.ListBox1Clicked(Sender:TObject);
begin
   MediaPlayer1.Stop;
   if ListBox1.ItemIndex <> -1 then
   begin
      SeekToTrack(MediaPlayer1, ListBox1.ItemIndex, 0, 0);
      MediaPlayer1.Play;
   end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
   Track, Minute, Second: byte;
begin
with MediaPlayer1 do
begin
Track := mci_TMSF_Track(Position);
Minute := mci_TMSF_Minute(Position);
Second := mci_TMSF_Second(Position);
Label1.Caption := Format('%.2d',[Track]);
Label2.Caption := Format('%.2d:%.2d',[Minute, Second]);
end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
 MediaPlayer1.Stop;
   if ListBox1.ItemIndex <> -1 then
   begin
      SeekToTrack(MediaPlayer1, ListBox1.ItemIndex, 0, 0);
      MediaPlayer1.Play;
   end;
end;

procedure TForm1.ListBox1Enter(Sender: TObject);
var
   i: integer;
   M: TMediaPlayer;
   T: Longint;
begin
ListBox1.Clear;
     M := MediaPlayer1;
     with M do
          if DeviceType = dtCDAudio then
     begin
          TimeFormat := tfTMSF;
          for i:=0 to M.Tracks -1 do
     begin
     T := M.TrackPosition[i];
     ListBox1.Items.Add(Format('Track %.2d = %.2d:%.2d',
                    [i+1,mci_TMSF_Minute(T),mci_TMSF_Second(T)]));
     end;
     end;
     end;

end.

Thanks again (and you igor)
Chris
0
 
viktornetCommented:
Ok, it seems like the tracks start from 1 to TracksCount and not from 0 to TracksCount-1 .... I'm not sure but it's worth trying.... Here is what you should try changing....

Find this code->

for i:=0 to M.Tracks -1 do begin
  T := M.TrackPosition[i];
  ListBox1.Items.Add(Format('Track %.2d = %.2d:%.2d',
    [i+1,mci_TMSF_Minute(T),mci_TMSF_Second(T)]));
end;

and change to this one...

for i:=1 to M.Tracks do begin
  T := M.TrackPosition[i];
  ListBox1.Items.Add(Format('Track %.2d = %.2d:%.2d',
    [i,mci_TMSF_Minute(T),mci_TMSF_Second(T)]));
end;

I hope it works if not I'll take a closer look at the code, or maybe Igor could tell you what eaxctly is the problem when he comes back, if mine doesn't work ;->
0
 
xpherAuthor Commented:
i've tried this and similar but it just creates alist starting from Track01 (which plays the whole CD), Track02on the list is actually track1 on the CD and I am a track short at the end.

I've been trying to experiment with the idea of hiding the listbox and copying it into a combo box without copying the track00. not much success here either so far. i know it seems like a cheat but if it would work so what.

Thanks again
Chris
0
 
viktornetCommented:
Ok, then here is what you do......Use the old code just change a little bit...
Use this instead of the one I gave you b4

for i := 0 to M.Tracks -1 do begin
       T := M.TrackPosition[i];
       ListBox1.Items.Add(Format('Track %.2d = %.2d:%.2d',
         [i+1,mci_TMSF_Minute(T),mci_TMSF_Second(T)]));
      ListBox1.Items.Delete(0);//add this to your code..
     end;

That's all...I hope it works this time...

Regards,
Viktor Ivanov
0
 
xpherAuthor Commented:
Unfortunately this just clears the ListBox.

Thanks anyway
Chris
0
 
xpherAuthor Commented:
Unfortunately this just clears the ListBox.

Thanks anyway
Chris
0
 
viktornetCommented:
Yeah, it deletes the first item that would be the whole time for the CD, so if it is cleared after you got all tracks everything is got to work fine....

btw- Please tell me what's wrong now...

Viktor
0
 
xpherAuthor Commented:
No it deletes everything in the ListBox.
Or am I doing something wrong?

Cheers
Chris
0
 
xpherAuthor Commented:
have just been trying MediaPlayer1.Start. I can get it to show 1 in an edit box is there any way to transfer this idea?
I used the line
Edit1.Text := IntToStr(MediaPlayer1.Start);

Here's hoping

Chris
0
 
viktornetCommented:
Just tried

ListBox1.Items.Delete(0);

and it deletes only the first lines as I told you! I don't know why it deletes the whole listbox after you aply this methdo . o O

btw- What do you want to transfer from the Edit1 control??? Please be specific????
0
 
xpherAuthor Commented:
I was only using Edit1 to play around with MediaPlayer.Start.
Also I have tried again with delete and it still clears the list box. I'm going to start over again and then see.

Thanks for help
Chris
0
 
viktornetCommented:
Please send all source code you got so far, and the events you have assigned it to 10x

Viktor
0
 
xpherAuthor Commented:
Here is the code:

{Using Delphi 2
       Decided to use a ComboBox instead of ListBox. CombBox named Tracklist.
       Problem is that track00 shows in list. The code as it stands presents
       all playable tracks + track00}
{****************************************************************************}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  MMSystem, MPlayer, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Tracklist: TComboBox;
    MediaPlayer1: TMediaPlayer;
    procedure TracklistEnter(Sender: TObject);
    procedure SeekToTrack(M : TMediaPlayer; Trac, Minute, Second: Byte);
    procedure TracklistClick(Sender: TObject);
    procedure MediaPlayer1Click(Sender: TObject; Button: TMPBtnType;
      var DoDefault: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.TracklistEnter(Sender: TObject);
var
  i : integer;
  M : TMediaPlayer;
  T : Longint;
begin
Tracklist.Clear;
 M := MediaPlayer1;
 with M do
   if DeviceType = dtCDAudio then
   begin
     TimeFormat := tfTMSF;
      for i:=0 to M.Tracks  do
      begin
          T :=  M.TrackPosition[i];
          Tracklist.Items.Add(Format('Track %.2d = %.2d:%.2d',
         [i,mci_TMSF_Minute(T),mci_TMSF_Second(T)]));
         {show track 01 in combo box}
         Tracklist.ItemIndex:=1;
         {Tracklist.Items.Delete(0); --- The offending line}
      end;
   end;
end;


procedure TForm1.SeekToTrack(M : TMediaPlayer; Trac, Minute, Second: Byte);
begin
  with M do
   if DeviceType = dtCDAudio then
   begin
     // we set the time to Track Minute Second Frame Format
     TimeFormat := tfTMSF;
     // This macro makes up a position from vars Frame is 0
     // for CDs
     Position := mci_Make_TMSF(Trac, Minute, Second,0);
   end;
end;

procedure TForm1.TracklistClick(Sender: TObject);
begin
   MediaPlayer1.Stop;
   if Tracklist.ItemIndex <> -1 then
   begin
      // seek the begining of the track
      SeekToTrack(MediaPlayer1, Tracklist.ItemIndex, 0, 0);
   end;
end;

end.

I really appreciate the help, thanks.
Chris
0
 
viktornetCommented:
Please send me the rest of the code where you load the music from the CD! 10x

Regards,
Viktor Ivanov
0
 
xpherAuthor Commented:
Don't understand?

The MediaPlayer1 is set for AutoOpen=True in Object Inspector, and for CDAudio.

The code given above is all I am using at the moment as it is pointless doing anymore with this idea unless I can find a way to get rid of Track00.

So when an audio CD is placed in the drive the MediaPlayer plays it.

Regards
Chris
0
 
viktornetCommented:
I don't know but I did that myself and it shows me an error message... I'll check it with more care after I get back from school .. Talk to you later

Regards,
Viktor Ivanov
0
 
xpherAuthor Commented:
Viktor

If you wish I can put together what I,ve got and create a file for you to download including project file?

Regards
Chris
0
 
viktornetCommented:
Yeah that would be great! For my e-mail take a look at my profile. It's there... :-)

btw- Sorry it takes so long but I've never worked with TMediaPlayer or music stuff at all in Delphi... I'll try to do my best though

Regards,
Viktor Ivanov
0
 
BlackManCommented:
There is an example CD player with full Delphi source at Torry's Delphi Pages, perhaps you can get a hint from it?
Check http://www.torry.ru/samples/samples/abcdplay.zip
0
 
xpherAuthor Commented:
DOn't apologise Viktor you've been a great help. Ill be in touch
Cheers
Chris

Thanks BlackMan I'll check it out.
Regards
Chris
0
 
viktornetCommented:
Hello Chris!

Please send me the example app.... I use D4, although I think it will compile.. PLease send me the code so I can check it out...

btw- Try it out b4 you send it so you R sure it works.. 10x

Regards,
Viktor Ivanov
0
 
xpherAuthor Commented:
Inter

How can I assign the points to you seeing as you got the ball rolling. Viktornet agrees that the points should be yours.

Chris
0
 
interCommented:
Ok, thanks but I could not solve your problem, so if you insist just drop a comment(sorry I was out for few days but here now..)

0
 
xpherAuthor Commented:
Points to Inter.


xpher
0
 
interCommented:
Thanks to all,
just drop a comment if I can do anything on audio...
regards, igor ;-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 17
  • 11
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now