This topic describes the supported OSs and the required development environment for ApsaraVideo Player SDK for Android. This topic also describes how to integrate the SDK and how to run the demo.

Prerequisites

Item Requirement
OS version Android V4.3 or later is used.
Mobile phone processor One of the following processor architectures is used:
  • ARMv7
  • ARM64
Development tool We recommend that you use Android Studio. The procedures in this topic are based on an Android Studio development environment. To download Android Studio, go to the Android Studio page.
Version of ApsaraVideo Player SDK for Android ApsaraVideo Player SDK for Android V5.3.0 or later is used.

Integrate the SDK by using on-premises files

  1. Download the SDK package.
    Download the latest version of ApsaraVideo Player SDK for Android. For more information, see Release notes of ApsaraVideo Player SDK for Android.
    SDK package structureStructure
    The SDK package contains the following .aar files:
    • AliyunPlayer-x.x.x-full.aar: a complete AAR package that contains FFmpeg dynamic libraries.
    • AliyunPlayer-x.x.x-part.aar: an AAR package that does not contain FFmpeg dynamic libraries.
    • AlivcArtp-x.x.x.aar: an AAR package that supports Alibaba Real-time Transport Protocol (ARTP). This package is optional.
    • AlivcArtc-x.x.x.aar: an AAR package that supports the Alibaba Real-Time Communication (ARTC) protocol. This package is optional.
    Note
    • If you do not integrate the short video SDK, use the AliyunPlayer-x.xx.x-full.aar package as the dependency.
    • If you want to also integrate the short video SDK, you must use the AliyunPlayer-x.x.x-part.aar package as a dependency for ApsaraVideo Player SDK. In addition, you must use the com.aliyun.video.android:AlivcFFmpeg:x.x.x package as a dependency for both ApsaraVideo Player SDK and the short video SDK.
    • If you include an invalid dependency for SDK integration, an FFmpeg error may occur.
  2. Integrate the SDK.
    1. Copy the required AAR package to the libs directory of your project. If the libs directory does not exist, create a libs directory.
      libs directory
    2. In the build.gradle file of the project, add the flatDir setting to the repositories section of the allprojects block. Sample code:
      flatDir {
         dirs 'libs'
      }
    3. Add the reference to the AAR package and the Conan package com.alivc.conan:AlivcConan:x.x.x to the dependencies block in the build.gradle file of the application. Sample code:
      dependencies {
            implementation fileTree(dir: 'libs', include: ['*.aar'])
            // You need to only add the Conan package com.alivc.conan:AlivcConan:x.x.x to the dependencies block for ApsaraVideo Player whose version is earlier than V5.3.0. 
            implementation 'com.alivc.conan:AlivcConan:x.x.x'
      }
  3. Configure obfuscation rules.
    Add obfuscation rules to the proguard-rules.pro file of the application. Sample code:
    -keep class com.alivc.**{*;}
    -keep class com.aliyun.**{*;}
    -keep class com.cicada.**{*;}
    -dontwarn com.alivc.**
    -dontwarn com.aliyun.**
    -dontwarn com.cicada.**

Integrate the SDK by using Gradle

  1. Add the URL of Alibaba Cloud Maven repository to in the build.gradle file of the project. Sample code:
    maven {
        url 'https://maven.aliyun.com/repository/releases'
    }
    The following figure shows the result of the preceding operations.Integrate the SDK by using Gradle
  2. Add the reference to the AAR package to the dependencies block in the build.gradle file of the application. Sample code:
    implementation 'com.aliyun.sdk.android:AliyunPlayer:x.x.x-full'
    // You need to only add the Conan package com.alivc.conan:AlivcConan:x.x.x to the dependencies block for ApsaraVideo Player whose version is earlier than V5.3.0.
    implementation 'com.alivc.conan:AlivcConan:x.x.x'
    The following figure shows the result of the preceding operations.Result

Integrate the demo of ApsaraVideo Player by using custom UIs

The demo of ApsaraVideo Player contains various custom user interfaces (UIs). To integrate the demo by using custom UIs, perform the following steps. Gradle can be used to import ApsaraVideo Player SDK for Android in the demo. Specific API operations are encapsulated in the SDK and custom UIs are provided. You can integrate the required modules in the demo or use the SDK based on your business requirements.

  1. Download the demo source code.
    For more information, see Release notes of ApsaraVideo Player SDK for Android. We recommend that you use the latest version of the demo.
    Folder or file Description
    demo The demo of ApsaraVideo Player.
    JavaDoc The API reference of ApsaraVideo Player.
    sdk The .aar library of ApsaraVideo Player SDK for Android.
    X.X.XReleaseNote The release notes.
  2. Run the demo.
    In the navigation bar of Android Studio, choose File > Open. Import the demo of ApsaraVideo Player, as shown in the following figure.mage-20210128162213155.png
    Table 1. Description of the demo directory
    Module Description
    AliyunListPlayer The list playback module, which contains the sample code of the list player.
    AliyunLiveShiftPlayer The time shifting module, which contains the sample code of the time shifting feature.
    AliyunPlayer The ApsaraVideo Player module, which contains the sample code of ApsaraVideo Player.
    AliyunPlayerBase The base module of the ApsaraVideo Player demo, in which ApsaraVideo Player SDK for Android is integrated by using Gradle.
    AliyunVideoCommon The common module for Alibaba Cloud projects, which contains specific utility classes.
    zxing The QR code scanning module.
    thirdparty-lib The module that contains all dependencies required by the demo.
    Note
    • You must import the AliyunPlayerBase and AliyunVideoCommon modules, regardless of the player-related module that you want to import.
    • You must also import the thirdparty-lib module. Because the thirdparty-lib module is not a built-in module in Android Studio, you must copy the module to the directory that stores the AliyunPlayerBase and AliyunVideoCommon modules in the project.
  3. Integrate modules in the demo.
    Note In the following steps, the AliyunPlayer module is used as an example to show you how to integrate a module.
    1. In the navigation bar of Android Studio, choose File > New > Import Module.... Select the module that you want to import.
      image-20210128173551010
    2. Click Finish. Other required modules are automatically selected in Android Studio, as shown in the following figure.
      image-20210128173905107
    3. After the modules are imported, you must import the thirdparty-lib module by copying the module to the project. Add the reference to the module and the URL of Alibaba Cloud Maven repository to the build.gradle file of the project. The following figure shows the imported thirdparty-lib module.
      image-20210201100541465
      buildscript {
          apply from: 'thirdparty-lib/config.gradle'
      
          repositories {
              google()
              jcenter()
              maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }
              // The URL of the Maven repository that is required for screen mirroring.
              maven { url 'http://4thline.org/m2' }
          }
      }
      
      allprojects {
          repositories {
              google()
              jcenter()
              maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }
              // The URL of the Maven repository that is required for screen mirroring.
              maven { url 'http://4thline.org/m2' }
          }
      }
    4. Open the build.gradle file of the application and configure the compileSdkVersion, buildToolsVersion, minSdkVersion, and targetSdkVersion parameters in a way that the values of the parameters are the same as those of the parameters for the module in the demo.
      Troubleshooting
      If an error message as shown in the following figure is displayed, perform the following steps to resolve the issue.Error message
      1. Open the app/AndroidManifest.xml file and add the following code to the application label:
        android:allowBackup="true"
        application
      2. Open the values/themes.xml and values-night/themes.xml files and change the value of android:theme to @style/Theme.AppCompat.NoActionBar. Modify the themes file
    5. In the build.gradle file of the application, delete the AndroidX-related dependencies and add the following dependencies:
      implementation externalAndroidSupportV4
      implementation externalAndroidAppCompatV7
      implementation project(':Aliyunplayer:player_demo')
    After integration, you can run the following code to use the AliyunPlayer module of ApsaraVideo Player.
    Intent intent = new Intent(MainActivity.this,AliyunPlayerSettingActivity.class);startActivity(intent);

Integrate the demo of ApsaraVideo Player without using custom UIs

The demo of ApsaraVideo Player contains various custom UIs. If your business scenario does not require custom UIs, you can reduce the number of modules to import in the demo.

  1. Integrate ApsaraVideo Player SDK for Android by using on-premises files or by using Gradle. In the following example, ApsaraVideo Player SDK for Android is integrated by using Gradle. Add the URL of Alibaba Cloud Maven repository to the build.gradle file of the project.
    buildscript {
          repositories {
              google()
              jcenter()
              maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }
          }
      }
    
      allprojects {
          repositories {
              google()
              jcenter()
              maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }
          }
      }
  2. Add dependencies of ApsaraVideo Player to the build.gradle file of the application, as shown in the following code:
    implementation 'com.aliyun.sdk.android:AliyunPlayer:x.x.x-full'
    // You need to only add the Conan package com.alivc.conan:AlivcConan:x.x.x to the dependencies block for ApsaraVideo Player whose version is earlier than V5.3.0.
    implementation 'com.alivc.conan:AlivcConan:x.x.x'
  3. Copy specific code in the demo.
    Copy the AliyunRenderView, IRenderView, TextureRenderView, and SurfaceRenderView files in the Aliyunplayer/AlivcPlayerTools/src/main/java/com/aliyun/player/alivecpalyerexpand/widget directory.
    Note
    • The SurfaceRenerView file is used to render video images based on the SurfaceView class. The TextureRenderView file is used to render video images based on the TextureView class.
    • The AliyunRenderView file is used to encapsulate the player and bind the player with the render view.
    • You do not need to copy the AliyunVodPlayerView file because this file contains the sample code of the AliyunRenderView class.
    image-20210202094604436
  4. Use the AliyunRenderView class.
    You can refer to the sample code provided in the AliyunVodPlayerView file.
    1. Create an AliyunRenderView object.
        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".AliPlayerActivity">
      
            <com.aliyun.playertest.playerDemo.AliyunRenderView
                android:id="@+id/aliyunRenderView"
                android:layout_width="match_parent"
                android:layout_height="200dp" />
      
        </LinearLayout>
    2. Call API operations.
      java
        // Specify the type of the render view. Valid values: SurfaceType.TEXTURE_VIEW and SurfaceType.SURFACE_VIEW.
        aliyunRenderView.setSurfaceType(AliyunRenderView.SurfaceType.SURFACE_VIEW);
      
        // Configure listeners.
        aliyunRenderView.setXXXListener;
      
        // Configure the playback source. The setDataSource method is a reload method. You can use this method to configure data sources such as sts and playAuth.
        aliyunRenderView.setDataSource(urlSource);
      
        // Call the API operations that are related to playback.
        aliyunRenderView.prepare();
        aliyunRenderView.start()
        aliyunRenderView.pause()
        aliyunRenderView.stop()
        aliyunRenderView.release()
      Note The preceding code shows specific API operations of the AliyunRenderView class. To call other API operations of the AliyunRenderView class, refer to the AliyunVodPlayerView class. You can also call other API operations of the AliyunRenderView class based on the API reference of ApsaraVideo Player SDK for Android.