Carson89 0 Newbie Poster

Ok, so im just finishing a media player program which works to an extent. Now, the problem im having is that i need to load a new file into the media player component and play it, after a previous song has finished playing. To acomplish this, i have written a thread which starts as a song begins to play, then waits until the song has finished. After this, it runs a prodecure to find and play the next song in the list.

Although it plays the next song in the list as it should, it seems to be loading the song into a player that i dont have access to controlling. (As if it was creating another player and using that instead of the one i've given it.)
If i can solve this problem, i would be delighted as this has been haunting me for quite some time.
Here is the code for the important bits.

This is the procedure to find and play the next song. (There is a similar procedure for shuffleing the songs.)

Procedure TMain_Form.NextSongNormal;
var
  Next: integer;
  NextFilename: String;
begin
  //Get The Next Track
  Next := MediaListRow + 1;
  //Clear Previous variables
  NextFilename := '';
  //Check that the Next song isn't off the end of the list
  if Next < MediaList_ListView.Items.Count then
    begin
      //Get Song Information
      MediaList_ListView.Items.Item[MediaListRow].Selected := False;
      MediaList_ListView.Items.Item[MediaListRow].Focused := False;
      MediaList_ListView.Items.Item[Next].Selected := True;
      MediaList_ListView.Items.Item[Next].Focused := True;
      //Find selected Song details from Media List
      with MediaList_ListView do
        begin
          ID3Title := Selected.Caption;
          ID3Artist := Selected.SubItems.Strings[0];
          ID3Album := Selected.SubItems.Strings[1];
          NextFilename := Selected.SubItems.Strings[2];
        end;
      //Make sure the previous media has been stopped
      if PlayingBoolean = True then
        Player_MediaPlayer.Stop;
      //Play Selected Playlist File
      PlayMedia(NextFilename);
      //Update the current Selected Row
      MediaListRow := Next;
    end;
end;

This is the procedure to actually play the song. (This procedure starts the thread.)

Procedure TMain_Form.PlayMedia(CurrentMedia: String);
var
  Playing: String;
  thr : THandle;
  thrID : DWORD;
begin
  //Reset TrackTime
  TrackTime := 0;
  //Change Play Control Buttons
  Play_Normal_Image.Visible := False;
  Pause_Normal_Image.Visible := True;
  //Clear previous variable
  Player_MediaPlayer.Filename := '';
  //Sets Media Player Volume and Progress Bar Settings
  SetMPVolume(Player_MediaPlayer, VolumeControl_TrackBar.Position);
  MediaProgress_ProgressBar.Max := 0;
  //Puts Media File into Player and Play
  Player_MediaPlayer.Close;
  Player_MediaPlayer.FileName := CurrentMedia;
  Player_MediaPlayer.Open;
  Player_MediaPlayer.Play;
  //Set Playing Boolean to True
  PlayingBoolean := True;
  //Get the track length
  TrackTime := Player_MediaPlayer.Length;
  //Create the TrackFinished Thread
  Thr := CreateThread(nil, 0, @TrackFinished, nil, 0, ThrID);
    if (Thr = 0) then
    ShowMessage('Thread not created');
  //Sets the Max value for progress bar to allow the progress bar to start
  MediaProgress_ProgressBar.Max := TrackTime * 1000;
  //Get ID3 Information for current media
  GetID3(CurrentMedia);
  //Get, Format and Display Song information
  Playing := ID3Artist + ' - ' + ID3Title;
  Main_StatusBar.Panels.Items[2].Text := 'Now Playing: ' + Playing;
end;

This is the thread that calls the next song procedure.

Function TrackFinished(P:Pointer):LongInt; stdcall;
begin
  //Sleep whilst playing starts
  Sleep(500);
  //Sleep for a but
  Sleep(UMain_Unit.TrackTime);
  //Set Playing Boolean to False
  UMain_Unit.PlayingBoolean := False;
  //Check Playing options and perform corresponding task
  If Options_Form.Shuffle_CheckBox.Checked = False then
    begin
      //Play Next Song
      Main_Form.NextSongNormal;
      //Exit Thread
      Exit;
    end
  else
    begin
      //Play Random Next Song
      Main_Form.NextSongShuffle;
      //Exit Thread
      Exit;
    end;
end;

Just in case, here are all the global variable declerations.

var
  Main_Form: TMain_Form;
  //Directory Variables
  RootDirectory,
  CurrentMedia,
  CurrentFile: String;
  //WMA Tag Variables
  WMA: TWMAfile;
  WMATitle,
  WMAArtist,
  WMAAlbum,
  WMAYear,
  WMAGenre,
  WMATrack,
  WMAComment: string;
  //ID3 Tag Variables
  ID3: Tid3v2Tag;
  ID3Title,
  ID3Artist,
  ID3Album,
  ID3Year,
  ID3Genre,
  ID3Track,
  ID3Comment: String;
  //Variables for threads
  TrackThr, LibraryThr: THandle;
  //Other Variables
  Found,
  PlaylistCount,
  Added,
  CurrentPlaylistRow, MediaListRow, TrackTime: integer;
  PlayingBoolean, Searching, Shuffle: boolean;
  SearchFile: string;

If anyone can work out why the next song is being played in what seems to be a different mediaplayer component and not the one i give it, I would be very grateful.

Thank you in advance for any help anyone may be able to provide.

Carson

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.