| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- part of './ijkplayer.dart';
- /// Media Controller
- class IjkMediaController {
- IjkMediaController({
- this.autoRotate = true,
- });
- int _textureId;
- bool autoRotate;
- int get textureId => _textureId;
- set textureId(int id) {
- _textureId = id;
- _textureIdController.add(id);
- }
- StreamController<int> _textureIdController = StreamController.broadcast();
- Stream<int> get textureIdStream => _textureIdController.stream;
- _IjkPlugin _plugin;
- bool get isInit => textureId == null;
- IJKEventChannel eventChannel;
- bool _isPlaying = false;
- bool get isPlaying => _isPlaying == true;
- set isPlaying(bool value) {
- this._isPlaying = value;
- _playingController.add(value);
- }
- StreamController<bool> _playingController = StreamController.broadcast();
- Stream<bool> get playingStream => _playingController.stream;
- StreamController<VideoInfo> _videoInfoController =
- StreamController.broadcast();
- Stream<VideoInfo> get videoInfoStream => _videoInfoController.stream;
- StreamController<int> _volumeController = StreamController.broadcast();
- Stream<int> get volumeStream => _volumeController.stream;
- int _volume = 100;
- set volume(int value) {
- this._volume = value;
- _volumeController.add(volume);
- _setVolume(value);
- }
- int get volume => _volume;
- Future<void> _initIjk() async {
- try {
- var id = await _createIjk();
- this.textureId = id;
- _plugin = _IjkPlugin(id);
- eventChannel = IJKEventChannel(this);
- await eventChannel.init();
- volume = 100;
- } catch (e) {
- print(e);
- print("初始化失败");
- }
- }
- void dispose() async {
- await reset();
- _playingController.close();
- _videoInfoController.close();
- _textureIdController.close();
- _volumeController.close();
- }
- Future<void> reset() async {
- volume = 100;
- this.textureId = null;
- _plugin?.dispose();
- _plugin = null;
- eventChannel?.dispose();
- eventChannel = null;
- }
- Future<void> setNetworkDataSource(
- String url, {
- bool autoPlay = false,
- }) async {
- await _initDataSource(() async {
- await _plugin?.setNetworkDataSource(uri: url);
- }, autoPlay);
- }
- Future<void> setAssetDataSource(
- String name, {
- String package,
- bool autoPlay = false,
- }) async {
- await _initDataSource(() async {
- await _plugin?.setAssetDataSource(name, package);
- }, autoPlay);
- }
- Future<void> setFileDataSource(
- File file, {
- bool autoPlay = false,
- }) async {
- await _initDataSource(() async {
- await _plugin?.setFileDataSource(file.absolute.path);
- }, autoPlay);
- }
- Future<void> _initDataSource(
- Future setDataSource(),
- bool autoPlay,
- ) async {
- autoPlay ??= false;
- if (this.textureId != null) {
- await _plugin?.dispose();
- }
- await _initIjk();
- _autoPlay(autoPlay);
- await setDataSource();
- }
- Future<void> playOrPause() async {
- var videoInfo = await getVideoInfo();
- var playing = videoInfo.isPlaying;
- if (playing) {
- await _plugin?.pause();
- } else {
- await _plugin?.play();
- }
- refreshVideoInfo();
- }
- Future<void> play() async {
- await _plugin?.play();
- refreshVideoInfo();
- }
- Future<void> pause() async {
- await _plugin?.pause();
- refreshVideoInfo();
- }
- Future<void> seekTo(double target) async {
- await _plugin?.seekTo(target);
- refreshVideoInfo();
- }
- Future<VideoInfo> getVideoInfo() async {
- Map<String, dynamic> result = await _plugin?.getInfo();
- var info = VideoInfo.fromMap(result);
- return info;
- }
- Future<void> refreshVideoInfo() async {
- var info = await getVideoInfo();
- isPlaying = info.isPlaying;
- if (info.hasData) _videoInfoController?.add(info);
- print("info = $info");
- }
- void _autoPlay(bool autoPlay) {
- if (autoPlay) {
- eventChannel?.autoPlay(this);
- }
- }
- Future<void> _setVolume(int volume) async {
- await _plugin?.setVolume(volume);
- }
- Future<void> stop() async {
- // await _plugin?.stop();
- // refreshVideoInfo();
- await _plugin?.pause();
- await _plugin?.seekTo(0);
- refreshVideoInfo();
- }
- }
- /// about channel
- MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
- Future<int> _createIjk() async {
- int id = await _globalChannel.invokeMethod("create");
- return id;
- }
- /// For the hot reload/ hot restart to release last texture resource. Release version does not have hot reload, so you can not call it.
- ///
- /// release版本可不调用, 主要是为了释放hot restart/hot reload的资源,因为原生资源不参与热重载
- ///
- ///
- /// If this method is not invoked in the debug version, the sound before the hot reload will continue to play.
- Future<void> initIJKPlayer() async {
- _globalChannel.invokeMethod("init");
- }
- class _IjkPlugin {
- MethodChannel get channel => MethodChannel("top.kikt/ijkplayer/$textureId");
- int textureId;
- _IjkPlugin(this.textureId);
- Future<void> dispose() async {
- await _globalChannel.invokeMethod("dispose", {"id": textureId});
- }
- Future<void> play() async {
- await channel.invokeMethod("play");
- }
- Future<void> pause() async {
- await channel.invokeMethod("pause");
- }
- Future<void> stop() async {
- await channel.invokeMethod("stop");
- }
- Future<void> setNetworkDataSource({String uri}) async {
- print("id = $textureId net uri = $uri");
- await channel.invokeMethod("setNetworkDataSource", {"uri": uri});
- }
- Future<void> setAssetDataSource(String name, String package) async {
- print("id = $textureId asset name = $name package = $package");
- var params = <String, dynamic>{
- "name": name,
- };
- if (package != null) {
- params["package"] = package;
- }
- await channel.invokeMethod("setAssetDataSource", params);
- }
- Future<void> setFileDataSource(String path) async {
- if (!File(path).existsSync()) {
- return Error.fileNotExists;
- }
- await channel.invokeMethod("setFileDataSource", <String, dynamic>{
- "path": path,
- });
- print("id = $textureId file path = $path");
- }
- Future<Map<String, dynamic>> getInfo() async {
- var map = await channel.invokeMethod("getInfo");
- if (map == null) {
- return null;
- } else {
- return map.cast<String, dynamic>();
- }
- }
- Future<void> seekTo(double target) async {
- await channel.invokeMethod("seekTo", <String, dynamic>{
- "target": target,
- });
- }
- Future<void> setVolume(int volume) async {
- await channel.invokeMethod("setVolume", <String, dynamic>{
- "volume": volume,
- });
- }
- }
|