引入外掛程式
shell
yarn add react-native-swarmcloudshell
npm install --save react-native-swarmcloudiOS
- 在 ios/Podfile 加入:
ruby
platform :ios, '13.0'Android
在專案的 android/build.gradle 的 allprojects.repositories 中設定 SwarmCloud 的儲存庫位址:
allprojects {
repositories {
// ... other repositories
maven {
url 'https://maven.swarmcloud.net/repository/maven-releases/'
}
}
}TIP
以下設定用於 release 打包階段, 開發階段不需要
- 為了保證正常使用 SDK,打包時請在 android/app/proguard-rules.pro 檔案中加入以下程式碼:
groovy
-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 設定
groovy
buildTypes {
release {
...
...
...
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}範例
tsx
import { useState, useEffect } from 'react';
import { StyleSheet, SafeAreaView } from 'react-native';
import { Header } from 'react-native/Libraries/NewAppScreen';
import Video from 'react-native-video';
import {
initP2pEngine,
parseStreamURL,
TrackerZone,
} from 'react-native-swarmcloud';
export default function App() {
const [source, setSource] = useState({
uri: '',
});
useEffect(() => {
initP2pEngine(YOUR_TOKEN, {
trackerZone: TrackerZone.Europe, // Set HongKong or USA if you changed zone
})
.then(() => {
const url = parseStreamURL(YOUR_STREAM_URL);
setSource({ uri: url });
})
.catch((e) => console.error(e.toString()));
}, []);
return (
<SafeAreaView>
<Header />
<Video
source={source}
controls={true}
style={styles.backgroundVideo}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
backgroundVideo: {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
},
});其中 YOUR_TOKEN 是用於標識使用者的字串,請換成自己的token,點擊這裡查看如何註冊 Appid 並取得 token。
Demo
完整的範例程式碼請參考這裡
