controller.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. /// seek to progress
  218. Future<void> seekToProgress(double progress) async {
  219. var videoInfo = await getVideoInfo();
  220. var target = videoInfo.duration * progress;
  221. await this.seekTo(target);
  222. refreshVideoInfo();
  223. }
  224. /// get video info from native
  225. Future<VideoInfo> getVideoInfo() async {
  226. Map<String, dynamic> result = await _plugin?.getInfo();
  227. var info = VideoInfo.fromMap(result);
  228. return info;
  229. }
  230. /// request info and notify
  231. Future<void> refreshVideoInfo() async {
  232. var info = await getVideoInfo();
  233. isPlaying = info.isPlaying;
  234. if (info.hasData) {
  235. _videoInfoController?.add(info);
  236. LogUtils.verbose("onrefreshInfo = $info");
  237. }
  238. }
  239. /// AutoPlay use
  240. Future<void> _autoPlay(bool autoPlay) async {
  241. if (autoPlay) {
  242. await eventChannel?.autoPlay(this);
  243. } else {
  244. await eventChannel?.disableAutoPlay(this);
  245. }
  246. }
  247. /// set video volume
  248. Future<void> _setVolume(int volume) async {
  249. await _plugin?.setVolume(volume);
  250. }
  251. /// [pause] and [seekTo] 0
  252. Future<void> stop() async {
  253. // await _plugin?.stop();
  254. // refreshVideoInfo();
  255. await _plugin?.pause();
  256. await _plugin?.seekTo(0);
  257. refreshVideoInfo();
  258. }
  259. /// get system volume
  260. Future<int> getSystemVolume() async {
  261. return IjkManager.getSystemVolume();
  262. }
  263. /// set system volume
  264. Future<void> setSystemVolume(int volume) async {
  265. await IjkManager.setSystemVolume(volume);
  266. }
  267. Future<void> pauseOtherController() async {
  268. await IjkMediaPlayerManager().pauseOther(this);
  269. }
  270. @override
  271. String toString() {
  272. return "IJKController[$index]";
  273. }
  274. Future<void> hideSystemVolumeBar() async {
  275. await IjkManager.hideSystemVolumeBar();
  276. }
  277. }
  278. /// about channel
  279. MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
  280. Future<int> _createIjk() async {
  281. int id = await _globalChannel.invokeMethod("create");
  282. return id;
  283. }
  284. class _IjkPlugin {
  285. MethodChannel get channel => MethodChannel("top.kikt/ijkplayer/$textureId");
  286. /// texture id
  287. int textureId;
  288. _IjkPlugin(this.textureId);
  289. Future<void> dispose() async {
  290. await _globalChannel.invokeMethod("dispose", {"id": textureId});
  291. }
  292. Future<void> play() async {
  293. await channel.invokeMethod("play");
  294. }
  295. Future<void> pause() async {
  296. await channel.invokeMethod("pause");
  297. }
  298. Future<void> stop() async {
  299. await channel.invokeMethod("stop");
  300. }
  301. Future<void> setNetworkDataSource(
  302. {String uri, Map<String, String> headers = const {}}) async {
  303. LogUtils.debug("id = $textureId net uri = $uri ,headers = $headers");
  304. await channel.invokeMethod("setNetworkDataSource", <String, dynamic>{
  305. "uri": uri,
  306. "headers": headers,
  307. });
  308. }
  309. Future<void> setAssetDataSource(String name, String package) async {
  310. LogUtils.debug("id = $textureId asset name = $name package = $package");
  311. var params = <String, dynamic>{
  312. "name": name,
  313. };
  314. if (package != null) {
  315. params["package"] = package;
  316. }
  317. await channel.invokeMethod("setAssetDataSource", params);
  318. }
  319. Future<void> setFileDataSource(String path) async {
  320. if (!File(path).existsSync()) {
  321. return Error.fileNotExists;
  322. }
  323. await channel.invokeMethod("setFileDataSource", <String, dynamic>{
  324. "path": path,
  325. });
  326. LogUtils.debug("id = $textureId file path = $path");
  327. }
  328. Future<Map<String, dynamic>> getInfo() async {
  329. var map = await channel.invokeMethod("getInfo");
  330. if (map == null) {
  331. return null;
  332. } else {
  333. return map.cast<String, dynamic>();
  334. }
  335. }
  336. Future<void> seekTo(double target) async {
  337. await channel.invokeMethod("seekTo", <String, dynamic>{
  338. "target": target,
  339. });
  340. }
  341. Future<void> setVolume(int volume) async {
  342. await channel.invokeMethod("setVolume", <String, dynamic>{
  343. "volume": volume,
  344. });
  345. }
  346. }
  347. /// Entity classe for data sources.
  348. class DataSource {
  349. /// See [DataSourceType]
  350. DataSourceType _type;
  351. File _file;
  352. String _assetName;
  353. String _assetPackage;
  354. String _netWorkUrl;
  355. Map<String, String> _headers;
  356. DataSource._();
  357. /// Create file data source
  358. factory DataSource.file(File file) {
  359. var ds = DataSource._();
  360. ds._file = file;
  361. ds._type = DataSourceType.file;
  362. return ds;
  363. }
  364. /// Create network data source
  365. factory DataSource.network(String url,
  366. {Map<String, String> headers = const {}}) {
  367. var ds = DataSource._();
  368. ds._netWorkUrl = url;
  369. ds._headers = headers;
  370. ds._type = DataSourceType.network;
  371. return ds;
  372. }
  373. /// Create asset data source
  374. factory DataSource.asset(String assetName, {String package}) {
  375. var ds = DataSource._();
  376. ds._assetName = assetName;
  377. ds._assetPackage = package;
  378. ds._type = DataSourceType.asset;
  379. return ds;
  380. }
  381. }
  382. enum DataSourceType {
  383. network,
  384. file,
  385. asset,
  386. }