part of '../ijkplayer.dart'; /// Media Controller class IjkMediaController with IjkMediaControllerMixin, IjkMediaControllerStreamMixin { /// It will automatically correct the direction of the video. bool autoRotate; int index; String get debugLabel => index.toString(); /// MediaController IjkMediaController({ this.autoRotate = true, }) { index = IjkMediaPlayerManager().add(this); } /// create ijk texture id from native Future _initIjk() async { try { var id = await _createIjk(); this.textureId = id; _plugin = _IjkPlugin(id); eventChannel = _IJKEventChannel(this); await eventChannel.init(); volume = 100; } catch (e) { LogUtils.warning(e); LogUtils.warning("初始化失败"); } } /// [reset] and close all controller void dispose() async { await reset(); await _disposeStream(); IjkMediaPlayerManager().remove(this); } /// dispose all resource Future reset() async { volume = 100; this.textureId = null; _plugin?.dispose(); _plugin = null; eventChannel?.dispose(); eventChannel = null; _ijkStatus = IjkStatus.noDatasource; } /// set net DataSource Future setNetworkDataSource( String url, { Map headers = const {}, bool autoPlay = false, }) async { _ijkStatus = IjkStatus.preparing; await _initDataSource(() async { await _plugin?.setNetworkDataSource( uri: url, headers: headers, ); _ijkStatus = IjkStatus.prepared; }, autoPlay); } /// set asset DataSource Future setAssetDataSource( String name, { String package, bool autoPlay = false, }) async { _ijkStatus = IjkStatus.preparing; await _initDataSource(() async { await _plugin?.setAssetDataSource(name, package); _ijkStatus = IjkStatus.prepared; }, autoPlay); } /// Set datasource with [DataSource] Future setDataSource( DataSource source, { bool autoPlay = false, }) async { switch (source._type) { case DataSourceType.asset: await setAssetDataSource( source._assetName, package: source._assetPackage, autoPlay: autoPlay, ); break; case DataSourceType.file: await setFileDataSource( source._file, autoPlay: autoPlay, ); break; case DataSourceType.network: await setNetworkDataSource( source._netWorkUrl, headers: source._headers, autoPlay: autoPlay, ); break; default: } } /// set file DataSource Future setFileDataSource( File file, { bool autoPlay = false, }) async { _ijkStatus = IjkStatus.preparing; await _initDataSource(() async { await _plugin?.setFileDataSource(file.absolute.path); _ijkStatus = IjkStatus.prepared; }, autoPlay); } /// dispose last textureId resource Future _initDataSource( Future setDataSource(), bool autoPlay, ) async { autoPlay ??= false; if (this.textureId != null) { await _plugin?.dispose(); } await _initIjk(); Future playFuture = _autoPlay(autoPlay); await setDataSource(); return playFuture; } /// Play or pause according to your current status Future playOrPause({ pauseOther = false, }) async { var videoInfo = await getVideoInfo(); var playing = videoInfo.isPlaying; if (playing) { await pause(); } else { await play(pauseOther: pauseOther); } } /// play media Future play({ pauseOther = false, }) async { if (pauseOther) { await pauseOtherController(); } LogUtils.info("$this play"); await _plugin?.play(); refreshVideoInfo(); _ijkStatus = IjkStatus.playing; } /// pause media Future pause() async { LogUtils.info("$this pause"); await _plugin?.pause(); refreshVideoInfo(); _ijkStatus = IjkStatus.pause; } /// seek to second /// /// [target] unit is second Future seekTo(double target) async { await _plugin?.seekTo(target); _ijkStatus = IjkStatus.pause; refreshVideoInfo(); } /// seek to progress Future seekToProgress(double progress) async { var videoInfo = await getVideoInfo(); var target = videoInfo.duration * progress; await this.seekTo(target); refreshVideoInfo(); } /// get video info from native Future getVideoInfo() async { Map result = await _plugin?.getInfo(); var info = VideoInfo.fromMap(result); return info; } /// request info and notify Future refreshVideoInfo() async { var info = await getVideoInfo(); _videoInfo = info; isPlaying = info.isPlaying; if (info.hasData) { _videoInfoController?.add(info); LogUtils.verbose("onrefreshInfo = $info"); } } /// AutoPlay use Future _autoPlay(bool autoPlay) async { if (autoPlay) { await eventChannel?.autoPlay(this); } else { await eventChannel?.disableAutoPlay(this); } } /// set video volume Future _setVolume(int volume) async { await _plugin?.setVolume(volume); } /// [pause] and [seekTo] 0 Future stop() async { // await _plugin?.stop(); // refreshVideoInfo(); await _plugin?.pause(); await _plugin?.seekTo(0); refreshVideoInfo(); } /// get system volume Future getSystemVolume() async { return IjkManager.getSystemVolume(); } /// set system volume Future setSystemVolume(int volume) async { await IjkManager.setSystemVolume(volume); } /// Pause all other players. Future pauseOtherController() async { await IjkMediaPlayerManager().pauseOther(this); } @override String toString() { return "IJKController[$index]"; } Future hideSystemVolumeBar() async { await IjkManager.hideSystemVolumeBar(); } void _onPlayFinish() { isPlaying = videoInfo.isPlaying; refreshVideoInfo(); _playFinishController?.add(this); _ijkStatus = IjkStatus.complete; } /// Intercept the video frame image and get the `Uint8List` format. /// /// Player UI is not included. If you need the effect of the player, use the screenshot of the system. Future screenShot() { return _plugin.screenShot(); } }