flutter_aliplayer 5.4.3-dev.2 copy "flutter_aliplayer: ^5.4.3-dev.2" to clipboard
flutter_aliplayer: ^5.4.3-dev.2 copied to clipboard

outdated

Aliyun Player With Flutter

flutter_aliplayer #

A new flutter plugin project , The project supports Android and iOS base on AliyunPlayerSDK Android SDK URL iOS SDK URL

Installation #

dependencies:
  flutter_aliplayer: ^{{latest version}}

说明 #

flutter 播放器在原生层是基于 Android 播放器 SDK 和 iOS 播放器 SDK 的。可能部分原生播放器 SDK 的接口有遗漏,在 flutter 播放器插件中没有暴露出来。目前我们已将源码提供出来,开发者可以自行添加。也可以通知给我们,我们会对遗漏的接口进行补充。

播放控制 1.播放流程

  ///1、创建播放器
  FlutterAliplayer fAliplayer = FlutterAliPlayerFactory.createAliPlayer();
  ///多实例播放器创建方式,需要 flutter 层管理 playerId,其他接口一样,在播放器的回调中会返回对应的 playerId 来通知 flutter 层是哪一个播放器对象
  //FlutterAliplayer fAliplayer = FlutterAliPlayerFactory.createAliPlayer(playerId: playerId);

  ///2、设置监听,只列举了部分接口,更多接口可以参考播放器 Android\iOS 接口文档
  ///准备成功
  fAliplayer.setOnPrepared((playerId) {});
  ///首帧显示
  fAliplayer.setOnRenderingStart((playerId) {});
  ///视频宽高变化
  fAliplayer.setOnVideoSizeChanged((width, height,playerId) {});
  ///播放器状态变化
  fAliplayer.setOnStateChanged((newState,playerId) {});
  ///加载状态
  fAliplayer.setOnLoadingStatusListener(
      loadingBegin: (playerId) {},
      loadingProgress: (percent, netSpeed,playerId) {},
      loadingEnd: (playerId) {});
  ///拖动完成
  fAliplayer.setOnSeekComplete((playerId) {});
  ///播放器事件信息回调,包括 buffer、当前播放进度 等等信息,根据 infoCode 来判断,对应 FlutterAvpdef.infoCode
  fAliplayer.setOnInfo((infoCode, extraValue, extraMsg,playerId) {});
  ///播放完成
  fAliplayer.setOnCompletion((playerId) {});
  ///设置流准备完成
  fAliplayer.setOnTrackReady((playerId) {});
  ///截图结果
  fAliplayer.setOnSnapShot((path,playerId) {});
  ///错误结果
  fAliplayer.setOnError((errorCode, errorExtra, errorMsg,playerId) {});
  ///切换流变化
  fAliplayer.setOnTrackChanged((value,playerId) {});

  ///3、设置渲染的 View
  @override
  Widget build(BuildContext context) {
    var x = 0.0;
    var y = 0.0;
    Orientation orientation = MediaQuery.of(context).orientation;
    var width = MediaQuery.of(context).size.width;

    var height;
    if (orientation == Orientation.portrait) {
      height = width * 9.0 / 16.0;
    } else {
      height = MediaQuery.of(context).size.height;
    }
    AliPlayerView aliPlayerView = AliPlayerView(
        onCreated: onViewPlayerCreated,
        x: x,
        y: y,
        width: width,
        height: height);
    return OrientationBuilder(
      builder: (BuildContext context, Orientation orientation) {
        return Scaffold(
          body: Column(
            children: [
              Container(
                  color: Colors.black,
                  child: aliPlayerView,
                  width: width,
                  height: height),
            ],
          ),
        );
      },
    );
  }

    ///4、设置播放源
    ///说明:STS 播放方式,vid、region、accessKeyId、accessKeySecret、securityToken 为必填,其他参数可选
    ///     AUTH 播放方式,vid、region、playAuth 为必填,其他参数可选
    /// 每个 Map 对应的key 在 flutter 的 Demo 的 config.dart 中查看,fAliplayer 为播放器对象,如果还未创建,参考后续文档创建播放器
  void onViewPlayerCreated(viewId) async {
    ///将 渲染 View 设置给播放器
    fAliplayer.setPlayerView(viewId);
    //设置播放源
    switch (_playMode) {
      //URL 播放方式
      case ModeType.URL:
        this.fAliplayer.setUrl(_dataSourceMap[DataSourceRelated.URL_KEY]);
        break;
      //STS 播放方式
      case ModeType.STS:
        this.fAliplayer.setVidSts(
            vid: _dataSourceMap[DataSourceRelated.VID_KEY],
            region: _dataSourceMap[DataSourceRelated.REGION_KEY],
            accessKeyId: _dataSourceMap[DataSourceRelated.ACCESSKEYID_KEY],
            accessKeySecret:
                _dataSourceMap[DataSourceRelated.ACCESSKEYSECRET_KEY],
            securityToken: _dataSourceMap[DataSourceRelated.SECURITYTOKEN_KEY],
            definitionList: _dataSourceMap[DataSourceRelated.DEFINITION_LIST],
            previewTime: _dataSourceMap[DataSourceRelated.PREVIEWTIME_KEY]);
        break;
      //AUTH 播放方式
      case ModeType.AUTH:
        this.fAliplayer.setVidAuth(
            vid: _dataSourceMap[DataSourceRelated.VID_KEY],
            region: _dataSourceMap[DataSourceRelated.REGION_KEY],
            playAuth: _dataSourceMap[DataSourceRelated.PLAYAUTH_KEY],
            definitionList: _dataSourceMap[DataSourceRelated.DEFINITION_LIST],
            previewTime: _dataSourceMap[DataSourceRelated.PREVIEWTIME_KEY]);
        break;
      default:
    }
  }

  ///可选步骤:开启自动播放,默认关闭
  fAliplayer.setAutoPlay(true);
  ///5、prepare
  fAliplayer.prepare();
  ///说明:如果开启了自动播放,则调用 prepare 后即可,播放器在 prepare 成功后会自动播放,如果未开启自动播放,则需要在 setOnPrepard() 准备成功回调中,调用 fAliplayer.play() 开始播放

2.暂停播放
```dart
  ///暂停播放后,恢复播放直接调用 play 即可
  fAliplayer.pause();

3.停止播放
```dart
  ///停止播放后,恢复播放需要重新走一遍播放流程:prepare --> play
  fAliplayer.stop();

4.销毁
```dart
  fAliplayer.release();

5.seek
```dart
  ///seekMode 可选值:FlutterAvpdef.ACCURATE(精准seek) 和 FlutterAvpdef.INACCURATE(非精准seek)
  fAliplayer.seek(position,seekMode);

6.循环播放
```dart
  fAliplayer.setLoop(true);

7.静音、音量控制
```dart
  fAliplayer.setMute(true);
  ///设置播放器音量,范围0~1.
  fAliPlayer.setVolume(1);

8.倍速播放
```dart
  ///可选值:0.5,1.0,1.5,2.0
  fAliplayer.setRate(1.0);

9.切换多码率,自动码率切换
```dart
  ///在prepare成功之后,通过getMediaInfo可以获取到各个码流的信息,即TrackInfo
  fAliplayer.getMediaInfo().then((value) {
    //value 为 map,value['tracks'] 可以获取对应的 TrackInfos 列表信息,可以参考 Demo 中 AVPMediaInfo info = AVPMediaInfo.fromJson(value); 如何解析 TrackInfo
  };
  ///在播放过程中,可以通过调用播放器的selectTrack方法切换播放的码流,参数为 TrackInfo 中的 trackIndex,切换的结果会在OnTrackChangedListener监听之后会回调
  fAliplayer.selectTrack(index);
  
  ///自动码率切换
  fAliplayer.selectTrack(-1);

10.画面旋转、填充、镜像操作
```dart
  //设置画面的镜像模式:水平镜像,垂直镜像,无镜像。
  fAliplayer.setMirrorMode(FlutterAvpdef.AVP_MIRRORMODE_NONE);
  //设置画面旋转模式:旋转0度,90度,180度,270度
  fAliplayer.setRotateMode(FlutterAvpdef.AVP_ROTATE_0);
  //设置画面缩放模式:宽高比填充,宽高比适应,拉伸填充
  fAliplayer.setScalingMode(FlutterAvpdef.AVP_SCALINGMODE_SCALETOFILL);


边播边缓存
  需要在 prepare 之前设置给播放器
```dart
var map = {
  "mMaxSizeMB": _mMaxSizeMBController.text,///缓存目录的最大占用空间
  "mMaxDurationS": _mMaxDurationSController.text,///设置能够缓存的单个文件的最大时长
  "mDir": _mDirController.text,///缓存目录
  "mEnable": mEnableCacheConfig,///是否开启缓存功能
};
fAliplayer.setCacheConfig(map);


播放器其他配置
  需要在 prepare 之前设置给播放器
```dart
var configMap = {
  'mStartBufferDuration':_mStartBufferDurationController.text,///起播缓冲区时长
  'mHighBufferDuratio':_mHighBufferDurationController.text,///高缓冲时长
  'mMaxBufferDuration':_mMaxBufferDurationController.text,///最大缓冲区时长
  'mMaxDelayTime': _mMaxDelayTimeController.text,///最大延迟。注意:直播有效
  'mNetworkTimeout': _mNetworkTimeoutController.text,///网络超时时间
  'mNetworkRetryCount':_mNetworkRetryCountController.text,///网络重试次数
  'mMaxProbeSize': _mMaxProbeSizeController.text,///最大probe大小
  'mReferrer': _mReferrerController.text,///referrer
  'mHttpProxy': _mHttpProxyController.text,///http代理
  'mEnableSEI': mEnableSEI,///是否启用SEI
  'mClearFrameWhenStop': !mShowFrameWhenStop,///停止后是否清空画面
  'mDisableVideo': mDisableVideo,///禁用Video
  'mDisableAudio': mDisableAudio///禁用Audio
};
widget.fAliplayer.setConfig(configMap);
  


列表播放器
  1.创建列表播放器
```dart
FlutterAliListPlayer fAliListPlayer = FlutterAliPlayerFactory.createAliListPlayer();

  2.添加资源、移除资源
    列表播放器目前只支持两种播放方式,URL 和 STS
```dart
  ///uid是视频的唯一标志。用于区分视频是否一样。如果uid一样,则认为是一样的
  fAliListPlayer.addUrlSource(url,uid);
  fAliListPlayer.addVidSource(vid,uid);
  fAliListPlayer.removeSource(uid);

  3.设置预加载个数
```dart
  fAliListPlayer.setPreloadCount(count);

  4.播放视频源
```dart
  ///uid 为必填项,如果是 URL 播放方式,只需要 uid 即可,如果是 STS 方式,则需要填写 STS 信息
  fAliListPlayer.moveTo();


视频下载
  可选步骤,如果是安全下载,需要配置自己的加密校验文件到 SDK 中,普通下载则不需要
```dart
  FlutterAliPlayerFactory.initService(byteData);

  1.创建下载器
```dart
  FlutterAliDownloader donwloader = FlutterAliDownloader.init();
  ///设置保存路径
  donwloader.setSaveDir(path)

  2.开始下载
```dart
  ///1.prepare
  ///参数说明:type 可选值为 FlutterAvpdef.DOWNLOADTYPE_STS / FlutterAvpdef.DOWNLOADTYPE_AUTH 。当 type 为 DOWNLOADTYPE_STS 时候,必填参数为: {vid,accessKeyId,accessKeySecret,securityToken},当 type 为 DOWNLOADTYPE_AUTH 时,必须填参数为 {vid,playAuth}
  downloader.prepare(type, vid).then((value) {
      //value 为 map,对应 Demo 中的 DownloadModel 自定义下载类
      DownloadModel downloadModel = DownloadModel.fromJson(value);
      //2.selectItem,根据不同的 trackInfo 来确定需要下载哪个清晰度
      List<TrackInfoModel> trackInfos = downloadModel.trackInfos;
      downloader.selectItem(vid,trackInfos[0].index);
      //3.start
      downloader.start(vid, trackInfos[0].index).listen((event) {
        //说明:event 可能会有多种信息,可参考 FlutterAvpdef.EventChanneldef 中的信息,以下为具体说明:
        if (event[EventChanneldef.TYPE_KEY] == EventChanneldef.DOWNLOAD_PROGRESS){
            //下载进度百分比信息,获取下载进度百分比:event[EventChanneldef.DOWNLOAD_PROGRESS]
        }else if(event[EventChanneldef.TYPE_KEY] == EventChanneldef.DOWNLOAD_PROCESS){
            //处理进度百分比信息,获取处理进度百分比:event[EventChanneldef.DOWNLOAD_PROCESS]
        }else if(event[EventChanneldef.TYPE_KEY] == EventChanneldef.DOWNLOAD_COMPLETION){
            //下载完成,可以通过 event['vid']、event['index'] 获取对应的 vid 和 index 用于判断是哪个视频下载完成,event['savePath'] 用于获取下载完成视频的本地路径
        }else if(event[EventChanneldef.TYPE_KEY] == EventChanneldef.DOWNLOAD_ERROR){
            //下载失败,可以通过 event['vid']、event['index'] 获取对应的 vid 和 index 用于判断是哪个视频下载失败,event['errorCode']、event['errorMsg'] 可以获取对应的错误码,和错误信息
        }
      });
  });

  3.停止下载、删除和释放
```dart
  downloader.stop(vid, index)
  //可以删除下载的本地文件
  downloader.delete(vid, index)
  downloader.release(vid, index)


18
likes
0
pub points
83%
popularity

Publisher

unverified uploader

Aliyun Player With Flutter

Homepage

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_aliplayer