YSLiveDetail.dart 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:flutterappfuyou/code/live/view/YSLiveUserView.dart';
  8. import 'package:live_flutter_plugin/v2_tx_live_player.dart';
  9. import 'package:live_flutter_plugin/v2_tx_live_player_observer.dart';
  10. import 'package:live_flutter_plugin/widget/v2_tx_live_video_widget.dart';
  11. import 'package:wakelock/wakelock.dart';
  12. import '../base/YSNetWorking.dart';
  13. import '../base/YSTools.dart';
  14. import 'YSLiveAnchor.dart';
  15. import 'YSVideoDetail.dart';
  16. class YSLiveDetail extends StatefulWidget {
  17. final liveId;
  18. final String passWord;
  19. const YSLiveDetail({Key key, this.liveId, this.passWord = ''}) : super(key: key);
  20. @override
  21. _YSLiveDetailState createState() => _YSLiveDetailState();
  22. }
  23. class _YSLiveDetailState extends State<YSLiveDetail> with SingleTickerProviderStateMixin{
  24. V2TXLivePlayer _livePlayer;
  25. Map _infoDict = {};
  26. List _userArray = [];
  27. int _userCount = 0;
  28. bool _isMute = false;
  29. bool _isPause = false;
  30. WebSocket _socket;
  31. String _content = '';
  32. @override
  33. void initState() {
  34. Wakelock.enable();
  35. _createPlayer();
  36. Future.delayed(Duration(seconds: 0)).then((value) {
  37. _getVideoDetailData();
  38. });
  39. super.initState();
  40. }
  41. @override
  42. void dispose() {
  43. Wakelock.disable();
  44. _livePlayer.stopPlay();
  45. _livePlayer.destroy();
  46. super.dispose();
  47. }
  48. _createPlayer() async{
  49. _livePlayer = await V2TXLivePlayer.create();
  50. _livePlayer.addListener(onPlayerObserver);
  51. }
  52. /// Player observer
  53. onPlayerObserver(V2TXLivePlayerListenerType type, param) {
  54. debugPrint("==player listener type= ${type.toString()}");
  55. debugPrint("==player listener type= $param");
  56. }
  57. @override
  58. Widget build(BuildContext context) {
  59. return NotificationListener<CustomerValueNotification>(
  60. onNotification: (notification){
  61. Map data = notification.value;
  62. String type = data['type'];
  63. if(type=='headcount'){
  64. _userArray = data['value']['users']??[];
  65. _userCount = data['value']['total']??0;
  66. setState(() {});
  67. }else if(type=='mute'){
  68. _isMute = data['value']['MuteTime']==0;
  69. LogUtil.d('train/live2/info$_isMute');
  70. setState(() {});
  71. }else if(type=='pause'||type=='resume'){
  72. _isPause = type=='pause';
  73. setState(() {});
  74. }else if(type=='leave'){
  75. ysFlutterToast(context, '直播已结束');
  76. Navigator.of(context).pop('');
  77. }else if(type=='enter'){
  78. _livePlayer.startLivePlay(_infoDict['pull_url']);
  79. }else if(type=='staff'){
  80. List users = data['value']['users']??[];
  81. ysShowBottomAlertView(context, YSUsersAlertView(users: users,),isBarr: true);
  82. }
  83. return true;
  84. },
  85. child: AnnotatedRegion<SystemUiOverlayStyle>(
  86. value: SystemUiOverlayStyle.light,
  87. child: Scaffold(
  88. backgroundColor: Colors.black,
  89. body: SingleChildScrollView(
  90. child: Container(
  91. width: ysWidth(context),
  92. height: ysHeight(context),
  93. child: Stack(
  94. children: [
  95. Container(
  96. padding: EdgeInsets.only(top: ysTOP(context)+10),
  97. child: ClipRRect(
  98. borderRadius: BorderRadius.all(Radius.circular(10)),
  99. child: Container(
  100. width: ysWidth(context),
  101. height: ysHeight(context)-ysTOP(context)-70,
  102. child: Stack(
  103. children: [
  104. _infoDict.isNotEmpty?V2TXLiveVideoWidget(
  105. onViewCreated: (viewId) async {
  106. _livePlayer.setRenderViewID(viewId);
  107. },
  108. ):Image.asset('lib/images/图.png',fit: BoxFit.fill,width: ysWidth(context),height: ysHeight(context),),
  109. Container(
  110. width: ysWidth(context),
  111. height: ysHeight(context),
  112. padding: EdgeInsets.only(top: 10,left: 10,right: 10,bottom: 10),
  113. child: Column(
  114. children: [
  115. Container(
  116. height: 40,
  117. child: Row(
  118. children: [
  119. Container(
  120. width: (ysWidth(context)-20)*0.3,
  121. decoration: BoxDecoration(
  122. color: Colors.black,
  123. borderRadius: BorderRadius.all(Radius.circular(50))
  124. ),
  125. child: Row(
  126. children: [
  127. if(_infoDict.isNotEmpty)Container(
  128. height: 40,
  129. width: 40,
  130. decoration: BoxDecoration(
  131. borderRadius: BorderRadius.all(Radius.circular(50)),
  132. image: DecorationImage(image: NetworkImage(_infoDict['cast_avatar']))
  133. ),
  134. ),
  135. Container(
  136. width: (ysWidth(context)-20)*0.3-40,
  137. padding: EdgeInsets.only(left: 5,right: 5),
  138. child: Text(_infoDict['cast_name']??'',style: TextStyle(fontSize: 10,color: Colors.white,fontWeight: FontWeight.bold,),
  139. maxLines: 1,overflow: TextOverflow.ellipsis,),
  140. )
  141. ],
  142. ),
  143. ),
  144. GestureDetector(
  145. onTap: (){
  146. if(_socket==null)return;
  147. _socket.add(jsonEncode({'type':'staff'}));
  148. // Navigator.of(context).push(
  149. // CupertinoPageRoute(builder: (context){
  150. // return YSLiveUser();
  151. // })
  152. // );
  153. },
  154. behavior: HitTestBehavior.opaque,
  155. child: Row(
  156. children: [
  157. Container(
  158. width: (ysWidth(context)-20)*0.55,
  159. padding: EdgeInsets.only(top: 5,bottom: 5,left: 10),
  160. child: ListView.builder(
  161. itemBuilder: (context,index){
  162. Map item = _userArray[index];
  163. return Container(
  164. height: 30,
  165. width: 30,
  166. decoration: BoxDecoration(
  167. borderRadius: BorderRadius.all(Radius.circular(50)),
  168. color: Colors.white,
  169. image: DecorationImage(image: NetworkImage(item['avatar']),fit: BoxFit.cover)
  170. ),
  171. );
  172. },
  173. itemCount: _userArray.length,
  174. scrollDirection: Axis.horizontal,
  175. ),
  176. ),
  177. Container(
  178. width: (ysWidth(context)-20)*0.15,
  179. decoration: BoxDecoration(
  180. color: Colors.black,
  181. borderRadius: BorderRadius.all(Radius.circular(50))
  182. ),
  183. alignment: Alignment.center,
  184. child: Text('$_userCount',style: TextStyle(fontSize: 12,color: Colors.white),),
  185. )
  186. ],
  187. ),
  188. )
  189. ],
  190. ),
  191. ),
  192. ],
  193. ),
  194. ),
  195. if(_isPause)Center(
  196. child: Icon(Icons.play_circle_fill,size: 100,color: Colors.white,),
  197. ),
  198. if(_infoDict.isNotEmpty)Positioned(
  199. bottom: 20,
  200. left: 10,
  201. child:YSTalkView(
  202. postSocket: (socket){
  203. _socket = socket;
  204. },
  205. )
  206. ),
  207. if(_isPause)Center(
  208. child: Icon(Icons.play_circle_fill,size: 100,color: Colors.white,),
  209. ),
  210. ],
  211. ),
  212. ),
  213. ),
  214. ),
  215. Positioned(
  216. bottom: 10,
  217. left: 10,
  218. right: 10,
  219. child: Container(
  220. margin: EdgeInsets.only(top: 10,bottom: 10),
  221. height: 30,
  222. child: Row(
  223. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  224. children: [
  225. GestureDetector(
  226. onTap: (){
  227. if(_isMute){
  228. ysFlutterToast(context, '直播间禁止发言');
  229. return;
  230. }
  231. ysShowBottomAlertView2(context, YSInputView(valueSetter: (value) async{
  232. User.instance.content = value;
  233. _socket.add(jsonEncode({'type':'message','content':value}));
  234. },));
  235. },
  236. child: Container(
  237. width: (ysWidth(context)-20)*0.6,
  238. height: 30,
  239. decoration: BoxDecoration(
  240. borderRadius: BorderRadius.all(Radius.circular(50)),
  241. border: Border.all(color: Colors.white,width: 1)
  242. ),
  243. child: Text('说点什么',style: TextStyle(fontSize: 13,color: Colors.white),),
  244. alignment: Alignment.centerLeft,
  245. padding: EdgeInsets.only(left: 15,right: 15),
  246. ),
  247. ),
  248. GestureDetector(
  249. onTap: (){
  250. Navigator.pop(context);
  251. },
  252. child: Icon(Icons.close,size: 30,color: Colors.white,)
  253. )
  254. ],
  255. ),
  256. )
  257. ),
  258. ],
  259. ),
  260. ),
  261. ),
  262. ),
  263. ),
  264. );
  265. }
  266. _getVideoDetailData() async{
  267. Map request = {'live_id':widget.liveId};
  268. if(widget.passWord.isNotEmpty)request['password'] = widget.passWord;
  269. Map dict = await ysRequestHttp(context, requestType.get, 'train/live2/info', request);
  270. if(dict!=null){
  271. _infoDict = dict['data'];
  272. _livePlayer.startLivePlay(_infoDict['pull_url']);
  273. User().stream = _infoDict['live_stream']??'';
  274. setState(() {});
  275. }
  276. }
  277. }
  278. class YSLiveInfoView extends StatefulWidget {
  279. const YSLiveInfoView({Key key}) : super(key: key);
  280. @override
  281. _YSLiveInfoViewState createState() => _YSLiveInfoViewState();
  282. }
  283. class _YSLiveInfoViewState extends State<YSLiveInfoView> {
  284. @override
  285. Widget build(BuildContext context) {
  286. return Align(
  287. alignment: Alignment.topCenter,
  288. child: Container(
  289. width: MediaQuery.of(context).size.width-20,
  290. decoration: BoxDecoration(
  291. color: Colors.white,
  292. borderRadius: BorderRadius.all(Radius.circular(5))
  293. ),
  294. margin: EdgeInsets.only(top: 20,left: 10,right: 10),
  295. padding: EdgeInsets.only(top: 20,left: 10,right: 10,bottom: 20),
  296. child: Text('简介',style: TextStyle(fontSize: 14,color: Colors.black),),
  297. ),
  298. );
  299. }
  300. }
  301. class YSCommentView2 extends StatefulWidget {
  302. final double height;
  303. const YSCommentView2({Key key, this.height = 0}) : super(key: key);
  304. @override
  305. _YSCommentView2State createState() => _YSCommentView2State();
  306. }
  307. class _YSCommentView2State extends State<YSCommentView2> {
  308. List _dataArray = [];
  309. static WebSocket _socket;
  310. Timer _timer;
  311. String _content = '';
  312. bool _isMute = false;
  313. @override
  314. void initState() {
  315. User().isAnchor = false;
  316. _getSocket();
  317. super.initState();
  318. }
  319. @override
  320. void dispose() {
  321. _socket.close();
  322. if(_timer!=null){
  323. if(_timer.isActive)_timer.cancel();
  324. }
  325. super.dispose();
  326. }
  327. _getSocket() async{
  328. WebSocket.connect('wss://v2fy.niwoshenghuo.com/websocket').then((socket) {
  329. LogUtil.d('WebSocket connect');
  330. _socket = socket;
  331. socket.listen(_onData, cancelOnError: false);
  332. _timer =Timer.periodic(Duration(seconds: 50), (timer) {
  333. _socket.add('heartbeat');
  334. });
  335. _socket.add(jsonEncode({'type':'headcount'}));
  336. }).catchError((e){
  337. LogUtil.d("Unable to connect: $e");
  338. _getSocket(); // 连接超时,重新建立连接
  339. });
  340. }
  341. _onData(event) async{
  342. Map dict = jsonDecode(event);
  343. LogUtil.d("---onData---$dict");
  344. if(dict['type']=='connection'){
  345. Map data = dict['data'];
  346. _dataArray.add({'type':3,'content':data['content']});
  347. setState(() {});
  348. Map message = {};
  349. message['type'] = 'bind';
  350. message['live_stream'] = User().stream;
  351. message['uid'] = data['uid'];
  352. message['user'] = {'username':User().name,'avatar':User().avatar,'is_owner':User().isAnchor};
  353. _socket.add(jsonEncode(message));
  354. }else if(dict['type']=='message'){
  355. var data = dict['data'];
  356. if(data is List){
  357. _dataArray.add({'type':2,'content':_content,'name':User().name,'avatar':User().avatar});
  358. }else{
  359. Map user = data['user'];
  360. _dataArray.add({'type':1,'content':data['content'],'name':user['username'],'avatar':user['avatar']});
  361. }
  362. setState(() {});
  363. }else if(dict['type']=='headcount'||dict['type']=='staff'||dict['type']=='mute'||dict['type']=='pause'||dict['type']=='resume'){
  364. var data = dict['data'];
  365. if(dict['type']=='mute'){
  366. _isMute = data['MuteTime']==0;
  367. LogUtil.d('MuteTime========$_isMute');
  368. setState(() {});
  369. }
  370. }
  371. }
  372. @override
  373. Widget build(BuildContext context) {
  374. return Column(
  375. children: [
  376. Container(
  377. height: widget.height,
  378. child: ListView.separated(
  379. padding: EdgeInsets.only(top: 20,bottom: 20,left: 20,right: 20),
  380. itemBuilder: (context,index){
  381. Map item = _dataArray[index];
  382. return LayoutBuilder(builder: (context,conSize){
  383. return item['type']==3?Container(
  384. child: Text(item['content'],style: TextStyle(fontSize: 13,color: Colors.orange),),
  385. ):item['type']==1?Column(
  386. crossAxisAlignment: CrossAxisAlignment.start,
  387. children: [
  388. Row(
  389. children: [
  390. Container(
  391. height: 35,
  392. width: 35,
  393. decoration: BoxDecoration(
  394. color: Colors.white,
  395. image: DecorationImage(image: NetworkImage(item['avatar'])),
  396. borderRadius: BorderRadius.all(Radius.circular(50))
  397. ),
  398. ),
  399. Container(
  400. width: conSize.maxWidth-35,
  401. padding: EdgeInsets.only(left: 8),
  402. child: Text(item['name']??'',style: TextStyle(fontSize: 13,color: Color(0xFF575757)),),
  403. )
  404. ],
  405. ),
  406. Container(
  407. margin: EdgeInsets.only(left: 45,top: 5),
  408. padding: EdgeInsets.all(10),
  409. decoration: BoxDecoration(
  410. color: Colors.white,
  411. borderRadius: BorderRadius.all(Radius.circular(5))
  412. ),
  413. child: Text(item['content']??'',style: TextStyle(fontSize: 13,color: Color(0xFF151515)),),
  414. )
  415. ],
  416. ):Column(
  417. crossAxisAlignment: CrossAxisAlignment.end,
  418. children: [
  419. Row(
  420. children: [
  421. Container(
  422. width: conSize.maxWidth-35,
  423. padding: EdgeInsets.only(right: 8),
  424. child: Text(item['name']??'',style: TextStyle(fontSize: 13,color: Color(0xFF575757)),),
  425. alignment: Alignment.centerRight,
  426. ),
  427. Container(
  428. height: 35,
  429. width: 35,
  430. decoration: BoxDecoration(
  431. color: Colors.white,
  432. image: DecorationImage(image: NetworkImage(item['avatar'])),
  433. borderRadius: BorderRadius.all(Radius.circular(50))
  434. ),
  435. ),
  436. ],
  437. ),
  438. Container(
  439. margin: EdgeInsets.only(right: 45,top: 5),
  440. padding: EdgeInsets.all(10),
  441. decoration: BoxDecoration(
  442. color: Color(0xFFE7688C),
  443. borderRadius: BorderRadius.all(Radius.circular(5))
  444. ),
  445. child: Text(item['content']??'',style: TextStyle(fontSize: 13,color: Colors.white),),
  446. )
  447. ],
  448. );
  449. });
  450. },
  451. separatorBuilder: (context,index){
  452. return Container(height: 10,);
  453. },
  454. itemCount: _dataArray.length
  455. ),
  456. ),
  457. Container(
  458. height: 50,
  459. color: Colors.white,
  460. width: MediaQuery.of(context).size.width,
  461. padding: EdgeInsets.only(left: 20,right: 20,top: 10,bottom: 10),
  462. child: GestureDetector(
  463. onTap: (){
  464. if(_isMute){
  465. ysFlutterToast(context, '直播间禁止发言');
  466. return;
  467. }
  468. ysShowBottomAlertView2(context, YSInputView(valueSetter: (value) async{
  469. _content = value;
  470. _socket.add(jsonEncode({'type':'message','content':value}));
  471. },));
  472. },
  473. child: Container(
  474. alignment: Alignment.centerLeft,
  475. padding: EdgeInsets.only(left: 20,right: 20),
  476. child: Text('请输入您要发送的内容',style: TextStyle(fontSize: 15,color: Color(0xFF707070)),),
  477. decoration: BoxDecoration(
  478. color: Color(0xFFF5F3F0),
  479. borderRadius: BorderRadius.all(Radius.circular(5))
  480. ),
  481. ),
  482. ),
  483. )
  484. ],
  485. );
  486. }
  487. }
  488. class YSLiveDetail2 extends StatefulWidget {
  489. final liveId;
  490. final String passWord;
  491. const YSLiveDetail2({Key key, this.liveId, this.passWord}) : super(key: key);
  492. @override
  493. _YSLiveDetail2State createState() => _YSLiveDetail2State();
  494. }
  495. class _YSLiveDetail2State extends State<YSLiveDetail2> with SingleTickerProviderStateMixin{
  496. V2TXLivePlayer _livePlayer;
  497. Map _infoDict = {};
  498. List _titles = ['简介','讨论'];
  499. TabController _tabController;
  500. int _index = 0;
  501. StateSetter _tabSet;
  502. @override
  503. void initState() {
  504. Wakelock.enable();
  505. _tabController = TabController(
  506. vsync: this,
  507. length: _titles.length
  508. )..addListener(() {
  509. _index = _tabController.index;
  510. _tabSet(() {});
  511. });
  512. _createPlayer();
  513. Future.delayed(Duration(seconds: 0)).then((value) {
  514. _getVideoDetailData();
  515. });
  516. super.initState();
  517. }
  518. _createPlayer() async{
  519. _livePlayer = await V2TXLivePlayer.create();
  520. _livePlayer.addListener(onPlayerObserver);
  521. }
  522. /// Player observer
  523. onPlayerObserver(V2TXLivePlayerListenerType type, param) {
  524. }
  525. @override
  526. void dispose() {
  527. Wakelock.disable();
  528. super.dispose();
  529. }
  530. @override
  531. void deactivate() {
  532. _livePlayer?.removeListener(onPlayerObserver);
  533. _livePlayer?.stopPlay();
  534. _livePlayer?.destroy();
  535. super.deactivate();
  536. }
  537. @override
  538. Widget build(BuildContext context) {
  539. return AnnotatedRegion<SystemUiOverlayStyle>(
  540. value: SystemUiOverlayStyle.light,
  541. child: Scaffold(
  542. body: SingleChildScrollView(
  543. child: Column(
  544. children: [
  545. Container(
  546. height: MediaQuery.of(context).size.height,
  547. width: MediaQuery.of(context).size.width,
  548. color: Color(0xFFF5F3F0),
  549. child: LayoutBuilder(
  550. builder: (BuildContext context, BoxConstraints constraints) {
  551. return Column(
  552. children: [
  553. Container(
  554. height: constraints.maxHeight*0.4,
  555. color: Colors.black,
  556. child: V2TXLiveVideoWidget(
  557. onViewCreated: (viewId) async {
  558. _livePlayer.setRenderViewID(viewId);
  559. },
  560. ),
  561. ),
  562. Container(
  563. height: constraints.maxHeight*0.6,
  564. child: DefaultTabController(
  565. length: _titles.length,
  566. child: Column(
  567. children: [
  568. Container(
  569. width: ysWidth(context),
  570. height: 50,
  571. child: StatefulBuilder(
  572. builder: (context,setState){
  573. _tabSet = setState;
  574. return TabBar(
  575. controller: _tabController,
  576. indicatorColor: Color(0xFFFA4444),
  577. labelColor: Color(0xFFFA4444),
  578. indicatorWeight: 2,
  579. indicatorPadding: EdgeInsets.all(0),
  580. labelPadding: EdgeInsets.all(0),
  581. indicatorSize: TabBarIndicatorSize.label,
  582. labelStyle: TextStyle(fontSize: 15),
  583. unselectedLabelColor: Color(0xFF5D6978),
  584. tabs: _titles.map((f) {
  585. return Container(
  586. color: _titles[_index]==f?Colors.transparent:Colors.white,
  587. alignment: Alignment.center,
  588. child: Tab(text: '$f'),
  589. width: ysWidth(context)/2,
  590. );
  591. }).toList(),
  592. );
  593. },
  594. ),
  595. ),
  596. Container(
  597. height: constraints.maxHeight*0.6-50,
  598. child: TabBarView(
  599. controller: _tabController,
  600. children: _titles.map((f) {
  601. return f=='简介'?YSLiveInfoView():_infoDict.isNotEmpty?YSCommentView2(
  602. height: constraints.maxHeight*0.6-100
  603. ):Container();
  604. }).toList(),
  605. )
  606. ),
  607. ],
  608. ),
  609. ),
  610. )
  611. ],
  612. );
  613. },
  614. )
  615. ),
  616. ],
  617. ),
  618. ),
  619. ),
  620. );
  621. }
  622. _getVideoDetailData() async{
  623. Map request = {'live_id':widget.liveId};
  624. if(widget.passWord.isNotEmpty)request['password'] = widget.passWord;
  625. Map dict = await ysRequestHttp(context, requestType.get, 'train/live2/info', request);
  626. if(dict!=null){
  627. _infoDict = dict['data'];
  628. _livePlayer.startLivePlay(_infoDict['pull_url']);
  629. User().stream = _infoDict['live_stream']??'';
  630. setState(() {});
  631. }
  632. }
  633. }