controller.dart 4.4 KB

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