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. Open the build.gradle file in the root directory of your Android Studio project and add the AppGallery Connect plug-in configurations and Maven repository address.
    buildscript { repositories { ...... maven {url 'https://developer.huawei.com/repo/'} } dependencies { ...... classpath 'com.huawei.agconnect:agcp:1.6.0.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 Maven dependencies in the build.gradle file under the app directory of your Android Studio project.
    dependencies { ...... implementation "com.huawei.hms:videokit-player:1.0.14.300" }
  4. If you want your app to use Video Kit without relying on HMS Core (APK), add the following fallback dependencies in the dependencies block.
    dependencies { implementation 'com.huawei.hms:videokit-player-fallback:1.0.14.300' }
  5. Configure the NDK in the build.gradle file under the app directory of your Android Studio project.
    android { defaultConfig { ...... ndk { abiFilters "armeabi-v7a", "arm64-v8a" } } ...... }
  6. 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" /> // (Optional) Obtain the wake lock. Call setWakeMode to enable it. <uses-permission android:name="android.permission.WAKE_LOCK" />

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.**{*;} # Exclude dynamic-loader-fallback from obfuscation. -keep class com.huawei.hms.feature.dynamic.descriptors.huawei_module_videoplayer.AssetModuleDescriptor{*;}
  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.Builder factoryOptions = new WisePlayerFactoryOptionsExt.Builder().setDeviceId("xxx"); // Set the log configurations. LogConfigInfo logCfgInfo = new LogConfigInfo(1, "", 20, 1024); factoryOptions.setLogConfigInfo(logCfgInfo); // 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.build(), 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. Then, add a listener for detecting WisePlayer status changes.
      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. pre>
      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.
      // Set a listener that detects errors of WisePlayer. player.setErrorListener(this); // Set a listener that detects media and playback events of WisePlayer. player.setEventListener(this); // Set a listener that detects video resolution changes. player.setResolutionUpdatedListener(this); // Set a listener that checks whether a video is ready for playback. player.setReadyListener(this); // Set a listener that detects buffering events of WisePlayer. player.setLoadingListener(this); // Set a listener that checks whether the playback ends. player.setPlayEndListener(this); // Set a listener that checks whether the seek operation is complete. 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. Specify the URL from which WisePlayer obtains video content to play. You can 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:

    • Integrate the Video SDK.
    • Call the Video SDK to play a video.

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

    Code copied