快速集成
- 1 Preparation for Reading
- 2 Environment Requirements
- 3 Directory Structure
- 4 Scene Module Integration
- 5 Project Configuration
- 6 Initialize SDK
- 7 Login to Live/Playback and Enter Different Scenes
1 Preparation for Reading
Download the Polyv Multi-Scene Demo in advance. Please download the Demo first.
Prepare a Polyv account. Logging into a live stream in the Demo requires the appId (App ID), appSecret (App Secret), userId (Account ID), and channelId (Channel ID) from the live streaming system. Logging into a playback additionally requires the vid (Playback Video ID) from the corresponding playback list of the channel.
2 Environment Requirements
| Name | Requirement |
|---|---|
| minSdkVersion | >=21 (Android 5.0) |
| targetSdkVersion | 27 (Android 8.1) |
| abiFilters | arm64-v8a, armeabi-v7a, x86 |
| jdk version | >=1.8.0 |
| Integration Tool | Android Studio |
Note 1: If developers need to support minSdkVersion >= 16 (Android 4.1 devices), please follow the OkHttp 4.x downgrade to 3.x compatibility solution.
Note 2: To support targetSdkVersion up to 28, add android:usesCleartextTraffic="true" to the AndroidManifest.xml file.
Note 3: The so library configuration can only use one or more of [arm64-v8a, armeabi-v7a, x86]; otherwise, crashes or audio/video desynchronization, skipping playback may occur on some devices.
Note 4: If targetSdkVersion is set to 29 or higher and access to files in the public external storage directory is required, declare requestLegacyExternalStorage in AndroidManifest.
<application
android:requestLegacyExternalStorage="true">
...
</application>
Note 5: If targetSdkVersion is set to 30 or higher and access to files in the public external storage directory is required, declare the manage external storage permission android.permission.MANAGE_EXTERNAL_STORAGE in AndroidManifest.
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
Additionally, dynamically check if the permission has been granted at runtime.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
activity.startActivity(intent);
}
}
Note 6: For versions 1.7.2 and earlier, if targetSdkVersion is set to 30 or higher, the pointer tagging feature must be disabled (Android official explanation on pointer tagging). Refer to the following code to modify AndroidManifest.xml to disable pointer tagging.
<application
android:allowNativeHeapPointerTagging="false">
...
</application>
3 Directory Structure
| Module Name | Description |
|---|---|
| demo | Demonstrates how to initialize the SDK, log in to live/playback, and enter different scenes |
| polyvLiveCloudClassScene | Cloud classroom scene module, includes demonstrations of playback, chat, co-streaming, interaction, etc. |
| polyvLiveEcommerceScene | Live commerce scene module, includes demonstrations of playback, chat, products, tipping, etc. |
| polyvLiveStreamerScene | Mobile streaming scene module, includes demonstrations of streaming, co-streaming, chat, documents, etc. |
| polyvStreamerAloneScene | Pure video streaming scene module, includes demonstrations of streaming, co-streaming, chat, etc. |
| polyvLiveCommonModul | Common module, provides base libraries, utility classes, etc., required by various scenes |
4 Scene Module Integration
4.1 Import Common Module (Required)
Copy the polyvLiveCommonModul module from the Polyv demo project to the root directory of your project. Then open the settings.gradle file of your project and add the following code:
include ':polyvLiveCommonModul'
4.2 Cloud Classroom Scene Module Integration (Optional)
Copy the polyvLiveCloudClassScene module from the Polyv demo project to the root directory of your project. Then open the settings.gradle file of your project and add the following code:
include ':polyvLiveCloudClassScene'
4.3 Live Commerce Scene Module Integration (Optional)
Copy the polyvLiveEcommerceScene module from the Polyv demo project to the root directory of your project. Then open the settings.gradle file of your project and add the following code:
include ':polyvLiveEcommerceScene'
4.4 Mobile Streaming Scene Module Integration (Optional)
Copy the polyvLiveStreamerScene module from the Polyv demo project to the root directory of your project. Then open the settings.gradle file of your project and add the following code:
include ':polyvLiveStreamerScene'
4.5 Pure Video Streaming Scene Module Integration (Optional)
Copy the polyvStreamerAloneScene module from the Polyv demo project to the root directory of your project. Then open the settings.gradle file of your project and add the following code:
include ':polyvStreamerAloneScene'
5 Project Configuration
5.1 Configure Module Compile Version
Open the build.gradle file of your project and add the following code:
ext {
compileSdkVersion = 27
minSdkVersion = 16
targetSdkVersion = 27
}
5.2 Configure Maven Address
Open the build.gralde file of your project and add the following code:
allprojects{
repositories {
maven { url "https://jitpack.io" }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases/' }
mavenCentral()
maven { url 'https://maven.aliyun.com/repository/public' }
// 保利威阿里云效
maven {
credentials {
username '609cc5623a10edbf36da9615'
password 'EbkbzTNHRJ=P'
}
url 'https://packages.aliyun.com/maven/repository/2102846-release-8EVsoM/'
}
}
}
5.3 Configure abiFilters
Open the app/build.gradle file of your project and add the following code:
//依赖的包可能支持很多类型的ABI
//为了避免打包了我们没有支持的ABI,指定需要打包的ABI目录
ndk {
abiFilters 'arm64-v8a', "armeabi-v7a", "x86"
}
5.4 Configure sdkVersion
Open the app/build.gradle file of your project, set minSdkVersion to 21 or higher, and set targetSdkVersion to 27 or lower.
To support minSdkVersion as 16, refer to the OkHttp 4.x downgrade to 3.x compatibility solution.
To support targetSdkVersion up to 28, add android:usesCleartextTraffic="true" to the AndroidManifest.xml file.
5.5 Configure renderScript
RenderScript is used for Gaussian blur backgrounds in the Polyv demo project. To support API 16, the demo uses RenderScript from the support package. If your project's minSdkVersion is 16 and you need the Gaussian blur feature, add the following configuration to the app/build.gradle file:
renderscriptSupportModeEnabled true
If your project's minSdkVersion大于 is not 16, or you do not need the Gaussian blur feature from the Polyv demo, perform the following steps:
1、移除polyvLiveCommonModul模块中build.gradle文件的renderscriptSupportModeEnabled true配置
2、移除polyvLiveCommonModul模块中SupportRenderScriptBlur类
3、把polyvLiveCommonModul模块中PLVBlurUtils类中的SupportRenderScriptBlur修改为RenderScriptBlur
5.6 Configure Apps with Over 64K Methods
If your project's minSdkVersion is set to 21 or higher, MultiDex is enabled by default, and you do not need the MultiDex support library.
Otherwise, refer to Configure Apps with Over 64K Methods to add MultiDex.
6 Initialize SDK
Open the Application class of your project and add the following code in the onCreate method:
PLVLiveSDKConfig.init(
new PLVLiveSDKConfig.Parameter(this)//sdk初始化所需参数
.isOpenDebugLog(true)//是否打开调试日志
.isEnableHttpDns(false)//是否使用httpdns
);
7 Login to Live/Playback and Enter Different Scenes
7.1 Viewer Side (Cloud Classroom Scene / Live Commerce Scene)
Before entering the viewer page of a scene, you must first call the login method for live/playback to verify. The purpose of login verification is to check the correctness of parameters and to save information obtained from the login interface internally in the SDK. The SDK provides IPLVSceneLoginManager for logging into live/playback. The interface definition is as follows:
/**
* 登陆直播观看
*
* @param appId 直播账号appId
* @param appSecret 直播账号appSecret
* @param userId 直播账号userId
* @param channelId 频道号
* @param loginListener 监听器
*/
void loginLiveNew(String appId,
String appSecret,
String userId,
String channelId,
OnLoginListener<PLVLiveLoginResult> loginListener);
/**
* 登陆回放观看
*
* @param appId 直播账号appId
* @param appSecret 直播账号appSecret
* @param userId 直播账号userId
* @param channelId 频道号
* @param videoId 视频id
* @param loginListener 监听器
*/
void loginPlaybackNew(String appId,
String appSecret,
String userId,
String channelId,
String videoId,
OnLoginListener<PLVPlaybackLoginResult> loginListener);
The result of the login verification is returned via a listener callback. You can choose to enter the required scene page in the successful login callback. The following example demonstrates how to log into a live stream and enter the scene page. The code is as follows:
//登录直播
loginManager.loginLiveNew(appId, appSecret, userId, channelId, new IPLVSceneLoginManager.OnLoginListener<PLVLiveLoginResult>() {
@Override
public void onLoginSuccess(PLVLiveLoginResult plvLiveLoginResult) {
loginProgressDialog.dismiss();
PLVLiveChannelConfigFiller.setupAccount(userId, appId, appSecret);
PLVLiveChannelType channelType = plvLiveLoginResult.getChannelTypeNew();
switch (curScene) {
//进入云课堂场景
case CLOUDCLASS:
PLVLaunchResult launchResult = PLVLCCloudClassActivity.launchLive(PLVLoginWatcherActivity.this, channelId, channelType, getViewerId(), getViewerName(), getViewerAvatar());
break;
//进入直播带货场景
case ECOMMERCE:
PLVLaunchResult launchResult = PLVECLiveEcommerceActivity.launchLive(PLVLoginWatcherActivity.this, channelId, channelType, getViewerId(), getViewerName(), getViewerAvatar());
break;
default:
break;
}
}
@Override
public void onLoginFailed(String msg, Throwable throwable) {
//...
}
});
Specific use cases for the above method can be found in the PLVLoginActivity class in the Polyv demo project.
7.2 Broadcaster Side (Mobile Streaming Scene)
Before entering the mobile streaming page, you must first call the login channel method to verify. The purpose of login verification is to check the correctness of parameters and to save information obtained from the login interface internally in the SDK. The SDK provides IPLVSceneLoginManager for logging into a channel for streaming. The interface definition is as follows:
/**
* 登录手机开播
*
* @param channelId 频道号
* @param pwd 频道密码
* @param loginListener 监听器
*/
void loginStreamerNew(String channelId,
String pwd,
OnLoginListener<PLVLoginVO> loginListener);
The result of the login verification is returned via a listener callback. You can enter the mobile streaming scene in the successful login callback. The following example demonstrates how to log into a channel and enter the mobile streaming scene. The code is as follows:
loginManager.loginStreamerNew(channelId, password, new IPLVSceneLoginManager.OnStringCodeLoginListener<PLVSLoginVO>() {
@Override
public void onLoginSuccess(PLVSLoginVO loginVO) {
// 其它处理
// 进入手机开播场景
PLVLaunchResult launchResult = PLVSAStreamerAloneActivity.launchStreamer(
PLVLoginStreamerActivity.this,
loginVO.getChannelId(),
loginVO.getInteractUid(),
loginNick,
loginVO.getTeacherAvatar(),
loginVO.getTeacherActor(),
loginVO.getChannelName(),
loginVO.getRole(),
loginVO.getColinMicType()
);
}
@Override
public void onLoginFailed(String msg, Throwable throwable) {
// 登录失败的处理
}
@Override
public void onLoginFailed(String msg, String code, Throwable throwable) {
// 登录失败的处理
}
});
Specific use cases for the above method can be found in the PLVLSLoginStreamerActivity class in the Polyv demo project.
