微信公众平台消息接口开发(50)在线点歌/在线音乐

来源:互联网 发布:检查路由器80端口 编辑:程序博客网 时间:2024/05/21 21:33

微信 在线听歌 在线点歌 在线点播 音乐API 公众平台 微信公众平台消息接口 微信开发模式  
作者:http://www.cnblogs.com/txw1958/

微信公众平台在年前开放了新的消息接口-音乐消息,这是广大微信公众平台开发者的福音。
根据这一功能,如果能做出在微信中点歌功能,那么我们以后就可以不用安装其他APP,
直接在微信里面关注一个账号,就可以听歌了,还省去了安装的麻烦,而且不用再占用空间。如果音乐资源足够好,那将秒杀一切手机上在线听歌软件也不是没有可能的。


一、获取音乐资源
方倍工作室推出音乐API接口,目前处于测试阶段,欢迎各位试用。

调用方法:

如调用“最炫民族风” ,传递其其URL编码 %e6%9c%80%e7%82%ab%e6%b0%91%e6%97%8f%e9%a3%8e 就可以了。

以下url可以直接在IE8中打开,能自动转码。

http://api2.sinaapp.com/search/music/?appkey=0020130430&appsecert=fa6095e1133d28ad&reqtype=music&keyword=%e6%9c%80%e7%82%ab%e6%b0%91%e6%97%8f%e9%a3%8e

返回格式:与微信音乐格式类似,json型:

{    "errcode": 0,    "msgtype": "music",    "music": {        "title": "最炫民族风",        "description": "music",        "musicurl": "http://stream10.qqmusic.qq.com/31432174.mp3",        "hqmusicurl": "http://stream10.qqmusic.qq.com/31432174.mp3"    }}


 

 失败时返回文本信息

{    "errcode": "1",    "msgtype": "text",    "text": {        "content": "检索音乐失败!"    }}

二、封装音乐消息

获取该音乐消息的代码如下:

else if(substr($keyword,0,6) == "点歌"){    $entityName = trim(substr($keyword,6,strlen($keyword)));    if ($entityName == ""){        $contentStr = "发送“点歌”加上歌名,如“点歌最炫民族风”";        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);        return $resultStr;    }    $apihost = "http://api2.sinaapp.com/";    $apimethod = "search/music/?";    $apiparams = array('appkey'=>"0020120430", 'appsecert'=>"fa6095e113cd28fd", 'reqtype'=>"music");    $apikeyword = "&keyword=".urlencode($entityName);    $apicallurl = $apihost.$apimethod.http_build_query($apiparams).$apikeyword;    $api2str = file_get_contents($apicallurl);    $api2json = json_decode($api2str, true);    $musicUrl = $api2json['music']['hqmusicurl'];    if ($musicUrl == ""){        $contentStr = "没有找到音乐,可能不是歌名或者检索失败,请换首歌试试!";        $resultStr = $this->transmitText($object, $contentStr, $funcFlag);    }else{        $musicArray = array("title"=>$api2json['music']['title'],                            "description"=>$api2json['music']['description'],                            "MusicUrl"=>$api2json['music']['musicurl'],                            "HQMusicUrl"=>$api2json['music']['hqmusicurl']);        $resultStr = $this->transmitMusic($object, $musicArray, $funcFlag);    }    return $resultStr;}

三、与微信对接

音乐消息格式如下

参数 描述

ToUserName  接收方帐号(收到的OpenID)FromUserName  开发者微信号CreateTime  消息创建时间MsgType  musicMusicUrl  音乐链接HQMusicUrl  高质量音乐链接,WIFI环境优先使用该链接播放音乐FuncFlag  位0x0001被标志时,星标刚收到的消息。


 

四、效果演示
需要添加 天气神 ,然后发送m,可以看到相关使用说明

1. 发送“点歌”加歌名,如“点歌最炫民族风”,返回指定的歌曲。2. 只发送“点歌”两个字,系统为您选择一首热门歌曲。3. 按住说话按钮2秒钟再松开,随机返回一首热门歌曲。

歌曲在WIFI环境中播放很流畅,有很好地体验。

这是点歌成功的界面

原文:http://www.cnblogs.com/txw1958/archive/2013/02/25/weixin-if50-music.html



 

 

原创粉丝点击