controller.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. part of './ijkplayer.dart';
  2. /// Media Controller
  3. class IjkMediaController extends ChangeNotifier {
  4. /// textureId
  5. int textureId;
  6. _IjkPlugin _plugin;
  7. bool get isInit => textureId == null;
  8. IJKEventChannel eventChannel;
  9. bool _isPlaying = false;
  10. bool get isPlaying => _isPlaying == true;
  11. set isPlaying(bool value) {
  12. this._isPlaying = value;
  13. _playingController.add(value);
  14. }
  15. StreamController<bool> _playingController = StreamController.broadcast();
  16. Stream<bool> get playingStream => _playingController.stream;
  17. StreamController<VideoInfo> _videoInfoController = StreamController.broadcast();
  18. Stream<VideoInfo> get videoInfoStream => _videoInfoController.stream;
  19. Future<void> _initIjk() async {
  20. try {
  21. var id = await _createIjk();
  22. this.textureId = id;
  23. _plugin = _IjkPlugin(id);
  24. eventChannel = IJKEventChannel(this);
  25. await eventChannel.init();
  26. } catch (e) {
  27. print(e);
  28. print("初始化失败");
  29. }
  30. }
  31. void dispose() async {
  32. await reset();
  33. _playingController.close();
  34. _videoInfoController.close();
  35. super.dispose();
  36. }
  37. Future<void> reset() async {
  38. this.textureId = null;
  39. this.notifyListeners();
  40. _plugin?.dispose();
  41. _plugin = null;
  42. eventChannel?.dispose();
  43. eventChannel = null;
  44. }
  45. Future<void> setNetworkDataSource(
  46. String url, {
  47. bool autoPlay = false,
  48. }) async {
  49. await _initDataSource(() async {
  50. await _plugin?.setNetworkDataSource(uri: url);
  51. }, autoPlay);
  52. }
  53. Future<void> setAssetDataSource(
  54. String name, {
  55. String package,
  56. bool autoPlay = false,
  57. }) async {
  58. await _initDataSource(() async {
  59. await _plugin?.setAssetDataSource(name, package);
  60. }, autoPlay);
  61. }
  62. Future<void> setFileDataSource(
  63. File file, {
  64. bool autoPlay = false,
  65. }) async {
  66. await _initDataSource(() async {
  67. await _plugin?.setFileDataSource(file.absolute.path);
  68. }, autoPlay);
  69. }
  70. Future<void> _initDataSource(
  71. Future setDataSource(),
  72. bool autoPlay,
  73. ) async {
  74. autoPlay ??= false;
  75. if (this.textureId != null) {
  76. await _plugin?.dispose();
  77. }
  78. await _initIjk();
  79. _autoPlay(autoPlay);
  80. await setDataSource();
  81. this.notifyListeners();
  82. }
  83. Future<void> playOrPause() async {
  84. var videoInfo = await getVideoInfo();
  85. var playing = videoInfo.isPlaying;
  86. if (playing) {
  87. await _plugin?.pause();
  88. } else {
  89. await _plugin?.play();
  90. }
  91. refreshVideoInfo();
  92. }
  93. Future<void> play() async {
  94. await _plugin?.play();
  95. refreshVideoInfo();
  96. }
  97. Future<void> pause() async {
  98. await _plugin?.pause();
  99. refreshVideoInfo();
  100. }
  101. Future<void> seekTo(double target) async {
  102. await _plugin?.seekTo(target);
  103. refreshVideoInfo();
  104. }
  105. Future<VideoInfo> getVideoInfo() async {
  106. Map<String, dynamic> result = await _plugin?.getInfo();
  107. var info = VideoInfo.fromMap(result);
  108. return info;
  109. }
  110. Future<void> refreshVideoInfo() async {
  111. var info = await getVideoInfo();
  112. isPlaying = info.isPlaying;
  113. _videoInfoController.add(info);
  114. }
  115. void _autoPlay(bool autoPlay) {
  116. if (autoPlay) {
  117. eventChannel.autoPlay(this);
  118. }
  119. }
  120. Future<void> stop() async {
  121. // await _plugin?.stop();
  122. // refreshVideoInfo();
  123. await _plugin.pause();
  124. await _plugin.seekTo(0);
  125. refreshVideoInfo();
  126. }
  127. }
  128. /// about channel
  129. const MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
  130. Future<int> _createIjk() async {
  131. int id = await _globalChannel.invokeMethod("create");
  132. return id;
  133. }
  134. class _IjkPlugin {
  135. MethodChannel get channel => MethodChannel("top.kikt/ijkplayer/$textureId");
  136. int textureId;
  137. _IjkPlugin(this.textureId);
  138. Future<void> dispose() async {
  139. await _globalChannel.invokeMethod("dispose", {"id": textureId});
  140. }
  141. Future<void> play() async {
  142. await channel.invokeMethod("play");
  143. }
  144. Future<void> pause() async {
  145. await channel.invokeMethod("pause");
  146. }
  147. Future<void> stop() async {
  148. await channel.invokeMethod("stop");
  149. }
  150. Future<void> setNetworkDataSource({String uri}) async {
  151. print("id = $textureId net uri = $uri");
  152. await channel.invokeMethod("setNetworkDataSource", {"uri": uri});
  153. }
  154. Future<void> setAssetDataSource(String name, String package) async {
  155. print("id = $textureId asset name = $name package = $package");
  156. var params = <String, dynamic>{
  157. "name": name,
  158. };
  159. if (package != null) {
  160. params["package"] = package;
  161. }
  162. await channel.invokeMethod("setAssetDataSource", params);
  163. }
  164. Future<void> setFileDataSource(String path) async {
  165. if (!File(path).existsSync()) {
  166. return Error.fileNotExists;
  167. }
  168. await channel.invokeMethod("setFileDataSource", <String, dynamic>{
  169. "path": path,
  170. });
  171. print("id = $textureId file path = $path");
  172. }
  173. Future<Map<String, dynamic>> getInfo() async {
  174. var map = await channel.invokeMethod("getInfo");
  175. if (map == null) {
  176. return null;
  177. } else {
  178. return map.cast<String, dynamic>();
  179. }
  180. }
  181. Future<void> seekTo(double target) async {
  182. await channel.invokeMethod("seekTo", <String, dynamic>{
  183. "target": target,
  184. });
  185. }
  186. }