# 2 快速集成

接入之前需要在 [Polyv 官网](http://www.polyv.net/) 中注册完毕，并开通云点播服务。

## 1. Gradle 配置

1、在 `Project` 的 `build.gradle` 文件中添加 `maven仓库`：

```groovy
allprojects {
    repositories {
        google()
        //阿里云的镜像库
        maven {url "http://maven.aliyun.com/nexus/content/groups/public/"}
        maven {url 'http://maven.aliyun.com/nexus/content/repositories/releases/'}
        mavenCentral()
        //阿里云效关于central的镜像
        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/'
        }
    }
}
```

2、在 `app` 的 `build.gradle` 文件中添加 ndk 声明

```groovy
android {
    ...
    defaultConfig {
        ...
        ndk {
            abiFilters 'arm64-v8a', 'armeabi-v7a', 'armeabi', 'x86_64', 'x86'
        }
    }
    ...
}
```

abiFilters 可以按实际情况筛选，建议配置为 `arm64-v8a、armeabi-v7a、x86`。**不要仅使用 `armeabi`，仅使用`armeabi`可能会在部分机型上出现音画不同步、跳秒播放等异常情况。**

3、在 `app` 的 `build.gradle` 文件中添加依赖：

```groovy
implementation 'com.android.support:support-annotations:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:multidex:1.0.3'

implementation 'net.polyv.android:polyvPlayer:2.22.1'//SDK核心包
implementation 'net.polyv.android:polyvDownload:2.22.1'//SDK下载功能
implementation 'net.polyv.android:polyvUpload:2.22.1'//SDK上传功能
implementation 'net.polyv.android:polyvSub:2.22.1'//弹幕、截图功能中使用

implementation 'de.hdodenhof:circleimageview:2.2.0'//圆形imageview，音频封面图使用
implementation 'com.github.bumptech.glide:glide:4.7.1'//demo中的ppt图片加载使用
implementation "com.daimajia.swipelayout:library:1.2.0@aar"//demo中下载列表使用

// 投屏SDK自点播2.15.0起不再维护，相关实现已经转移到Demo，详细请查看Wiki-7.视频投屏
// 投屏sdk
implementation files("libs/wx-commonlib-1.0.1.aar")
implementation files("libs/wx-dlnasdk-1.1.5.aar")
implementation files("libs/wx-dlnasender-1.1.5.aar")
implementation files("libs/wx-jettylib-1.0.1.aar")
```

SDK 核心包是视频播放必须要使用的包，其他依赖可以按照实际需求导入。若引入库过多导致超过64k方法数限制，可参考 Demo 解决方案。

## 2 配置 AndroidManifest.xml

1、在应用程序的 `AndroidManifest.xml` 文件中添加相应的权限：

```xml
<!-- 基础权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<!-- WRITE_EXTERNAL_STORAGE属于android6.0运行时权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Android 10 起需要精确定位才能获取wifi详细信息  -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
```

**注1：2.18.0以前版本，如需适配Android 11，即targetSdkVersion>=30，需要关闭指针标记功能（**[**Android官方关于指针标记的说明**](https://source.android.google.cn/devices/tech/debug/tagged-pointers)**），参考以下代码修改AndroidManifest.xml以关闭指针标记。**

```xml
  <application 
    android:allowNativeHeapPointerTagging="false">
    ...
  </application>
```

## 3. 初始化 SDK

### 3.1 获取 SDK 加密串

SDK加密串在官网 **云点播-设置-API接口** 里面可见。更多说明见 初始化-设置SDK。

> **注意**：**保利威后台提供的 `SDK加密串` 只是一种固定的加密方式，仅做演示使用。** 对于设置SDK加密串，开发者应当使用自己的加密方式，具体可见[正式使用解决方案](https://github.com/easefun/polyv-ios-client-demo/wiki/SDK_Encrypted_String)。

### 3.2 初始化SDK

**注意**：在集成2.16.5.1及之后的版本，需要在获取用户隐私授权的前提下，在适当的时机做httpdns启动。以demo中为例：在`PolyvMainActivity` 中调用 PolyvSDKClient.getInstance().enableHttpDns()方法来启动httpdns。

```java
PolyvSDKClient.getInstance().enableHttpDns(true);
```

在应用程序启动的 Application 的 `onCreate()` 方法中对 SDK 进行初始化。

```java
//获取 PolyvSDKClient实例
PolyvSDKClient client = PolyvSDKClient.getInstance();
//设置SDK加密串
client.settingsWithConfigString("你的SDK加密串", "加密密钥", "加密向量");
//初始化SDK设置
client.initSetting(getApplicationContext());
//设置学员唯一标识
client.setViewerId("学员唯一标识");
//设置下载保存目录
PolyvSDKClient.getInstance().setDownloadDir("下载视频保存的目录");
```

## 4. 视频播放

视频的操作依赖于 VID，VID是视频的唯一识别标志。视频在后台上传成功后会自动生成VID 。

`PolyvVideoView` 是视频播放器的主类。在布局文件中声明并初始化完成后，通过 `videoView.setVid(VID)` 就可以实现视频的自动播放。

```xml
<com.easefun.polyvsdk.video.PolyvVideoView
	android:id="@+id/polyv_video_view"
	android:layout_width="match_parent"
	android:layout_height="match_parent"/>
```

```java
PolyvVideoView videoView = findViewById(R.id.polyv_video_view);
videoView.setVid(vid);
```

## 5. 视频下载

下载的视频统一保存到 `PolyvSDKClient.getInstance().setDownloadDir(File)`设置的目录中。若没有设置将会无法下载保存。视频下载会根据VID、码率进行下载。`PolyvDownloaderManager` 是对视频下载操作的封装类，可对此进行视频下载的基本操作。

```java
// 设置下载队列总数，多少个视频能同时下载。(默认是1，设置负数和0是没有限制)
PolyvDownloaderManager.setDownloadQueueCount(1);
//获取downloader
PolyvDownloader downloader = PolyvDownloaderManager.getPolyvDownloader(vid, bitrate);
//开始下载
downloader.start(getApplicationContext);
```

`getPolyvDownloader()` 方法会将下载视频任务保存到 Map 中，保证下载任务的唯一。该方法返回一个 `PolyvDownloader` 实例，是视频下载的具体实现类，可启动和关闭具体的下载任务。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://polyv.gitbook.io/document/docs/vod/android/2.-kuai-su-ji-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
