controller.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. Future<void> _initIjk() async {
  9. try {
  10. var id = await createIjk();
  11. this.textureId = id;
  12. _plugin = _IjkPlugin(id);
  13. } catch (e) {
  14. print(e);
  15. print("初始化失败");
  16. }
  17. }
  18. void dispose() {
  19. this.textureId = null;
  20. this.notifyListeners();
  21. _plugin?.dispose();
  22. super.dispose();
  23. }
  24. Future<void> setNetworkDataSource(String url) async {
  25. if (this.textureId != null) {
  26. await _plugin?.dispose();
  27. }
  28. await _initIjk();
  29. await _plugin?.setNetworkDataSource(uri: url);
  30. this.notifyListeners();
  31. }
  32. Future<void> setAssetDataSource(String name, {String package}) async {
  33. if (this.textureId != null) {
  34. await _plugin?.dispose();
  35. }
  36. await _initIjk();
  37. await _plugin?.setAssetDataSource(name, package);
  38. this.notifyListeners();
  39. }
  40. Future<void> play() async {
  41. await _plugin?.play();
  42. this.notifyListeners();
  43. }
  44. }
  45. const MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
  46. Future<int> createIjk() async {
  47. int id = await _globalChannel.invokeMethod("create");
  48. return id;
  49. }
  50. class _IjkPlugin {
  51. MethodChannel get channel => MethodChannel("top.kikt/ijkplayer/$textureId");
  52. int textureId;
  53. _IjkPlugin(this.textureId);
  54. Future<void> dispose() async {
  55. _globalChannel.invokeMethod("dispose", {"id": textureId});
  56. }
  57. Future<void> play() async {
  58. await channel.invokeMethod("play");
  59. }
  60. Future<void> pause() async {
  61. channel.invokeMethod("pause");
  62. }
  63. Future<void> stop() async {
  64. channel.invokeMethod("stop");
  65. }
  66. Future<void> setNetworkDataSource({String uri}) async {
  67. // todo
  68. print("id = $textureId net uri = $uri");
  69. channel.invokeMethod("setNetworkDataSource", {"uri": uri});
  70. }
  71. Future<void> setAssetDataSource(String name, String package) async {
  72. print("id = $textureId asset name = $name package = $package");
  73. var params = <String, dynamic>{
  74. "name": name,
  75. };
  76. if (package != null) {
  77. params["package"] = package;
  78. }
  79. channel.invokeMethod("setAssetDataSource", params);
  80. }
  81. Future<void> setFileDataSource(String path) async {
  82. if (!File(path).existsSync()) {
  83. return Error.fileNotExists;
  84. }
  85. channel.invokeMethod("setFileDataSource", <String, dynamic>{
  86. "path": path,
  87. });
  88. // todo
  89. print("id = $textureId file path = $path");
  90. }
  91. }