plugin.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. part of '../ijkplayer.dart';
  2. /// about channel
  3. MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
  4. Future<int> _createIjk({
  5. List<IjkOption> options,
  6. }) async {
  7. List<Map<String, dynamic>> _optionList = [];
  8. for (var option in options) {
  9. _optionList.add(option.toMap());
  10. }
  11. int id = await _globalChannel.invokeMethod(
  12. "create",
  13. <String, dynamic>{
  14. "options": _optionList,
  15. },
  16. );
  17. return id;
  18. }
  19. class _IjkPlugin {
  20. MethodChannel get channel => MethodChannel("top.kikt/ijkplayer/$textureId");
  21. /// texture id
  22. int textureId;
  23. _IjkPlugin(this.textureId);
  24. Future<void> dispose() async {
  25. await _globalChannel.invokeMethod("dispose", {"id": textureId});
  26. }
  27. Future<void> play() async {
  28. await channel.invokeMethod("play");
  29. }
  30. Future<void> pause() async {
  31. await channel.invokeMethod("pause");
  32. }
  33. Future<void> stop() async {
  34. await channel.invokeMethod("stop");
  35. }
  36. Future<void> setNetworkDataSource(
  37. {String uri, Map<String, String> headers = const {}}) async {
  38. LogUtils.debug("id = $textureId net uri = $uri ,headers = $headers");
  39. await channel.invokeMethod("setNetworkDataSource", <String, dynamic>{
  40. "uri": uri,
  41. "headers": headers,
  42. });
  43. }
  44. Future<void> setAssetDataSource(String name, String package) async {
  45. LogUtils.debug("id = $textureId asset name = $name package = $package");
  46. var params = <String, dynamic>{
  47. "name": name,
  48. };
  49. if (package != null) {
  50. params["package"] = package;
  51. }
  52. await channel.invokeMethod("setAssetDataSource", params);
  53. }
  54. Future<void> setFileDataSource(String path) async {
  55. if (!File(path).existsSync()) {
  56. return Error.fileNotExists;
  57. }
  58. await channel.invokeMethod("setFileDataSource", <String, dynamic>{
  59. "path": path,
  60. });
  61. LogUtils.debug("id = $textureId file path = $path");
  62. }
  63. Future<Map<String, dynamic>> getInfo() async {
  64. try {
  65. var map = await channel.invokeMethod("getInfo");
  66. if (map == null) {
  67. return null;
  68. } else {
  69. return map.cast<String, dynamic>();
  70. }
  71. } on Exception {
  72. return null;
  73. }
  74. }
  75. Future<void> seekTo(double target) async {
  76. await channel.invokeMethod("seekTo", <String, dynamic>{
  77. "target": target,
  78. });
  79. }
  80. ///
  81. Future<void> setVolume(int volume) async {
  82. await channel.invokeMethod("setVolume", <String, dynamic>{
  83. "volume": volume,
  84. });
  85. }
  86. Future<Uint8List> screenShot() async {
  87. var result = await channel.invokeMethod("screenShot");
  88. if (result == null) {
  89. return null;
  90. }
  91. return result;
  92. }
  93. }