3 Initialization
Initialize the SDK in the onCreate() method of the Application when the app starts.
1. Obtain the PolyvSDKClient Instance
PolyvSDKClient is the singleton object for Polyv SDK client settings. Correct parameters must be set for normal usage.
//获取 PolyvSDKClient 实例
PolyvSDKClient client = PolyvSDKClient.getInstance();
2. Set the SDK Encryption String
2.1 Obtain the SDK Encryption String
The SDK encryption string is visible on the official website under Cloud Video on Demand - Settings - API Interface. Alongside it, you can see the four user configuration items: userid, writetoken, readtoken, and secretkey. The Polyv SDK requires these configuration items to decrypt and play videos.
To prevent the app from being sniffed and reverse-engineered to obtain these four values, encryption is necessary. Hence, SDK加密串 is used, where encryption is performed on the server side and decryption correspondingly on the app side. The SDK加密串 provided by the Polyv backend is only a fixed encryption method for demonstration purposes.
For setting the SDK encryption string, developers should design their own encryption method, encrypting the four values userid, readtoken, writetoken, and secretkey on the server side, and decrypting them on the app side (it is recommended to use C language for decryption in Native methods), then pass the parameters to the SDK.
2.2 Set the SDK Encryption String
- Method 1: Directly set using the SDK encryption string provided by the Polyv backend (not recommended):
PolyvSDKClient client = PolyvSDKClient.getInstance();
client.settingsWithConfigString("你的SDK加密串", "加密密钥", "加密向量");
//client.settingsWithUserid(String userid, String secretkey, String readtoken, String writetoken)
If a sub-account with the role of Content Distribution has been added in the backend under Cloud Video on Demand - Settings - Account Management, it is recommended to use the settingsWithAppId() method instead of directly setting the SDK encryption string. Here, appid,secretKey can be found by clicking "Modify" on the sub-account in the backend interface and viewing the edit sub-account details. userId still uses the value from the API interface.
PolyvSDKClient client = PolyvSDKClient.getInstance();
//client.settingsWithConfigString("你的SDK加密串", "加密密钥", "加密向量");
client.settingsWithAppId(String appId, String secretKey, String userId);
...
Method 2: Developers encrypt user configuration information themselves and obtain it via network (recommended):
The following is an example of decryption and configuration on the Android side. For more encryption/decryption examples, refer to SDK Encryption String Official Solution.
public class PolyvApplication extends Application {
@Override
public void onCreate() {
new LoadConfigTask().execute();
...
}
private class LoadConfigTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String config = PolyvSDKUtil.getUrl2String("http://demo.ipolyv.cn/demo/appkey2.php");
if (TextUtils.isEmpty(config)) {
try {
throw new Exception("没有取到数据");
} catch (Exception e) {
e.printStackTrace();
}
}
return config;//取得SDK加密串
}
@Override
protected void onPostExecute(String config) {
//解密处理,拿到需要的初始化信息,进行初始化
PolyvSDKClient client = PolyvSDKClient.getInstance();
client.settingsWithConfigString(config, "加密密钥", "加密向量");
//client.settingsWithUserid(String userid, String secretkey, String readtoken, String writetoken)
//...
}
}
}
3. Initialize the SDK
PolyvSDKClient client = PolyvSDKClient.getInstance();
//初始化SDK设置
client.initSetting(getApplicationContext());
4. Set the Save Directory
Set the download file save directory.
PolyvSDKClient.getInstance().setDownloadDir("下载视频保存的目录");
5. Set the Student Unique Identifier
The student unique identifier, also known as viewer ID, audience ID, or student ID. Setting a unique identifier for students helps effectively locate issues reported by students and improves the efficiency of troubleshooting on the SDK side. For more details, see The Significance of Setting a Student Unique Identifier.
client.setViewerId("学员唯一标识");
6. Set Crash Reporting (Optional, Recommended)
We have integrated Bugly crash reporting into the SDK. Developers can initialize and enable it (optional).
//启动Bugly
client.initCrashReport(getApplicationContext());
//启动Bugly后,在学员登录时设置学员id
//client.crashReportSetUserId(userId);
7. Set HttpDNS (IPV6 Support)
Note: For versions integrated from 2.16.5.1 onwards, HttpDNS needs to be started at an appropriate time after obtaining user privacy authorization. For example, in the demo, call PolyvSDKClient.getInstance().enableHttpDns() in PolyvMainActivity to start HttpDNS.
PolyvSDKClient.getInstance().enableHttpDns(true);
In versions prior to 2.16.5.1, HttpDNS is enabled by default within the SDK, which effectively prevents network hijacking. However, HttpDNS resolves to IPv4 IP addresses by default. To support IPv6 environments, HttpDNS can be disabled (available from v2.13.2). After disabling, domain names will be used for access, supporting both IPv4 and IPv6.
//v2.13.2起提供了以下接口
//默认开启了HttpDns,使用IPV4
client.enableHttpDns(true);
//如果需要支持IPV6/IPV4,请开启IPV6开关,或者将enableHttpDns设置为false。开启后自动关闭HttpDns,采用域名访问
//client.enableIPV6(true);
For versions prior to 2.13.2, reflection can be used to disable HttpDNS. After disabling, IPv4/IPv6 is supported.
//在Application的PolyvSDKClient.getInstance().initSetting(getApplicationContext())初始化后调用
try {
Field[] fields = PolyvSDKClient.getInstance().getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if(field.getType() == HttpDnsService.class){
field.setAccessible(true);
field.set(PolyvSDKClient.getInstance(), null);
break;
}
}
}catch (IllegalAccessException e){
e.printStackTrace();
}
