Ejemplo n.º 1
0
        /// <summary>
        ///     Sends a command to Winamp using it's publically visible IPC.
        /// </summary>
        /// <param name="sParsedCommand"></param>
        /// <param name="sPassedValue"></param>
        public static void WinampCommand(string sParsedCommand, string sPassedValue)
        {
            string sFileLocation;

            try
            {
                //Parse this command first, because if WinAMP isnt open, we need to open it and get handles.
                if (sParsedCommand == "startwinamp")
                {
                    //Execute WinAMP
                    System.Diagnostics.Process.Start(AppConfiguration.configWinampHomeDirectory + "winamp.exe");

                    //Wait 5 Seconds for WinAmp to start
                    Thread.Sleep(5000);

                    //Get Handles
                    GetWinampHandles();
                }

                //If the handles are missing, then do not try to call API's
                if (privHwnd_wa == IntPtr.Zero || hwnd_pe == IntPtr.Zero)
                {
                    return;
                }

                switch (sParsedCommand)
                {
                case "stop":
                    SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_STOP, IntPtr.Zero);
                    break;

                case "play":
                    if (WinampPlaylist.PlaylistItems.Count > 0)
                    {
                        SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_PLAY, IntPtr.Zero);
                    }
                    break;

                case "skipfwd":
                    SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_SKIPFWD, IntPtr.Zero);
                    break;

                case "skipback":
                    SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_SKIPBACK, IntPtr.Zero);
                    break;

                case "pause":
                    SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_PAUSE, IntPtr.Zero);
                    break;

                case "repeat":
                    SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_REPEAT, IntPtr.Zero);
                    break;

                case "shuffle":
                    SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_SHUFFLE, IntPtr.Zero);
                    break;

                case "volume":
                    if (Convert.ToInt16(sPassedValue) >= 0 && Convert.ToInt16(sPassedValue) <= 255)
                    {
                        SendMessage(privHwnd_wa, WM_USER, (IntPtr)Convert.ToInt16(sPassedValue), (IntPtr)WINAMP_SETVOLUME);
                    }
                    break;

                case "balance":
                    if (Convert.ToInt16(sPassedValue) >= 0 && Convert.ToInt16(sPassedValue) <= 255)
                    {
                        SendMessage(privHwnd_wa, WM_USER, (IntPtr)Convert.ToInt16(sPassedValue), (IntPtr)WINAMP_BALANCE);
                    }
                    break;

                case "addfile":
                    if (Convert.ToInt16(sPassedValue) <= LibraryController.dbMediaLibrary.Count)
                    {
                        sFileLocation = LibraryController.dbMediaLibrary[Convert.ToInt16(sPassedValue)].FilePath + @"\" + LibraryController.dbMediaLibrary[Convert.ToInt16(sPassedValue)].FileName;
                        AddFileToWinAmpPlaylist(sFileLocation);
                    }
                    break;

                case "addfolder":
                    //iPassedValue contains 1st file of the folder to be added.
                    if (Convert.ToInt16(sPassedValue) <= LibraryController.dbMediaLibrary.Count)
                    {
                        for (int iLoop = 1; iLoop < LibraryController.dbMediaLibrary.Count; iLoop++)
                        {
                            if (LibraryController.dbMediaLibrary[iLoop].FilePath == LibraryController.dbMediaLibrary[Convert.ToInt16(sPassedValue)].FilePath)
                            {
                                sFileLocation = LibraryController.dbMediaLibrary[iLoop].FilePath + @"\" + LibraryController.dbMediaLibrary[iLoop].FileName;
                                AddFileToWinAmpPlaylist(sFileLocation);
                            }
                        }
                    }
                    break;

                case "jumppos":
                    //Sets Highlighted Song on Playlist by PlaylistID
                    SendMessage(privHwnd_wa, WM_USER, (IntPtr)Convert.ToInt16(sPassedValue), (IntPtr)PLAYLIST_JUMPTOPOS);

                    //Play highlighted song
                    WinampCommand("play", "0");
                    break;

                case "removefile":
                    //Remove song from playlist based on PlaylistID
                    SendMessage(hwnd_pe, WM_USER, (IntPtr)PLAYLIST_REMOVE, (IntPtr)Convert.ToInt16(sPassedValue));
                    break;

                case "clear":
                    //Clear Playlist
                    SendMessage(privHwnd_wa, WM_USER, IntPtr.Zero, (IntPtr)PLAYLIST_CLEAR);
                    break;

                case "rescan":
                    //Call Library Updader
                    Thread thBuildMediaLibrary = new Thread(new ThreadStart(LibraryController.BuildMediaLibrary));
                    thBuildMediaLibrary.Start();
                    break;

                case "shutdown":
                    //Closes WWWinamp
                    System.Environment.Exit(1);
                    break;

                case "closewinamp":
                    //Closes WinAmp
                    SendMessage(privHwnd_wa, WM_SYSCOMMAND, (IntPtr)WINAMP_EXIT, IntPtr.Zero);
                    break;

                case "restartwinamp":
                    //Restarts WinAmp
                    SendMessage(privHwnd_wa, WM_USER, IntPtr.Zero, (IntPtr)WINAMP_RESTART);

                    //Wait 5 Seconds for WinAmp to start
                    Thread.Sleep(5000);

                    //Get Handles
                    GetWinampHandles();
                    break;

                case "saveplaylist":
                    //Call IPC to save playlist to WINAMP.M3U
                    lock (threadLock)
                    {
                        SendMessage(privHwnd_wa, WM_USER, IntPtr.Zero, (IntPtr)PLAYLIST_WRITEPLAYLIST);
                    }

                    //Copy WINAMP.M3U to SavedPlaylists Folder as filename specified in URL/POST
                    File.Copy(AppConfiguration.configWinampHomeDirectory + "winamp.m3u", AppConfiguration.configWinampPlaylistDirectory + sPassedValue);
                    break;

                case "moveup":
                    Playlist_MoveSongUp(Convert.ToInt16(sPassedValue));
                    break;

                case "movedown":
                    Playlist_MoveSongDown(Convert.ToInt16(sPassedValue));
                    break;
                }
            }
            catch (Exception e)
            {
                LogHandler.LogError(e);
            }
        }