View Single Post
Old 2008-04-06, 15:33   #7 (permalink)
hbe02
Portal Member
 
Join Date: Apr 2008
Posts: 26
Thanks: 2
Thanked 1 Time in 1 Post

Country:


Default

very impressive code. i see youve made it pretty standardized and professional. my code is not really for normal users so all you do is send a string path file on the TCP socket, the plugin recieves it and simply plays the file.
check out this post maybe you can help me:
play file

btw what is GUIWindowManager
and how does this work:
GUIWindowManager.SendThreadMessage(message);

anyways, great job! I also attached a copy of what ive done :-)

[EDIT] okay my university firewall seems to have a problem with me uploading this file.

until i firgure that out, here is some sample code :
Code:
  private HDSockets comSocket;
        private Thread ListeningProgram;
        private bool newcommand = false;
        private String FileToPlay;
        private System.Windows.Forms.Timer timer1;
       

        public override bool Init()
        {
            return Load(GUIGraphicsContext.Skin + @"\HDMPPlugin.xml");
        }
      
        protected override void OnPageLoad()
        {
            //load listening socket
            comSocket = new HDSockets(4500, HDSockets.ConnectionType.TCP);
            comSocket.Bind();
            comSocket.Listen(5);

            //start thread that recieves data
            ListeningProgram = new Thread(commandsHandler);
            ListeningProgram.IsBackground = true;
            ListeningProgram.Start();

            //initialize parameters
            newcommand = false;

            //initialize timer to play songs
            timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 1000;
            timer1.Tick += new System.EventHandler(timer1_Tick);
            timer1.Enabled = true;
            //popUp("HD SmartHome Status", "HD SmartHome Has Been Loaded Successfuly");
        }

        void commandsHandler(object obj) 
        {
            
            while (true) //loop forever listening for recieved commands
            {
                HDSockets Client = new HDSockets(4500, comSocket.Accept());   //accept connection and create NetSocket to represetn client
                ThreadPool.QueueUserWorkItem((new WaitCallback(clientHandler)), Client); //handle client in separate thread
            }
        }

        void clientHandler(Object obj)
        { //playing here created alot of problems b/c 
          //media player instance cannot be declared here
            HDSockets Client = (HDSockets)(obj);
            FileToPlay = Client.Recieve();
            newcommand = true;
            Client.Close();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (newcommand == true)
            {
                MediaPortal.Player.g_Player.Play(FileToPlay.Trim(), MediaPortal.Player.g_Player.MediaType.Music);
                newcommand = false;
            }
        }
        private void SendtoVoice(String IP, String Playlist)
        {
            HDSockets initSocket = new HDSockets(4501, HDSockets.ConnectionType.TCP);
            if (initSocket.Connect(IP) == true)
            {
                initSocket.Send(Playlist);
                initSocket.Close();
            }
        }


        protected override void OnClicked(int controlId, GUIControl control,Action.ActionType actionType)
        {
            if (control == buttonOne)
                OnButtonOne();
          
            base.OnClicked(controlId, control, actionType);
        }

        private void OnButtonOne()
        {
            SendtoVoice("127.0.0.1", "Activate");
            popUp("Status", "HD Smart Home Activated Successfuly");
        }

        private void popUp(String title, String text)
        {
            GUIDialogOK dlg = (GUIDialogOK)GUIWindowManager.GetWindow(
            (int)GUIWindow.Window.WINDOW_DIALOG_OK);
            dlg.SetHeading(title);
            dlg.SetLine(1, text);
            dlg.SetLine(2, String.Empty);
            dlg.SetLine(3, String.Empty);
            dlg.DoModal(GUIWindowManager.ActiveWindow);
        }
A few comments on my code, I tried Playing the file directly in my thread that handles socket requests. That didnt work! i cant remember why i debugged it a long time ago. After that i tried playing the file in the Process() function of my plugin. that also backfired and gave me some errors executing after a few times. sorry i dont remember much details. so what i did was make a timer that executes every 0.5 seconds. this timers simply checks if a flag has been change, this flag indicates if a request was recieved in the socket. it anything was recieved it simply plays the file located in a variable that is stored when the plugin recieves the data. yes i thought this approach was a bit eccentric, but.. thats the only was i could make it work that fast:-D

Last edited by hbe02; 2008-04-06 at 15:37. Reason: Automerged Doublepost
hbe02 is offline   Reply With Quote