Overview

HUAWEI Video Kit provides video playback in this version, and will support video editing and video hosting in later versions, helping you quickly build desired video features to deliver a superb video experience to your app users.

What You Will Create

In this codelab, you will call Video Kit APIs through the sample code to create an app and use its functions:

What You Will Learn

Hardware Requirements

Software Requirements

Required Knowledge

Android app development basics

To integrate HUAWEI HMS Core, you must complete the following preparations:

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

Adding the AppGallery Connect Configuration File

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your app project and click the app that needs to integrate the HMS Core SDK.
  3. Go to Project Setting > General information. In the App information area, download the agconnect-services.json file.
  4. Copy the agconnect-services.json file to the app's root directory of your Android Studio project.

Adding SDK Dependencies

  1. Add the AppGallery Connect plug-in and the Maven repository in the project-level build.gradle file.
    buildscript { repositories { ...... maven {url 'https://developer.huawei.com/repo/'} } dependencies { ...... classpath 'com.huawei.agconnect:agcp:1.4.2.300' // HUAWEI agcp plugin } } allprojects { repositories { ...... maven {url 'https://developer.huawei.com/repo/'} } }
  2. Open the build.gradle file in the app directory and add the AppGallery Connect plug-in.
    apply plugin: 'com.android.application' // Add the following line apply plugin: 'com.huawei.agconnect' // HUAWEI agconnect Gradle plugin android { ...... }
  3. Configure the Maven dependency in the app-level build.gradle file.
    dependencies { ...... implementation 'com.android.support:appcompat-v7:28.0.0' implementation "com.huawei.hms:videokit-player:1.0.2.304" implementation "com.huawei.hms:network-grs:4.0.20.301" implementation 'com.huawei.hms:device:5.1.0.300' implementation 'com.huawei.hms:base:5.1.0.300' implementation 'com.huawei.android.hms:security-base:1.1.5.301' implementation 'com.huawei.android.hms:security-ssl:1.1.5.301' implementation 'com.huawei.android.hms:security-encrypt:1.1.5.301' implementation 'com.squareup.okhttp3:okhttp:3.12.2' implementation 'com.google.code.gson:gson:2.8.6' }
  4. Configure the NDK in the app-level build.gradle file.
    android { defaultConfig { ...... ndk { abiFilters "armeabi-v7a", "arm64-v8a" } } ...... }
  5. Add permissions in the AndroidManifest.xml file.
    // Add the permission to access the Internet. <uses-permission android:name="android.permission.INTERNET" /> // Add the permission to obtain the network status. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> // Add the permission to access the Wi-Fi network. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> // Add the permission to write data into external storage. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> // Add the permission to read data from external storage. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> // (Optional) Add the permission to read device information, which helps users better experience the functions of WisePlayer. <uses-permission android:name="android.permission.READ_PHONE_STATE" /> // Add the permission to check whether a device is rooted. <uses-permission android:name="com.huawei.permission.SECURITY_DIAGNOSE" />

Configuring Obfuscation Scripts

Before building the APK, configure the obfuscation configuration file to prevent the HMS Core SDK from being obfuscated.
The obfuscation configuration file is proguard-rules.pro for Android Studio.

  1. Open the obfuscation configuration file of your Android Studio project.
  2. Add configurations to exclude the HMS Core SDK from obfuscation.
    -ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;} -keep class com.huawei.hianalytics.**{*;}
  3. If you are using AndResGuard, add its trustlist to the obfuscation configuration file.
    "R.string.hms*", "R.string.connect_server_fail_prompt_toast", "R.string.getting_message_fail_prompt_toast", "R.string.no_available_network_prompt_toast", "R.string.third_app_*", "R.string.upsdk_*", "R.layout.hms*", "R.layout.upsdk_*", "R.drawable.upsdk*", "R.color.upsdk*", "R.dimen.upsdk*", "R.style.upsdk*", "R.string.agc*"

Initializing WisePlayer

The app needs to implement a class that inherits Application. In the onCreate() method, the initialization API WisePlayerFactory.initFactory() of the Video SDK is called.

public class VideoKitApplication extends Application { private static final String TAG = "VideoKitApplication"; private static WisePlayerFactory factory; @Override public void onCreate() { super.onCreate(); // Pass the unique ID of a device to the setDeviceId method. // Pass a country/region code to the setServeCountry method. WisePlayerFactoryOptionsExt factoryOptions = new WisePlayerFactoryOptionsExt.Builder().setDeviceId("xxx").build(); // In the multi-process scenario, the onCreate method in Application is called multiple times. // The app needs to call the WisePlayerFactory.initFactory() API in the onCreate method of the app process (named "app package name") and WisePlayer process (named "app package name:player"). WisePlayerFactory.initFactory(this, factoryOptions, new InitFactoryCallback() { @Override public void onSuccess(WisePlayerFactory wisePlayerFactory) { Logger.d(TAG, "onSuccess wisePlayerFactory:" + wisePlayerFactory); factory = wisePlayerFactory; } @Override public void onFailure(int errorCode, String msg) { Logger.e(TAG, "onFailure errorcode:" + errorCode + " reason:" + msg); } }); } public static WisePlayerFactory getWisePlayerFactory() { return factory; } }

Playing a Video

  1. Create a PlayActivity that inherits from AppCompatActivity and implement the Callback and SurfaceTextureListener APIs. In the layout file, add SurfaceView or TextureView to be displayed in WisePlayer.
    import android.view.SurfaceHolder.Callback; import android.view.TextureView.SurfaceTextureListener; public class PlayActivity extends AppCompatActivity implements Callback, SurfaceTextureListener, WisePlayer.ErrorListener, WisePlayer.ReadyListener, WisePlayer.EventListener, WisePlayer.PlayEndListener,WisePlayer.ResolutionUpdatedListener, WisePlayer.SeekEndListener, WisePlayer.LoadingListener
  2. A WisePlayerFactory instance is returned when the initialization is complete in Application. Use the onCreate method to obtain the WisePlayerFactory instance from Application. Then call createWisePlayer() to create WisePlayer.
    WisePlayer player = VideoKitApplication.getWisePlayerFactory().createWisePlayer();
  3. Initialize the WisePlayer layout and add layout listeners.
    // Method 1: SurfaceView display interface SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_view); SurfaceHolder surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Method 2: TextureView display interface TextureView textureView = (TextureView) findViewById(R.id.texture_view); textureView.setSurfaceTextureListener(this);
  4. Register WisePlayer listeners. The app will react based on listener callbacks.
    player.setErrorListener(this); player.setEventListener(this); player.setResolutionUpdatedListener(this); player.setReadyListener(this); player.setLoadingListener(this); player.setPlayEndListener(this); player.setSeekEndListener(this);
  5. Set playback parameters, such as video type, bookmark, and whether to enable the repeat mode.
    player.setVideoType(PlayMode.PLAY_MODE_NORMAL); player.setBookmark(10000); player.setCycleMode(CycleMode.MODE_CYCLE);
  6. Set one or multiple URLs for a video.
    // Method 1: Set one URL for a video. player.setPlayUrl("https://videoplay-mos-dra.dbankcdn.com/P_VT/video_injection/92/v3/C072F990370950198572111872/MP4Mix_H.264_1920x1080_6000_HEAAC1_PVC_NoCut.mp4"); // Method 2: Set multiple URLs for a video. (When an exception occurs during the playback from one URL, WisePlayer will switch to another URL to retrieve the video.) // player.setPlayUrl(new String[] {"https://videoplay-mos-dra.dbankcdn.com/P_VT/video_injection/92/v3/C072F990370950198572111872/MP4Mix_H.264_1920x1080_6000_HEAAC1_PVC_NoCut.mp4", "https://videoplay-mos-dra.dbankcdn.com/P_VT/video_injection/92/v3/C072F990370950198572111872/MP4Mix_H.264_1920x1080_6000_HEAAC1_PVC_NoCut.mp4"});
  7. Set a view to display the video. Currently, WisePlayer supports SurfaceView and TextureView. Make sure that your app has a valid view for video display; otherwise, the playback will fail.
    // SurfaceView listener callback @Override public void surfaceCreated(SurfaceHolder holder) { player.setView(surfaceView); } // TextureView listener callback @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { player.setView(textureView); // Call the resume API to bring WisePlayer to the foreground. You can determine whether to automatically start playback when WisePlayer is brought to the foreground by passing the corresponding parameter. player.resume(ResumeType.KEEP); }
  8. Prepare for the playback and start requesting data.
    player.ready();
  9. Implement the WisePlayer.ReadyListener API in PlayActivity. Start the playback upon a success response of the onReady callback method in this API.
    @Override public void onReady(WisePlayer wisePlayer) { player.start(); }

Checking the Running Result

In your Android Studio, click . Launch the demo app on your phone. You will see the following screens:

Home screen

Settings options

Playback screen

Playback settings

Well done. You have successfully completed this codelab and learned how to:

To learn more, click the following link:
Documentation
To download the sample code, click the following link:

Sample Code

Code copied