controller.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. part of './ijkplayer.dart';
  2. /// Media Controller
  3. class IjkMediaController {
  4. /// MediaController
  5. IjkMediaController({
  6. this.autoRotate = true,
  7. }) {
  8. index = IjkMediaPlayerManager().add(this);
  9. }
  10. int index;
  11. String get debugLabel => index.toString();
  12. /// texture id from native
  13. int _textureId;
  14. /// It will automatically correct the direction of the video.
  15. bool autoRotate;
  16. /// texture id from native
  17. int get textureId => _textureId;
  18. /// set texture id, Normally the user does not call
  19. set textureId(int id) {
  20. _textureId = id;
  21. _textureIdController.add(id);
  22. }
  23. /// on texture id change
  24. StreamController<int> _textureIdController = StreamController.broadcast();
  25. /// on texture id change
  26. Stream<int> get textureIdStream => _textureIdController?.stream;
  27. /// Channel of flutter and native.
  28. _IjkPlugin _plugin;
  29. /// Whether texture id is null
  30. bool get isInit => textureId == null;
  31. /// channel of native to flutter
  32. _IJKEventChannel eventChannel;
  33. /// playing state
  34. bool _isPlaying = false;
  35. /// playing state
  36. bool get isPlaying => _isPlaying == true;
  37. /// playing state
  38. set isPlaying(bool value) {
  39. this._isPlaying = value;
  40. _playingController?.add(value);
  41. }
  42. /// playing state stream controller
  43. StreamController<bool> _playingController = StreamController.broadcast();
  44. /// playing state stream
  45. Stream<bool> get playingStream => _playingController?.stream;
  46. /// video info stream controller
  47. StreamController<VideoInfo> _videoInfoController =
  48. StreamController.broadcast();
  49. /// video info stream
  50. Stream<VideoInfo> get videoInfoStream => _videoInfoController?.stream;
  51. /// video volume stream controller
  52. StreamController<int> _volumeController = StreamController.broadcast();
  53. /// video volume stream
  54. Stream<int> get volumeStream => _volumeController?.stream;
  55. /// video volume, not system volume
  56. int _volume = 100;
  57. /// video volume, not system volume
  58. set volume(int value) {
  59. if (value > 100) {
  60. value = 100;
  61. } else if (value < 0) {
  62. value = 0;
  63. }
  64. this._volume = value;
  65. _volumeController?.add(value);
  66. _setVolume(value);
  67. }
  68. /// video volume, not system volume
  69. int get volume => _volume;
  70. /// create ijk texture id from native
  71. Future<void> _initIjk() async {
  72. try {
  73. var id = await _createIjk();
  74. this.textureId = id;
  75. _plugin = _IjkPlugin(id);
  76. eventChannel = _IJKEventChannel(this);
  77. await eventChannel.init();
  78. volume = 100;
  79. } catch (e) {
  80. LogUtils.warning(e);
  81. LogUtils.warning("初始化失败");
  82. }
  83. }
  84. /// [reset] and close all controller
  85. void dispose() async {
  86. await reset();
  87. _playingController?.close();
  88. _videoInfoController?.close();
  89. _textureIdController?.close();
  90. _volumeController?.close();
  91. _playingController = null;
  92. _videoInfoController = null;
  93. _textureIdController = null;
  94. _volumeController = null;
  95. IjkMediaPlayerManager().remove(this);
  96. }
  97. /// dispose all resource
  98. Future<void> reset() async {
  99. volume = 100;
  100. this.textureId = null;
  101. _plugin?.dispose();
  102. _plugin = null;
  103. eventChannel?.dispose();
  104. eventChannel = null;
  105. }
  106. /// set net DataSource
  107. Future<void> setNetworkDataSource(
  108. String url, {
  109. Map<String, String> headers = const {},
  110. bool autoPlay = false,
  111. }) async {
  112. await _initDataSource(() async {
  113. await _plugin?.setNetworkDataSource(
  114. uri: url,
  115. headers: headers,
  116. );
  117. }, autoPlay);
  118. }
  119. /// set asset DataSource
  120. Future<void> setAssetDataSource(
  121. String name, {
  122. String package,
  123. bool autoPlay = false,
  124. }) async {
  125. await _initDataSource(() async {
  126. await _plugin?.setAssetDataSource(name, package);
  127. }, autoPlay);
  128. }
  129. /// Set datasource with [DataSource]
  130. Future<void> setDataSource(
  131. DataSource source, {
  132. bool autoPlay = false,
  133. }) async {
  134. switch (source._type) {
  135. case DataSourceType.asset:
  136. await setAssetDataSource(
  137. source._assetName,
  138. package: source._assetPackage,
  139. autoPlay: autoPlay,
  140. );
  141. break;
  142. case DataSourceType.file:
  143. await setFileDataSource(
  144. source._file,
  145. autoPlay: autoPlay,
  146. );
  147. break;
  148. case DataSourceType.network:
  149. await setNetworkDataSource(
  150. source._netWorkUrl,
  151. headers: source._headers,
  152. autoPlay: autoPlay,
  153. );
  154. break;
  155. default:
  156. }
  157. }
  158. /// set file DataSource
  159. Future<void> setFileDataSource(
  160. File file, {
  161. bool autoPlay = false,
  162. }) async {
  163. await _initDataSource(() async {
  164. await _plugin?.setFileDataSource(file.absolute.path);
  165. }, autoPlay);
  166. }
  167. /// dispose last textureId resource
  168. Future<void> _initDataSource(
  169. Future setDataSource(),
  170. bool autoPlay,
  171. ) async {
  172. autoPlay ??= false;
  173. if (this.textureId != null) {
  174. await _plugin?.dispose();
  175. }
  176. await _initIjk();
  177. Future playFuture = _autoPlay(autoPlay);
  178. await setDataSource();
  179. return playFuture;
  180. }
  181. /// Play or pause according to your current status
  182. Future<void> playOrPause({
  183. pauseOther = false,
  184. }) async {
  185. var videoInfo = await getVideoInfo();
  186. var playing = videoInfo.isPlaying;
  187. if (playing) {
  188. await pause();
  189. } else {
  190. await play(pauseOther: pauseOther);
  191. }
  192. }
  193. /// play media
  194. Future<void> play({
  195. pauseOther = false,
  196. }) async {
  197. if (pauseOther) {
  198. await pauseOtherController();
  199. }
  200. LogUtils.info("$this play");
  201. await _plugin?.play();
  202. refreshVideoInfo();
  203. }
  204. /// pause media
  205. Future<void> pause() async {
  206. LogUtils.info("$this pause");
  207. await _plugin?.pause();
  208. refreshVideoInfo();
  209. }
  210. /// seek to second
  211. ///
  212. /// [target] unit is second
  213. Future<void> seekTo(double target) async {
  214. await _plugin?.seekTo(target);
  215. refreshVideoInfo();
  216. }
  217. Future<void> seekToProgress(double progress) async {
  218. var videoInfo = await getVideoInfo();
  219. var target = videoInfo.duration * progress;
  220. await this.seekTo(target);
  221. refreshVideoInfo();
  222. }
  223. /// get video info from native
  224. Future<VideoInfo> getVideoInfo() async {
  225. Map<String, dynamic> result = await _plugin?.getInfo();
  226. var info = VideoInfo.fromMap(result);
  227. return info;
  228. }
  229. /// request info and notify
  230. Future<void> refreshVideoInfo() async {
  231. var info = await getVideoInfo();
  232. isPlaying = info.isPlaying;
  233. if (info.hasData) {
  234. _videoInfoController?.add(info);
  235. LogUtils.verbose("onrefreshInfo = $info");
  236. }
  237. }
  238. /// AutoPlay use
  239. Future<void> _autoPlay(bool autoPlay) async {
  240. if (autoPlay) {
  241. await eventChannel?.autoPlay(this);
  242. } else {
  243. await eventChannel?.disableAutoPlay(this);
  244. }
  245. }
  246. /// set video volume
  247. Future<void> _setVolume(int volume) async {
  248. await _plugin?.setVolume(volume);
  249. }
  250. /// [pause] and [seekTo] 0
  251. Future<void> stop() async {
  252. // await _plugin?.stop();
  253. // refreshVideoInfo();
  254. await _plugin?.pause();
  255. await _plugin?.seekTo(0);
  256. refreshVideoInfo();
  257. }
  258. /// get system volume
  259. Future<int> getSystemVolume() async {
  260. return IjkManager.getSystemVolume();
  261. }
  262. /// set system volume
  263. Future<void> setSystemVolume(int volume) async {
  264. await IjkManager.setSystemVolume(volume);
  265. }
  266. Future<void> pauseOtherController() async {
  267. await IjkMediaPlayerManager().pauseOther(this);
  268. }
  269. @override
  270. String toString() {
  271. return "IJKController[$index]";
  272. }
  273. }
  274. /// about channel
  275. MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
  276. Future<int> _createIjk() async {
  277. int id = await _globalChannel.invokeMethod("create");
  278. return id;
  279. }
  280. class _IjkPlugin {
  281. MethodChannel get channel => MethodChannel("top.kikt/ijkplayer/$textureId");
  282. /// texture id
  283. int textureId;
  284. _IjkPlugin(this.textureId);
  285. Future<void> dispose() async {
  286. await _globalChannel.invokeMethod("dispose", {"id": textureId});
  287. }
  288. Future<void> play() async {
  289. await channel.invokeMethod("play");
  290. }
  291. Future<void> pause() async {
  292. await channel.invokeMethod("pause");
  293. }
  294. Future<void> stop() async {
  295. await channel.invokeMethod("stop");
  296. }
  297. Future<void> setNetworkDataSource(
  298. {String uri, Map<String, String> headers = const {}}) async {
  299. LogUtils.debug("id = $textureId net uri = $uri ,headers = $headers");
  300. await channel.invokeMethod("setNetworkDataSource", <String, dynamic>{
  301. "uri": uri,
  302. "headers": headers,
  303. });
  304. }
  305. Future<void> setAssetDataSource(String name, String package) async {
  306. LogUtils.debug("id = $textureId asset name = $name package = $package");
  307. var params = <String, dynamic>{
  308. "name": name,
  309. };
  310. if (package != null) {
  311. params["package"] = package;
  312. }
  313. await channel.invokeMethod("setAssetDataSource", params);
  314. }
  315. Future<void> setFileDataSource(String path) async {
  316. if (!File(path).existsSync()) {
  317. return Error.fileNotExists;
  318. }
  319. await channel.invokeMethod("setFileDataSource", <String, dynamic>{
  320. "path": path,
  321. });
  322. LogUtils.debug("id = $textureId file path = $path");
  323. }
  324. Future<Map<String, dynamic>> getInfo() async {
  325. var map = await channel.invokeMethod("getInfo");
  326. if (map == null) {
  327. return null;
  328. } else {
  329. return map.cast<String, dynamic>();
  330. }
  331. }
  332. Future<void> seekTo(double target) async {
  333. await channel.invokeMethod("seekTo", <String, dynamic>{
  334. "target": target,
  335. });
  336. }
  337. Future<void> setVolume(int volume) async {
  338. await channel.invokeMethod("setVolume", <String, dynamic>{
  339. "volume": volume,
  340. });
  341. }
  342. }
  343. /// Entity classe for data sources.
  344. class DataSource {
  345. /// See [DataSourceType]
  346. DataSourceType _type;
  347. File _file;
  348. String _assetName;
  349. String _assetPackage;
  350. String _netWorkUrl;
  351. Map<String, String> _headers;
  352. DataSource._();
  353. /// Create file data source
  354. factory DataSource.file(File file) {
  355. var ds = DataSource._();
  356. ds._file = file;
  357. ds._type = DataSourceType.file;
  358. return ds;
  359. }
  360. /// Create network data source
  361. factory DataSource.network(String url,
  362. {Map<String, String> headers = const {}}) {
  363. var ds = DataSource._();
  364. ds._netWorkUrl = url;
  365. ds._headers = headers;
  366. ds._type = DataSourceType.network;
  367. return ds;
  368. }
  369. /// Create asset data source
  370. factory DataSource.asset(String assetName, {String package}) {
  371. var ds = DataSource._();
  372. ds._assetName = assetName;
  373. ds._assetPackage = package;
  374. ds._type = DataSourceType.asset;
  375. return ds;
  376. }
  377. }
  378. enum DataSourceType {
  379. network,
  380. file,
  381. asset,
  382. }