手机控制电脑上的酷狗切换歌曲
国庆回家,喜欢听歌,音响接上电脑,由于电脑和音响放在三楼上,要是遇到不喜欢听的歌就得爬到3楼去换歌,太麻烦。想个办法就弄了这么个控制程序。人在一楼吃饭,可以通过手机控制三楼上的电脑播放歌曲,只要PC和手机端都可以接入互联网,没有距离的限制。
思路:手机和pc机通过中转站通信,中转站选择美国的免费服务器空间,手机上通过浏览器提交换歌的参数,服务端接受并保存参数到数据库上。pc端的程序每隔5秒去读取服务器数据库上的数据,根据取回的数据作简单的判断,然后调用win32 API键盘操作函数, 模拟键盘操作来控制酷狗切换歌曲。
1. 推荐使用免费的服务器空间:http://www.freewebhostingarea.com/,我申请空间的地址是,http://fjut.ueuo.com/
2. 在数据库上建表KuGouController,如下图:
三个字段分别表示 操作类型、当前播放状态、是否改变状态
3. 编写php文件,上传到服务器根目录。
/**KuGouInfo_PC1_Rec.php 读取手机端是否提交播放状态*/
<?php //连接数据库 $con = mysql_connect("localhost","506129","密码是什么呢?") or die("连接服务器失败!"); //查询状态 $sql = "SELECT * FROM KuGouController WHERE ActionType = 'TrackSwitch'"; $result = mysql_db_query("506129", $sql, $con) or die("查询数据库失败!"); while($row = mysql_fetch_array($result)) { echo $row['IsChange']; } ?>
/**KuGouInfo_PC2_Rec.php 读取手机端提交的播放状态*/
<?php //接收参数 $Type = $HTTP_POST_VARS["postType"]; //连接数据库 $con = mysql_connect("localhost","506129","密码是什么呢?") or die("连接服务器失败!"); //输出状态 $sql = "SELECT * FROM KuGouController WHERE ActionType = 'TrackSwitch'"; $result = mysql_db_query("506129", $sql, $con) or die("查询数据库失败!"); while($row = mysql_fetch_array($result)) { echo $row['CurrentState']; } //切换状态 $sql = "UPDATE KuGouController SET IsChange = 'N' WHERE ActionType = 'TrackSwitch'"; $query = mysql_db_query("506129", $sql, $con) or die("更新数据库失败!"); ?>
/**KuGouInfo_Mobile_Rec.php 接收手机端提交的播放状态并存入数据库*/
<?php //接收参数 $Type = $_GET["actionType"]; //连接数据库 $con = mysql_connect("localhost","506129","密码?") or die("连接服务器失败!"); //上一曲 if($Type == "PreviousTrack") { $sql = "UPDATE KuGouController SET CurrentState = 'PreviousTrack', IsChange = 'Y' WHERE ActionType = 'TrackSwitch'"; $query = mysql_db_query("506129", $sql, $con) or die("更新数据库失败!"); } //下一曲 else if($Type == "NextTrack") { $sql = "UPDATE KuGouController SET CurrentState = 'NextTrack', IsChange = 'Y' WHERE ActionType = 'TrackSwitch'"; $query = mysql_db_query("506129", $sql, $con) or die("更新数据库失败!"); } else { echo "非法提交,会发生吗? http://www.cnblogs.com/fjut"; } ?>
4.手机上访问的html文件,上传到服务器根目录。
<html> <body> <title>酷狗播放远程控制</title> <a href="http://fjut.ueuo.com/KuGouInfo_Mobile_Rec.php?actionType=PreviousTrack"><h3>上一曲</h3></a></br> <a href="http://fjut.ueuo.com/KuGouInfo_Mobile_Rec.php?actionType=NextTrack"><h3>下一曲</h3></a> </body> </html>
5. PC端上的Delphi程序。
//使用idhttp控件提交和获取数据 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls; type TForm1 = class(TForm) IdHTTP1: TIdHTTP; Timer1: TTimer; Button2: TButton; Edit1: TEdit; Label1: TLabel; procedure Timer1Timer(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; postList: TStrings; Response: TStringStream; result: string; done: Boolean; implementation {$R *.dfm} procedure TForm1.Button2Click(Sender: TObject); begin Response := TStringStream.Create(''); postList := TStringList.Create; Timer1.Enabled := True; Button2.Caption := 'Controlling'; button2.Enabled := False; end; procedure TForm1.Timer1Timer(Sender: TObject); var str: string; begin //获取状态是否改变 //IdHTTP1.IOHandler.Close; try str := IdHTTP1.Get('http://fjut.ueuo.com/KuGouInfo_PC1_Rec.php'); except Edit1.text := '连接服务器出错!'; end; if( str= 'Y') then begin try //Post数据 IdHTTP1.HandleRedirects := True; postList.add('postType=fjut'); IdHTTP1.Post('http://fjut.ueuo.com/KuGouInfo_PC2_Rec.php', postList, Response); done := True; except done := False; end; //获取服务端响应的数据 if(done) and (Pos('200 OK', IdHTTP1.ResponseText) <> 0) then begin try result := IdHTTP1.Get('http://fjut.ueuo.com/KuGouInfo_PC2_Rec.php'); Edit1.text := result; except Edit1.text := '连接服务器出错!'; end; //模拟键盘操作 //下一首 if(result = 'NextTrack') then begin keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键 keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0); //按下Alt键 keybd_event(VK_RIGHT, MapVirtualKey(VK_RIGHT, 0), 0, 0); //按下->键 keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键 keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0); //放开Alt键 keybd_event(VK_RIGHT, MapVirtualKey(VK_RIGHT, 0), KEYEVENTF_KEYUP, 0); //放开->键 end //上一首 else if(result = 'PreviousTrack') then begin keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键 keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0); //按下Alt键 keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), 0, 0); //按下<-键 keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键 keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0); //放开Alt键 keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), KEYEVENTF_KEYUP, 0); //放开<-键 end else begin ;//other function such as......fjut end; end; end; end; end.
6. 打开酷狗,打开delphi程序,点击 “开始” 控制,然后就可以使用手机上的浏览器打开http://fjut.ueuo.com/可以进行歌曲的上下切换。不受距离的影响,不管人在哪,只要PC和手机端都可以介入互联网就可以切歌操作。
7. 截图:
(1).PC端:
(2). 手机端:
8. OK,现在人坐在一楼吃饭,可以拿出手机打开浏览器控制三楼电脑上的音乐切换,哈哈