controller_stream_use.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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: StreamBuilder<int>(
  73. builder: (BuildContext context, snapshot) {
  74. return buildText("volume: ${snapshot.data}");
  75. },
  76. stream: controller.volumeStream,
  77. initialData: controller.volume,
  78. ),
  79. );
  80. }
  81. Widget buildVideoInfo() {
  82. return StreamBuilder<VideoInfo>(
  83. builder: (BuildContext context, snapshot) {
  84. if (!snapshot.hasData || !snapshot.data.hasData) {
  85. return buildText("videoInfo: null");
  86. }
  87. return buildInfo(snapshot.data);
  88. },
  89. stream: controller.videoInfoStream,
  90. initialData: controller.videoInfo,
  91. );
  92. }
  93. buildInfo(VideoInfo info) {
  94. return Container(
  95. padding: const EdgeInsets.all(8.0),
  96. child: Column(
  97. children: <Widget>[
  98. buildInfoText("width", info.width.toString()),
  99. buildInfoText("height", info.height.toString()),
  100. buildInfoText("degree", info.degree.toString()),
  101. buildInfoText("currentPosition", info.currentPosition.toString()),
  102. buildInfoText("totalDuration", info.duration.toString()),
  103. buildInfoText("tcp speed", info.tcpSpeed.toString()),
  104. buildInfoText("isPlaying", info.isPlaying.toString()),
  105. ],
  106. ),
  107. );
  108. }
  109. StreamSubscription subscription;
  110. subscriptPlayFinish() {
  111. subscription = controller.playFinishStream.listen((data) {
  112. showToast(currentI18n.playFinishToast);
  113. });
  114. }
  115. buildTextureId() {
  116. var stream = StreamBuilder<int>(
  117. builder: (BuildContext context, snapshot) {
  118. return buildText("texture id = ${snapshot.data}");
  119. },
  120. initialData: controller.textureId,
  121. stream: controller.textureIdStream,
  122. );
  123. return Column(
  124. children: <Widget>[
  125. buildButton("change data source", changeId),
  126. stream,
  127. ],
  128. );
  129. }
  130. changeId() {
  131. controller.setDataSource(
  132. DataSource.network(
  133. "http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4",
  134. ),
  135. autoPlay: true);
  136. }
  137. }
  138. Widget buildInfoText(String title, String text) {
  139. return Container(
  140. height: 20.0,
  141. alignment: Alignment.center,
  142. child: Text(
  143. "$title : $text",
  144. textAlign: TextAlign.center,
  145. ),
  146. );
  147. }
  148. Widget buildText(String text) {
  149. return Container(
  150. height: 60.0,
  151. alignment: Alignment.center,
  152. child: Text(
  153. text,
  154. textAlign: TextAlign.center,
  155. ),
  156. );
  157. }
  158. Widget buildButton(String text, Function function) {
  159. return OutlineButton(
  160. child: Text(text),
  161. onPressed: function,
  162. );
  163. }
  164. const decoration = BoxDecoration(
  165. border: Border(
  166. top: _borderSlider,
  167. bottom: _borderSlider,
  168. left: _borderSlider,
  169. right: _borderSlider,
  170. ),
  171. );
  172. const _borderSlider = BorderSide(
  173. color: Colors.grey,
  174. style: BorderStyle.solid,
  175. width: 1,
  176. );