引入插件
在项目的 pubspec.yaml 中添加依赖:
flutter pub add flutter_p2p_engine
iOS
- 在 ios/Podfile 添加 platform :ios, '13.0'
- 用 xcode 打开 ios/Runner.xcworkspace ,将 Minimum Developments Target 改成 13.0
macOS
- 编辑 macos/Runner/Release.entitlements & macos/Runner/DebugProfile.entitlements:
xml
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.app-sandbox</key>
<false/>
- 在 macos/Podfile 添加 platform :osx, '10.15'
- 用 xcode 打开 macos/Runner.xcworkspace ,将 Minimum Developments Target 改成 10.15
Android
- 编辑 android/app/src/main/AndroidManifest.xml ,增加如下权限声明:
xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
...
android:usesCleartextTraffic="true"
...
/>
TIP
以下设置用于 release 打包阶段, 开发阶段不需要
- 为了保证正常使用 SDK ,打包时请在 android/app/proguard-rules.pro 文件中添加以下代码:
-dontwarn com.p2pengine.**
-keep class com.p2pengine.**{*;}
-keep interface com.p2pengine.**{*;}
-keep class com.cdnbye.libdc.**{*;}
-keep interface com.cdnbye.libdc.**{*;}
-keep class com.snapchat.djinni.**{*;}
- 在 android/app/build.gradle 增加以下代码来引入 proguard 配置
buildTypes {
release {
...
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
示例
dart
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_p2p_engine/flutter_p2p_engine.dart';
// Init p2p engine
_initEngine();
// Start playing video
_loadVideo();
_initEngine() async {
await FlutterP2pEngine.init(
YOUR_TOKEN,
config: P2pConfig(
trackerZone: TrackerZone.Europe, // Set HongKong or USA if you changed zone
),
);
}
_loadVideo() async {
var url = YOUR_STREAM_URL;
url = await FlutterP2pEngine.parseStreamURL(url); // Parse your stream url
player = VideoPlayerController.networkUrl(url);
}
其中 YOUR_TOKEN 是用于标识用户的字符串,请换成自己的token,点击这里查看如何注册 Appid 并获取 token。