[PHPRPC] PHPRPC只应用实例----播放器DEMO

LibreTeam 2009-02-20
C#--PHP--MySQL--PHPRPC的播放器!

这个项目是为采集来的歌手,歌曲,专辑名称歌曲url制作的播放DEMO

PHPRPC在这个项目中负责数据服务中间件,负责写入和检索数据对外提供数据库接口。

ps:我习惯性的使用PHPRPC做数据服务中间件。

c#客户端,PHP做服务端,数据库用的是MySQL。

PHPRPC使用的是PHPRPC for PHP 3.0.1
----------------------------------------------神奇的分割线 -------------------------------------------------------------------------------------------------
下面面上图,看代码:

Client(C#)




客户端代码:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Resources;
using System.Threading;
using System.Net;
using org.phprpc;
using org.phprpc.util;
using System.IO;

namespace LibreMusic
{
    public interface Music
    {
        void GetSonger(string sex, string addr, PHPRPC_Callback<string[][]> callback);
        void GetSonger(PHPRPC_Callback<string[][]> callback);
        void GetRecord(string s_id, PHPRPC_Callback<string[][]> callback);
    }

    public delegate void ListViewDeleage(string[][] arr);

    public partial class Main : Form
    {
        private string m_sex="";
        private string m_class="";
        private PHPRPC_Client m_rpc;
        private Music m_music;
        private string[][] m_list;
        private ResourceManager m_rm;

        private string m_state;

        private Thread m_tListView;

        private WebClient m_client;
        private Regex rAuthHtml;
        private Match mAuth;
        public Main()
        {        
            InitializeComponent();
            m_state = "";
            rAuthHtml = new Regex("\"(.+)\"");
            m_client = new WebClient();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            try
            {
                m_rpc = new PHPRPC_Client("http://musicdoo.gx3.cn/rpc/api.php");
                m_rpc.EncryptMode = 2;
                m_rpc.Charset = "GBK";
            }
            catch
            {
                MessageBox.Show("与服务器通信失败");
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
            m_music = (Music)m_rpc.UseService(typeof(Music));
            
            comboBox1.SelectedItem = comboBox1.Items[0];

            m_rm = new ResourceManager("LibreMusic.LibreRes", System.Reflection.Assembly.GetExecutingAssembly());
            imageList1.Images.Add("icon01", (Image)m_rm.GetObject("icon1"));

            listView1.DoubleClick += new EventHandler(SongerClick);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            listView1.Groups.Clear();
            listView1.Items.Clear();
            toolStripProgressBar1.Value = 0;
            state.Text = "服务器通信中......";
            timer1.Enabled = true;
            m_class = comboBox1.Text;
            if (radioButton1.Checked)
                m_sex = radioButton1.Text;
            if (radioButton2.Checked)
                m_sex = radioButton2.Text;
            if (radioButton3.Checked)
                m_sex = radioButton3.Text;

            m_music.GetSonger(m_sex,m_class,SongerCallBack);
        }

        private void ListViewAdd(string[][] arr)
        {
            listView1.Groups.Clear();
            listView1.Columns.Clear();
            listView1.Items.Clear();
            state.Text = "设置列表中......";
            toolStripProgressBar1.Maximum = arr.Length-1;
            listView1.View = View.LargeIcon;
            listView1.LargeImageList = imageList1;
            listView1.MultiSelect = false;
            
            string[] val;
            ListViewItem itemX;
            for (int i = 0; i < arr.Length; i++)
            {
                val = arr[i];
                itemX = new ListViewItem(val[1]);
                itemX.ImageKey = "icon01";
                itemX.SubItems.Add(val[0]);
                itemX.SubItems.Add("songer");
                listView1.Items.Add(itemX);
                toolStripProgressBar1.Value = i;
            }
            state.Text = "完成";
            if (toolStripProgressBar1.Value == arr.Length - 1)
                timer1.Enabled = false;

            button1.Enabled = true;
        }

        private void SongerClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                switch (item.SubItems[2].Text)
                { 
                    case "songer":
                        state.Text = "服务器通信中......";
                        timer1.Enabled = true;
                        m_music.GetRecord(item.SubItems[1].Text,RecordCallBack);
                        break;
                    case "record":
                        Play();
                        break;
                }
            }
        }

        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        private void Play()
        {
            byte[] htmlData = m_client.DownloadData(new Uri("http://www.6621.com/" + listView1.SelectedItems[0].SubItems[4].Text));
            string html = Encoding.Default.GetString(htmlData);
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    wmp1.URL = listView1.SelectedItems[0].SubItems[3].Text +
                        "?tflag=1234158165&pin=ed4cb4d64bb879cb5b8caa7e995e13e0&ip=222.169.11.102";
                    wmp1.Ctlcontrols.play();
                }
            }
            catch
            { }
        }

        private void ThreadFuncListVies()
        {
            listView1.Invoke(new ListViewDeleage(ListViewAdd), new object[] { m_list});
        }

        private void SongerCallBack(string[][] result,Object[] args,string output,PHPRPC_Error warning)
        {
            state.Text = "设置列表中......";
            m_list = result;
            m_tListView = new Thread(new ThreadStart(ThreadFuncListVies));
            m_tListView.Start();
        }

        private void RecordCallBack(string[][] result, Object[] args, string output, PHPRPC_Error warning)
        {
            
            button2.Enabled = true;
            timer1.Enabled = true;
            state.Text = "设置列表中......";
            
            string[] val;
            string t="";
            ListViewGroup g=new ListViewGroup();
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.MultiSelect = false;
            listView1.Items.Clear();
            listView1.Columns.Add("歌曲名称", 400);
            listView1.Columns.Add("歌手",150);
            toolStripProgressBar1.Maximum = result.Length - 1;
            for (int i = 0; i < result.Length; i++)
            { 
                val=result[i];
                if (t != val[2])
                {
                    g = new ListViewGroup(val[2]);
                    listView1.Groups.Add(g);
                }

                t = val[2];
                ListViewItem itemX = new ListViewItem(val[0]);
                itemX.SubItems.Add(val[3]);     //re_name
                itemX.SubItems.Add("record");   //mark
                itemX.SubItems.Add(val[1]);     //re_mp3url
                itemX.SubItems.Add(val[4]);     //re_url
                itemX.Group = g;
                listView1.Items.Add(itemX);
                toolStripProgressBar1.Value = i;
            }

            state.Text = "完成";
            if (toolStripProgressBar1.Value == (result.Length - 1))
                timer1.Enabled = false;

            //listView1.DoubleClick += new EventHandler(Play);
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //state.Text = m_state;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (m_list.Length > 0)
            {
                state.Text = "设置列表中......";
                button2.Enabled = false;
                ListViewAdd(m_list);
                timer1.Enabled = true;
            }
        }
    }
}



服务端代码:

aip.php

<?php
require_once("server/phprpc_server.php");
require_once("db.class.php");
require_once("music.class.php");

$rpc = new phprpc_server();
$rpc->setCharset("GBK");
$rpc->add(array(
	"GetSonger",
	"GetRecord",
	"SetAuthCode"
	),new Music());

$rpc->start();
//$m =new Music();
//print_r($m->GetRecord(1350));
?>



music.class.php
<?php
class Music extends DB
{
	public function __construct()
	{
		parent::__construct();
	}

	public function GetSonger($sex="男",$addr="华人")
	{
		$sql="select * from songer where s_sex='$sex' and s_class='$addr'";
		return $this->fetchAll($sql);
		//$arr[]=array("你好么",iconv("gb2312","utf-8","你好"));
		//return $arr;
	}

	public function GetRecord($s_id)
	{
		$sql="select `re_name`, `re_mp3url`, `r_name`, `s_name`,`re_url` FROM `repertoire` where s_id='$s_id'";
		return $this->fetchAll($sql);
	}

	public function SetAuthCode($codestr)
	{
		return file_get_contents("http://1111111.6621.com/getuuauthcode");
	}
};
?>


db.class.php
<?php
require_once("config.class.php");
class DB extends Config 
{
	//Class Var
	private $pdo=null;
	private $fetchmode_assoc=PDO::FETCH_ASSOC;

	//构造函数
	public function __construct()
	{
		$dns=parent::DBType.":host=".parent::DBHost.";dbname=".parent::DBName;
		$this->pdo = new PDO($dns,parent::DBUser,parent::DBPassWord);
		//$this->Query("set names utf-8");
		$this->Query("set names gb2312");
	}

	public function getPDO()
	{
		return $this->pdo;
	}
	
	
	//执行sql语句
	public function Query($sql)
	{
		return $this->pdo->query($sql);
	}
	
	public function Execute($sql)
	{
		return $this->pdo->exec($sql);
	}
	
	//获取所有记录
	public function fetchAll($sql)
	{
		$rs=$this->Query($sql);
		return $rs->fetchAll($this->fetchmode);
	}
	
	//获取单个记录
	public function Fetch($sql)
	{
		$rs=$this->Query($sql);
		return $rs->fetch($this->fetchmode_assoc);
	}
	
}
?>


config.class.php
<?php
class Config
{
	//InitDB
	const DBType="mysql";			//DataBaseType
	const DBHost="127.0.0.1";		//ServerIP
	const DBUser="root";			//UserName
	const DBPassWord="123456";	        //PassWord
	const DBName="LibreMusic";		//DabaBaseName
		
}

?>
andot 2009-02-20
上图,上图,能上软件下载就好了,直接用!
LibreTeam 2009-02-21
图怎么不见了,?奇怪
ideage 2009-02-21
很精彩的应用!期待图!
LibreTeam 2009-02-21

LibreTeam 2009-02-21
啥破玩意啊,发不了图
andot 2009-02-21
你可以把帖子先发的论坛里,然后图引用论坛中的地址就可以了。哈哈
xzj127 2009-02-21
  图片
andot 2009-02-21
          try  
          {  
              m_rpc = new PHPRPC_Client("http://musicdoo.gx3.cn/rpc/api.php");  
              m_rpc.EncryptMode = 2;  
              m_rpc.Charset = "GBK";  
          }  
          catch  
          {  
              MessageBox.Show("与服务器通信失败");  
              System.Diagnostics.Process.GetCurrentProcess().Kill();  
          }  

刚才看了一下,发现这一段。这个地方不需要try catch,这个地方虽然设置服务器地址,但是实际上并没有跟服务器进行通讯。所以这里是捕捉不到错误的。
LibreTeam 2009-02-23


Global site tag (gtag.js) - Google Analytics