controller_stream_use.dart 4.7 KB

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