controller.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. part of './ijkplayer.dart';
  2. /// Media Controller
  3. class IjkMediaController extends ChangeNotifier {
  4. /// textureId
  5. int textureId;
  6. bool get isInit => textureId == null;
  7. Future _initIjk() async {
  8. try {
  9. var id = await _IjkPlugin.createIjk();
  10. this.textureId = id;
  11. } catch (e) {
  12. print(e);
  13. print("初始化失败");
  14. }
  15. }
  16. void dispose() {
  17. var id = textureId;
  18. this.textureId = null;
  19. this.notifyListeners();
  20. super.dispose();
  21. _IjkPlugin.dispose(id);
  22. }
  23. Future setDataSource(String url) async {
  24. if (this.textureId != null) {
  25. await _IjkPlugin.dispose(this.textureId);
  26. }
  27. await _initIjk();
  28. await _IjkPlugin.setDataSource(id: this.textureId, uri: url);
  29. this.notifyListeners();
  30. }
  31. Future play() async {
  32. await _IjkPlugin.play(this.textureId);
  33. this.notifyListeners();
  34. }
  35. }
  36. class _IjkPlugin {
  37. static MethodChannel channel = MethodChannel("top.kikt/ijkplayer");
  38. static Future<int> createIjk() async {
  39. int id = await channel.invokeMethod("create");
  40. return id;
  41. }
  42. static Future dispose(int id) async {
  43. channel.invokeMethod("dispose", id);
  44. }
  45. static Future play(int id) async {
  46. await channel.invokeMethod("play", id);
  47. }
  48. static Future pause(int id) async {
  49. channel.invokeMethod("pause", id);
  50. }
  51. static Future stop(int id) async {
  52. channel.invokeMethod("stop", id);
  53. }
  54. static Future setDataSource({int id, String uri}) async {
  55. print("id = $id , uri = $uri");
  56. channel.invokeMethod("setDataSource", {"id": id, "uri": uri});
  57. }
  58. }