0

I created a class which run VLC songs. he user can control the playing externally. When I run, it works but when I try to send it as a thread it is not. I tried placing the code inside

new Thread(() =>
{...}).Start();

block or use

var thread1 = new Thread(VlcPlayer.RunPlayer);
threa1.Start();

but in both cases the player does not start.

this is the code: Global.VaProxy is the class which the user has external access to.

public class VlcPlayer
    {
        private static int _waitForVlc = 500;//ms
        public static void RunPlayer()
        {
            //new Thread(() =>
            //{
                var randomPlay = true;
                var musicPreset = int.Parse(ConfigFile.GetEntry("MusicPreset"));
                var playListFile =
                    ConfigFile.GetEntryArray("MusicPresetFile", musicPreset);
                Console.WriteLine("running....");
                Globals.vaProxy.SessionState["pauseRequested"] = false;
                Globals.vaProxy.SessionState["skipRequested"] = false;
                Globals.vaProxy.SessionState["volumeUpRequested"] = false;
                Globals.vaProxy.SessionState["volumeDownRequested"] = false;
                Globals.vaProxy.SessionState["stopRequested"] = false;
                Globals.vaProxy.SessionState["skipRequested"] = false;
                Globals.vaProxy.SessionState["resumeRequested"] = false;
                

                // Initialize LibVLC
                Core.Initialize();

                // Create a new LibVLC instance
                var libVLC = new LibVLC();
                var playList = new Media(libVLC, playListFile);
                playList.Parse(MediaParseOptions.ParseLocal);
                Thread.Sleep(_waitForVlc);
                // Create a shuffled playlist if random play is desired
                var shuffledPlaylist = randomPlay
                    ? playList.SubItems.OrderBy(s => Guid.NewGuid()).ToList()
                    : playList.SubItems.ToList();
                // Create a media player
                var mediaPlayer = new MediaPlayer(libVLC);

                foreach (var subItem in shuffledPlaylist)
                {
                    Console.WriteLine("Still running....");

                    if (!Globals.VlcKeepRunning) break;
                    // Set the current subitem in the media player
                    mediaPlayer.Media = subItem;
                    var isPaused = false;
                    // Play the subitem
                    //mediaPlayer.Play();
                    var playTask = Task.Run(() => mediaPlayer.Play());
                    Thread.Sleep(_waitForVlc);
                    //playTask.Wait();
                    // Wait for the subitem to finish playing
                    while (mediaPlayer.State == VLCState.Playing && Globals.VlcKeepRunning)
                    //mediaPlayer.State != VLCState.Ended && mediaPlayer.State != VLCState.Error
                    {
                        // Handle the commands based on the session state
                        if (Globals.vaProxy.SessionState["pauseRequested"])
                        {
                            if (!isPaused)
                            {
                                mediaPlayer.Pause();
                                isPaused = true;
                            }

                            Globals.vaProxy.SessionState["pauseRequested"] = false;
                        }
                        else if (Globals.vaProxy.SessionState["resumeRequested"])
                        {
                            if (isPaused)
                            {
                                mediaPlayer.Play();
                                isPaused = false;
                            }

                            Globals.vaProxy.SessionState["resumeRequested"] = false;
                        }
                        else if (Globals.vaProxy.SessionState["skipRequested"])
                        {
                            break;
                        }
                        else if (Globals.vaProxy.SessionState["volumeUpRequested"])
                        {
                            mediaPlayer.Volume += 10;
                            Globals.vaProxy.SessionState["volumeUpRequested"] = false;
                        }
                        else if (Globals.vaProxy.SessionState["volumeDownRequested"])
                        {
                            mediaPlayer.Volume -= 10;
                            Globals.vaProxy.SessionState["volumeDownRequested"] = false;
                        }
                        else if (Globals.vaProxy.SessionState["stopRequested"])
                        {
                            mediaPlayer.Stop();
                            Globals.vaProxy.SessionState["stopRequested"] = false;
                            break;
                        }

                        Thread.Sleep(1000);
                    }
            }
            //}).Start();
        }
dandan21
  • 37
  • 1
  • 7

0 Answers0