0 purchases
bjy playbackui flutter
bjy_playbackui_flutter #
flutter demo 示例工程
百家云回放和点播 SDK 的 flutter 插件。
1. 初始化 SDK #
在合适的时机初始化 SDK,建议在用户勾选完隐私政策之后,避免合规风险。确保进点播回放之前调用即可。
BJYPlaybackUIFlutterPlatform.instance.initSDK("e33180987");
copied to clipboard
2. 回放 #
普通回放
BJYPlaybackUIFlutterPlatform.instance.startPlayback("roomID", "roomToken", {
// 用户唯一标识
"userId": "853145204",
// 用户昵称
"userName": "yongjiaming",
// 是否支持循环播放
"supportLooping": true,
// 是否支持记忆播放,即下次播放从上次关闭的时间点续播
"supportBreakPointPlay": true
});
copied to clipboard
长期课回放,map 中必须要传 session_id
裁剪回放,map 中必须要传 clipedVersion
BJYPlaybackUIFlutterPlatform.instance.startPlayback("roomID", "roomToken", {
// 长期课必须要传 session_id
"session_id": "20240101",
"clipedVersion": "1",
// 用户唯一标识
"userId": "853145204",
// 用户昵称
"userName": "yongjiaming",
// 是否支持循环播放
"supportLooping": true,
// 是否支持记忆播放,即下次播放从上次关闭的时间点续播
"supportBreakPointPlay": true,
// 默认是否横屏播放
"isLandscape": true });
copied to clipboard
3. 点播(跳转原生播放页面) #
BJYPlaybackUIFlutterPlatform.instance.startVideo("videoId", "token", {
// 用户唯一标识
"userId": "853145204",
// 用户昵称
"userName": "yongjiaming",
// 是否支持循环播放
"supportLooping": true,
// 是否支持记忆播放,即下次播放从上次关闭的时间点续播
"supportBreakPointPlay": true,
// 默认是否横屏播放
"isLandscape": true });
copied to clipboard
4. 内嵌播放器 #
内嵌播放器视图,能方便嵌入到 flutter 进行页面自定义开发。
以下是 dart 创建内嵌播放器视图的代码,支持单个页面嵌入多个播放器视图。
4.1. 在线播放
在线播放,token 必须传,isLandscape 表示当前播放器是否处于横屏状态,用于更新全屏图标状态。
var playerWidget = buildNativeView("e33180987", "160491264",
"Vaac3j5n5hdjXPso7IvWENxo6c4wA6poVNb7GL8FoVwnHPIguunSCjG5JtrxIFp-", false);
copied to clipboard
4.2. 离线播放
离线播放,token 传空字符串,isOffline 传 true。同时需要保证下载状态是 Finish。
var playerWidget = buildNativeView("e33180987", "189522041",
"", false, isOffline: true);
copied to clipboard
播放器默认为 fit模式,保证画面完整显示,但可能会有黑边。
可在 buildNativeView 里通过 creationParams.putIfAbsent("aspectRatio", () => 1) 动态配置。
buildNativeView 的实现如下
Widget buildNativeView(String domain, String vid, String token, bool isLandscape, {bool isOffline = false}) {
// This is used in the platform side to register the view.
const String viewType = 'flutter_bjy_player_view';
// Pass parameters to the platform side.
final Map<String, dynamic> creationParams = <String, dynamic>{};
creationParams.putIfAbsent("customDomain", () => domain);
creationParams.putIfAbsent(
"playerConfig",
() => {
// 用户唯一标识
"userId": "853145204",
// 用户昵称
"userName": "yongjiaming",
// 是否支持循环播放
"supportLooping": false,
// 是否支持记忆播放,即下次播放从上次关闭的时间点续播
"supportBreakPointPlay": true,
"supportBackgroundAudio": false,
// 通知 VideoView 当前是否是横屏,涉及到全屏按钮样式
"isLandscape": isLandscape
});
creationParams.putIfAbsent("vid", () => vid);
creationParams.putIfAbsent("token", () => token);
creationParams.putIfAbsent("isOffline", () => isOffline);
// 设置视频view 的裁剪方式,0 fill,1 fit,默认 fit
// creationParams.putIfAbsent("aspectRatio", () => 1);
if (Platform.isIOS) {
return UiKitView(
viewType: viewType,
onPlatformViewCreated: (viewId) {
MethodChannel methodChannel =
MethodChannel('com.baijiayun.flutter.NativePlayerView_$viewId');
nativeMessageListener(methodChannel);
platforms.add(methodChannel);
},
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
}
return AndroidView(
viewType: viewType,
onPlatformViewCreated: (viewId) {
print('viewId:$viewId');
MethodChannel methodChannel =
MethodChannel('com.baijiayun.flutter.NativePlayerView_$viewId');
nativeMessageListener(methodChannel);
platforms.add(methodChannel);
},
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
}
copied to clipboard
4.3. 状态回调
methodChannel 监听播放器状态回调
switch (methodCall.method) {
// 点击播放器的全屏按钮
case "onToggleScreen":
if (methodCall.arguments == true) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
} else {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
break;
// 播放器状态回调
case "onPlayerStatus":
/**
* 出错
STATE_ERROR,
未初始化
STATE_IDLE,
初始化
STATE_INITIALIZED,
数据已准备好,待播放
STATE_PREPARED,
播放中
STATE_STARTED,
暂停状态
STATE_PAUSED,
终止状态(已释放播放器实例)
STATE_STOPPED,
播放结束
STATE_PLAYBACK_COMPLETED
*/
print("onPlayerStatus ${methodCall.arguments}");
break;
// 播放器播放进度回调
case "onPlayingTime":
print("onPlayingTime ${methodCall.arguments}");
break;
// 播放器出错回调
case "onError":
print("onError ${methodCall.arguments}");
break;
default:
break;
}
copied to clipboard
5. 下载 #
// 获取下载记录,返回对象为 JsonArray,每个 JsonObject 对应一条下载记录。
// 格式如下
/**
{
// 视频分辨率,-1未知类型;0标清;1高清;2超清;3,720P;4,1080P;5 音频
"definition": 2,
// 房间号,仅回放有值,点播为空
"roomId": "22112282539916",
// 0 点播下载类型,1 回放下载类型
"type": 1,
// 下载文件大小,单位 byte
"downloadedLength": 4016792,
// 下载状态,0初始状态;1下载中;2暂停;3出错;4完成;5已取消
"status": 4,
// 回放长期课专有
"sessionId": ”202307050“,
// 视频 id
"videoId": ”190660972“,
// 总文件大小
"totalLength": 4016792,
// 视频时长,单位秒
"duration": 1233
}
*/
BJYPlaybackUIFlutterPlatform.instance.getAllDownloadInfo()
// 下载点播视频
BJYPlaybackUIFlutterPlatform.instance.downloadVideo("videoID", "videoToken",
(downloadedLength, totalLength) {
print("progress: $downloadedLength/$totalLength");
}, (stateCode, message) {
print("state: $stateCode");
});
// 下载回放视频
BJYPlaybackUIFlutterPlatform.instance
.downloadPlayback("playbackClassID", "playbacksSessionID", "playbacksToken",
(downloadedLength, totalLength) {
print("progress: $downloadedLength/$totalLength");
}, (stateCode, message) {
print("state: $stateCode");
});
// 开始下载
BJYPlaybackUIFlutterPlatform.instance.startDownloadWithVideoID("videoID");
BJYPlaybackUIFlutterPlatform.instance.startDownloadWithRoomIDAndSessionID("roomID", "sessionID");
// 暂停下载
BJYPlaybackUIFlutterPlatform.instance.pauseDownloadWithVideoID("videoID");
BJYPlaybackUIFlutterPlatform.instance.pauseDownloadWithRoomIDAndSessionID("roomID", "sessionID");
// 取消并删除下载文件
BJYPlaybackUIFlutterPlatform.instance.cancelDownloadWithVideoID("videoID");
BJYPlaybackUIFlutterPlatform.instance.cancelDownloadWithRoomIDAndSessionID("roomID", "sessionID");
copied to clipboard
6. 离线点播回放 #
请确保 videoId/RoomId 有对应的下载记录,否则无法正常调起播放页面
// 播本地点播
BJYPlaybackUIFlutterPlatform.instance.startLocalVideo("videoID", {});
// 播本地回放
BJYPlaybackUIFlutterPlatform.instance.startLocalPlayback("roomID", "sessionID", {});
copied to clipboard
7. 监听播放器状态 (非内嵌集成方式) #
内嵌播放器集成下请使用 4.3 节的回调,直接跳转到原生播放页面使用本节的监听方式。
BJYPlaybackUIFlutterPlatform.instance.setPlayerListener(MyPlayerListener());
copied to clipboard
MyPlayerListener 如下
class MyPlayerListener implements PlayerListener {
@override
void onPlayerStatus(String playerStatus) {
// 处理播放器状态变化
print("MyPlayerListener onPlayerStatus playerStatus=${playerStatus}");
}
@override
void onError(int code, String errorDescription) {
// 处理播放器错误
print("MyPlayerListener onError code=${code}, errorDescription=${errorDescription}");
}
@override
void onPlayingTime(int currentTime, int totalTime) {
// 处理播放器时间变化
print("MyPlayerListener onPlayingTime currentTime=${currentTime}, totalTime=${totalTime}");
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.