controller_stream_use.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
  4. import 'package:ijkplayer_example/i18n/i18n.dart';
  5. import 'package:oktoast/oktoast.dart';
  6. class ControllerStreamUsagePage extends StatefulWidget {
  7. @override
  8. _ControllerStreamUsagePageState createState() =>
  9. _ControllerStreamUsagePageState();
  10. }
  11. class _ControllerStreamUsagePageState extends State<ControllerStreamUsagePage> {
  12. IjkMediaController controller = IjkMediaController();
  13. @override
  14. void initState() {
  15. super.initState();
  16. controller.setDataSource(
  17. DataSource.asset("assets/sample1.mp4"),
  18. autoPlay: true,
  19. );
  20. subscriptPlayFinish();
  21. }
  22. @override
  23. void dispose() {
  24. subscription?.cancel();
  25. controller?.dispose();
  26. super.dispose();
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. appBar: AppBar(
  32. title: Text(currentI18n.useStreamUsage),
  33. ),
  34. body: ListView(
  35. children: <Widget>[
  36. AspectRatio(
  37. aspectRatio: 1280 / 720,
  38. child: IjkPlayer(
  39. mediaController: controller,
  40. controllerWidgetBuilder: (ctl) {
  41. return DefaultIJKControllerWidget(
  42. controller: ctl,
  43. volumeType: VolumeType.media,
  44. );
  45. },
  46. ),
  47. ),
  48. Container(
  49. child: Row(
  50. children: <Widget>[
  51. Expanded(
  52. child: buildAudioVolume(),
  53. ),
  54. Expanded(
  55. child: Container(
  56. decoration:
  57. BoxDecoration(border: Border(left: _borderSlider)),
  58. child: buildVideoInfo(),
  59. ),
  60. ),
  61. ],
  62. ),
  63. decoration: decoration,
  64. ),
  65. buildTextureId(),
  66. ],
  67. ),
  68. );
  69. }
  70. Widget buildAudioVolume() {
  71. return Container(
  72. child: Column(
  73. children: <Widget>[
  74. StreamBuilder<int>(
  75. builder: (BuildContext context, snapshot) {
  76. return buildText("volume: ${snapshot.data}");
  77. },
  78. stream: controller.volumeStream,
  79. initialData: controller.volume,
  80. ),
  81. StreamBuilder<IjkStatus>(
  82. builder: (ctx, snapshot) {
  83. return buildText("status : ${snapshot.data}");
  84. },
  85. stream: controller.ijkStatusStream,
  86. initialData: controller.ijkStatus,
  87. ),
  88. ],
  89. ),
  90. );
  91. }
  92. Widget buildVideoInfo() {
  93. return StreamBuilder<VideoInfo>(
  94. builder: (BuildContext context, snapshot) {
  95. if (!snapshot.hasData || !snapshot.data.hasData) {
  96. return buildText("videoInfo: null");
  97. }
  98. return buildInfo(snapshot.data);
  99. },
  100. stream: controller.videoInfoStream,
  101. initialData: controller.videoInfo,
  102. );
  103. }
  104. buildInfo(VideoInfo info) {
  105. return Container(
  106. padding: const EdgeInsets.all(8.0),
  107. child: Column(
  108. children: <Widget>[
  109. buildInfoText("width", info.width.toString()),
  110. buildInfoText("height", info.height.toString()),
  111. buildInfoText("degree", info.degree.toString()),
  112. buildInfoText("currentPosition", info.currentPosition.toString()),
  113. buildInfoText("totalDuration", info.duration.toString()),
  114. buildInfoText("tcp speed", info.tcpSpeed.toString()),
  115. buildInfoText("isPlaying", info.isPlaying.toString()),
  116. ],
  117. ),
  118. );
  119. }
  120. StreamSubscription subscription;
  121. subscriptPlayFinish() {
  122. subscription = controller.playFinishStream.listen((data) {
  123. showToast(currentI18n.playFinishToast);
  124. });
  125. }
  126. buildTextureId() {
  127. var stream = StreamBuilder<int>(
  128. builder: (BuildContext context, snapshot) {
  129. return buildText("texture id = ${snapshot.data}");
  130. },
  131. initialData: controller.textureId,
  132. stream: controller.textureIdStream,
  133. );
  134. return Column(
  135. children: <Widget>[
  136. buildButton("change data source", changeId),
  137. stream,
  138. ],
  139. );
  140. }
  141. changeId() {
  142. controller.setDataSource(
  143. DataSource.network(
  144. "http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4",
  145. ),
  146. autoPlay: true);
  147. }
  148. }
  149. Widget buildInfoText(String title, String text) {
  150. return Container(
  151. height: 20.0,
  152. alignment: Alignment.center,
  153. child: Text(
  154. "$title : $text",
  155. textAlign: TextAlign.center,
  156. ),
  157. );
  158. }
  159. Widget buildText(String text) {
  160. return Container(
  161. height: 60.0,
  162. alignment: Alignment.center,
  163. child: Text(
  164. text,
  165. textAlign: TextAlign.center,
  166. ),
  167. );
  168. }
  169. Widget buildButton(String text, Function function) {
  170. return OutlineButton(
  171. child: Text(text),
  172. onPressed: function,
  173. );
  174. }
  175. const decoration = BoxDecoration(
  176. border: Border(
  177. top: _borderSlider,
  178. bottom: _borderSlider,
  179. left: _borderSlider,
  180. right: _borderSlider,
  181. ),
  182. );
  183. const _borderSlider = BorderSide(
  184. color: Colors.grey,
  185. style: BorderStyle.solid,
  186. width: 1,
  187. );