Threads and MediaPlayer component problem

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 8
Reputation: Carson89 is an unknown quantity at this point 
Solved Threads: 0
Carson89 Carson89 is offline Offline
Newbie Poster

Threads and MediaPlayer component problem

 
0
  #1
Mar 8th, 2007
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.)
Pascal and Delphi Syntax (Toggle Plain Text)
  1. Procedure TMain_Form.NextSongNormal;
  2. var
  3. Next: integer;
  4. NextFilename: String;
  5. begin
  6. //Get The Next Track
  7. Next := MediaListRow + 1;
  8. //Clear Previous variables
  9. NextFilename := '';
  10. //Check that the Next song isn't off the end of the list
  11. if Next < MediaList_ListView.Items.Count then
  12. begin
  13. //Get Song Information
  14. MediaList_ListView.Items.Item[MediaListRow].Selected := False;
  15. MediaList_ListView.Items.Item[MediaListRow].Focused := False;
  16. MediaList_ListView.Items.Item[Next].Selected := True;
  17. MediaList_ListView.Items.Item[Next].Focused := True;
  18. //Find selected Song details from Media List
  19. with MediaList_ListView do
  20. begin
  21. ID3Title := Selected.Caption;
  22. ID3Artist := Selected.SubItems.Strings[0];
  23. ID3Album := Selected.SubItems.Strings[1];
  24. NextFilename := Selected.SubItems.Strings[2];
  25. end;
  26. //Make sure the previous media has been stopped
  27. if PlayingBoolean = True then
  28. Player_MediaPlayer.Stop;
  29. //Play Selected Playlist File
  30. PlayMedia(NextFilename);
  31. //Update the current Selected Row
  32. MediaListRow := Next;
  33. end;
  34. end;

This is the procedure to actually play the song. (This procedure starts the thread.)
Pascal and Delphi Syntax (Toggle Plain Text)
  1. Procedure TMain_Form.PlayMedia(CurrentMedia: String);
  2. var
  3. Playing: String;
  4. thr : THandle;
  5. thrID : DWORD;
  6. begin
  7. //Reset TrackTime
  8. TrackTime := 0;
  9. //Change Play Control Buttons
  10. Play_Normal_Image.Visible := False;
  11. Pause_Normal_Image.Visible := True;
  12. //Clear previous variable
  13. Player_MediaPlayer.Filename := '';
  14. //Sets Media Player Volume and Progress Bar Settings
  15. SetMPVolume(Player_MediaPlayer, VolumeControl_TrackBar.Position);
  16. MediaProgress_ProgressBar.Max := 0;
  17. //Puts Media File into Player and Play
  18. Player_MediaPlayer.Close;
  19. Player_MediaPlayer.FileName := CurrentMedia;
  20. Player_MediaPlayer.Open;
  21. Player_MediaPlayer.Play;
  22. //Set Playing Boolean to True
  23. PlayingBoolean := True;
  24. //Get the track length
  25. TrackTime := Player_MediaPlayer.Length;
  26. //Create the TrackFinished Thread
  27. Thr := CreateThread(nil, 0, @TrackFinished, nil, 0, ThrID);
  28. if (Thr = 0) then
  29. ShowMessage('Thread not created');
  30. //Sets the Max value for progress bar to allow the progress bar to start
  31. MediaProgress_ProgressBar.Max := TrackTime * 1000;
  32. //Get ID3 Information for current media
  33. GetID3(CurrentMedia);
  34. //Get, Format and Display Song information
  35. Playing := ID3Artist + ' - ' + ID3Title;
  36. Main_StatusBar.Panels.Items[2].Text := 'Now Playing: ' + Playing;
  37. end;

This is the thread that calls the next song procedure.
Pascal and Delphi Syntax (Toggle Plain Text)
  1. Function TrackFinished(P:Pointer):LongInt; stdcall;
  2. begin
  3. //Sleep whilst playing starts
  4. Sleep(500);
  5. //Sleep for a but
  6. Sleep(UMain_Unit.TrackTime);
  7. //Set Playing Boolean to False
  8. UMain_Unit.PlayingBoolean := False;
  9. //Check Playing options and perform corresponding task
  10. If Options_Form.Shuffle_CheckBox.Checked = False then
  11. begin
  12. //Play Next Song
  13. Main_Form.NextSongNormal;
  14. //Exit Thread
  15. Exit;
  16. end
  17. else
  18. begin
  19. //Play Random Next Song
  20. Main_Form.NextSongShuffle;
  21. //Exit Thread
  22. Exit;
  23. end;
  24. end;

Just in case, here are all the global variable declerations.
Pascal and Delphi Syntax (Toggle Plain Text)
  1. var
  2. Main_Form: TMain_Form;
  3. //Directory Variables
  4. RootDirectory,
  5. CurrentMedia,
  6. CurrentFile: String;
  7. //WMA Tag Variables
  8. WMA: TWMAfile;
  9. WMATitle,
  10. WMAArtist,
  11. WMAAlbum,
  12. WMAYear,
  13. WMAGenre,
  14. WMATrack,
  15. WMAComment: string;
  16. //ID3 Tag Variables
  17. ID3: Tid3v2Tag;
  18. ID3Title,
  19. ID3Artist,
  20. ID3Album,
  21. ID3Year,
  22. ID3Genre,
  23. ID3Track,
  24. ID3Comment: String;
  25. //Variables for threads
  26. TrackThr, LibraryThr: THandle;
  27. //Other Variables
  28. Found,
  29. PlaylistCount,
  30. Added,
  31. CurrentPlaylistRow, MediaListRow, TrackTime: integer;
  32. PlayingBoolean, Searching, Shuffle: boolean;
  33. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC