(转)aar做斗鱼简易播放器带弹幕功能
By
admin
at 2017-11-25 • 0人收藏 • 1425人看过
转自:http://bbs.aardio.com/forum.php?mod=viewthread&tid=21930&extra=page%3D1
谢谢cad老大分享.
import win.ui; /*DSG{{*/ var winform = win.form(text="斗鱼简易播放器";right=927;bottom=503) winform.add( btnView={cls="button";text="查看";left=184;top=456;right=280;bottom=488;db=1;dl=1;z=3}; edRoomId={cls="edit";text="71771";left=64;top=456;right=168;bottom=489;db=1;dl=1;edge=1;multiline=1;z=2}; ffWindow={cls="custom";left=8;top=8;right=920;bottom=448;db=1;dl=1;dr=1;dt=1;edge=1;transparent=1;z=1}; static={cls="static";text="房间号";left=16;top=464;right=64;bottom=488;db=1;dl=1;transparent=1;z=4} ) /*}}*/ import mpvPlayer; import hpsocket.tcpPullClient; import crypt; import inet.whttp; import web.json; import thread.command var mplayer = mpvPlayer(winform.ffWindow); var tcpPullClient = hpsocket.tcpPullClient() var whttp = inet.whttp() var key = "Y237pxTx2In5ayGz" var sign = function(roomId, t){ var str = "room/" ++ roomId ++ "?aid=androidhd1&cdn=ws&client_sys=android&time=" ++ t ++ key return crypt.md5(str,false); } var getFlvUrl = function(roomId){ var t = tonumber(time()) var url = "http://capi.douyucdn.cn/api/v1/room/" ++ roomId ++ "?aid=androidhd1&cdn=ws&client_sys=android&time=" ++ t ++ "&auth=" ++ sign(roomId, t) var html = whttp.get( url ) if( html ){ var r = web.json.parse(html) if( r.error === 0 ){ return r.data.rtmp_url ++ "/" ++ r.data.rtmp_live; } } } var getUrl = function(roomId="71771"){ var flvUrl = getFlvUrl(roomId) return flvUrl ? whttp.location( flvUrl, "GET" ); } var sendData = function(s){ var data = { int len; struct msg = { int len; word type = 689; byte cryptedField; byte reservedField; byte body[] = s; byte e; } } data.len = raw.sizeof(data.msg) data.msg.len = data.len tcpPullClient.send( data,raw.sizeof(data )) } var loginreq = function(roomId){ sendData( "type@=loginreq/roomid@=" ++ roomId ++ "/" ) } var joingroup = function(roomId){ sendData( "type@=joingroup/rid@=" ++ roomId ++ "/gid@=-9999/" ) } var keepLive = function(){ sendData( "type@=mrkl/" ) } tcpPullClient.onPullReceive = function(hpTcpPullClient,connId,length){ import thread.command while( var min, remain = 9, length; ; remain >= min ) { remain -= 4; var buf = {INT len}; if 0 == hpTcpPullClient.peek( buf, 4 ) { var len = buf.len; if remain < len return ; remain -= len; var bufLen = 4+len buf = raw.buffer( bufLen ) if 0 == hpTcpPullClient.fetch( buf, bufLen ) { var s = raw.tostring(buf) if( string.match( s, `type@=(.+?)/`) == "chatmsg" ){ var nickname = string.match( s, `nn@=(.+?)/`) var txt = string.match( s, `txt@=(.+?)/`) if( nickname ? txt ){ thread.command.post( "onReceiveChatmsg", nickname , txt ) } } } } } } var thCmd = thread.command() thCmd.onReceiveChatmsg = function(nick, txt){ mplayer.setOption("osd-msg1", nick ++ ":" ++ txt ); } winform.btnView.oncommand = function(id,event){ var roomId = winform.edRoomId.text var url = getUrl(roomId) if( url ){ mplayer.command("loadfile", url) if( tcpPullClient.start( "openbarrage.douyutv.com",8601,false) ){ loginreq( roomId ) joingroup( roomId ) var tmId = winform.addtimer( 15000, function(hwnd,msg,id,tick){ keepLive() } ) } } } winform.show() win.loopMessage();
我在执行这个代码的时候,发现这个调用的tcpPullClient.aardio库函数,最后一行弹出错误,
原来是重复代码导致,改为下面即可正常运行
peek = function(buf,len){ if(len===null)len=#buf; return ::HPSocket.HP_TcpPullClient_Peek(this.pSocket,buf,len); }
2 个回复 | 最后更新于 2017-11-19
完整修改如下:
//添加全局(记录房间号) var oldRoomId = ""; winform.btnView.oncommand = function(id,event){ var newRoomId = winform.edRoomId.text //判断房间号是否变化 if( oldRoomId === newRoomId ){ return ; } //如果不是第一次打开软件 if(oldRoomId != ""){ //断开一下,刷新 tcpPullClient.stop(); } //记录下现在的房间号 oldRoomId = newRoomId; var url = getUrl(newRoomId) if( url ){ mplayer.command("loadfile", url) if( tcpPullClient.start( "openbarrage.douyutv.com",8601,false) ){ loginreq( newRoomId ) joingroup( newRoomId ) var tmId = winform.addtimer( 15000, function(hwnd,msg,id,tick){ keepLive() } ) } } }
登录后方可回帖
有人发现如果改变了房间号,开启,但是弹幕还是原来的那个,这个怎么弄?
很简单, 断开链接,重新连一下就好了啊.....
增加一个端口操作...