微信公众平台消息接口开发(50)在线点歌/在线音乐
微信 在線聽歌 在線點(diǎn)歌 在線點(diǎn)播音樂API 公眾平臺(tái) 微信公眾平臺(tái)消息接口 微信開發(fā)模式
作者:http://www.cnblogs.com/txw1958/
原文:http://www.cnblogs.com/txw1958/archive/2013/02/25/weixin-if50-music.html
微信公眾平臺(tái)在年前開放了新的消息接口-音樂消息,這是廣大微信公眾平臺(tái)開發(fā)者的福音。
根據(jù)這一功能,如果能做出在微信中點(diǎn)歌功能,那么我們以后就可以不用安裝其他APP,
直接在微信里面關(guān)注一個(gè)賬號(hào),就可以聽歌了,還省去了安裝的麻煩,而且不用再占用空間。如果音樂資源足夠好,那將秒殺一切手機(jī)上在線聽歌軟件也不是沒有可能的。
一、獲取音樂資源
方倍工作室推出音樂API接口,目前處于測(cè)試階段,歡迎各位試用。
調(diào)用方法:
如調(diào)用“最炫民族風(fēng)” ,傳遞其其URL編碼%e6%9c%80%e7%82%ab%e6%b0%91%e6%97%8f%e9%a3%8e 就可以了。
以下url可以直接在IE8中打開,能自動(dòng)轉(zhuǎn)碼。
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": "最炫民族風(fēng)",
"description": "music",
"musicurl": "http://stream10.qqmusic.qq.com/31432174.mp3",
"hqmusicurl": "http://stream10.qqmusic.qq.com/31432174.mp3"
}
}
失敗時(shí)返回文本信息
{
"errcode": "1",
"msgtype": "text",
"text": {
"content": "檢索音樂失敗!"
}
}
二、封裝音樂消息
獲取該音樂消息的代碼如下:
else if(substr($keyword,0,6) == "點(diǎn)歌"){
$entityName = trim(substr($keyword,6,strlen($keyword)));
if ($entityName == ""){
$contentStr = "發(fā)送“點(diǎn)歌”加上歌名,如“點(diǎn)歌最炫民族風(fēng)”";
$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 = "沒有找到音樂,可能不是歌名或者檢索失敗,請(qǐng)換首歌試試!";
$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;
}
三、與微信對(duì)接
音樂消息格式如下
參數(shù)描述
ToUserName 接收方帳號(hào)(收到的OpenID) FromUserName 開發(fā)者微信號(hào) CreateTime 消息創(chuàng)建時(shí)間 MsgType music MusicUrl 音樂鏈接 HQMusicUrl 高質(zhì)量音樂鏈接,WIFI環(huán)境優(yōu)先使用該鏈接播放音樂 FuncFlag 位0x0001被標(biāo)志時(shí),星標(biāo)剛收到的消息。
四、效果演示
需要添加 天氣神 ,然后發(fā)送m,可以看到相關(guān)使用說明
1. 發(fā)送“點(diǎn)歌”加歌名,如“點(diǎn)歌最炫民族風(fēng)”,返回指定的歌曲。 2. 只發(fā)送“點(diǎn)歌”兩個(gè)字,系統(tǒng)為您選擇一首熱門歌曲。 3. 按住說話按鈕2秒鐘再松開,隨機(jī)返回一首熱門歌曲。
歌曲在WIFI環(huán)境中播放很流暢,有很好地體驗(yàn)。
這是點(diǎn)歌成功的界面
這是最上邊欄時(shí)的顯示
這是音樂播放界面
原文:http://www.cnblogs.com/txw1958/archive/2013/02/25/weixin-if50-music.html
=============================================================
關(guān)注豆立方(賬號(hào)BeanCube)方法:
1. 依次進(jìn)入以下路徑:朋友們—>添加朋友—>搜號(hào)碼,輸入beancube,不區(qū)分大小寫,點(diǎn)擊查找,然后點(diǎn)擊關(guān)注。
2. 掃描二維碼:
總結(jié)
以上是生活随笔為你收集整理的微信公众平台消息接口开发(50)在线点歌/在线音乐的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: antd 表格树如何展开_ant-des
- 下一篇: 微信运动更新有延迟吗