Player integration
  • 22 Aug 2023
  • 2 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

Player integration

  • Dark
    Light
  • PDF

Article summary

Developers can interact with the Player SDK by using the VLPlayer class. 

Add VLPlayer view in your layout xml (or you can create it by code providing all the needed UI parameters). 

Example:

<com.vl.viewlift.playersdk.views.VLPlayer
    android:id="@+id/sampleVideoPlayer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="H,16:9"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

Now you can simply initialize the VLPlayer Android SDK with a valid ViewLift Token, a ViewLift video ID, along with any optional parameters. 

At first, declare VLPlayer instance, token, and video Id.

private VLPlayer vlPlayer;
public static String vlToken = "enter-viewlift-token"; 
private static String videoID = "enter-viewlift-video-id" ;

Then initialise the player by finding the player view and calling init() method on it . init() method initialises SDK object & allows your code to add configurations to player.

//set the VLPlayer to the appropriate view				
vlPlayer = findViewById(R.id.sampleVideoPlayer);
vlPlayer.init();

Set optional parameters for the player.

//set any optional parameters					
OptionalPlayerParams optionalPlayerParams = new OptionalPlayerParams();
optionalPlayerParams.setAdUrl(adUrl);
optionalPlayerParams.setShowThumbImage(true);

Now call method setSource to set your video to player. 

setSource needs to be initialized with: 

  • Valid ViewLift Token 
  • A ViewLift video ID 
  • Video Player Tag - String value for tagging video player instance - this tag will come with every event to identify the instance of player 
  • OptionalPlayerParams – Will have optional parameters like thumbnail image show or ad URL.
vlPlayer.setSource(vlToken, videoID, “tag1”, optionalPlayerParams);

init & setSource methods needs to be used for every new instance of player. 

Having separate init methods for each player makes sure we can have different configs for each player instance. 

Next, in your video player activity class paste this:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == VLPlayerActivity.LAST_PLAY_POSITION) {
            seekPosition = data.getExtras().getLong(VLPlayerActivity.VL_LAST_PLAY_POSITION, 0);
            int cc_button_pref_name_id = getResources().getIdentifier("cc_button_pref_name","string", getPackageName());
            SharedPreferences ccPrefs = getSharedPreferences(getString(cc_button_pref_name_id), MODE_PRIVATE);
            int cc_button_key_id = getResources().getIdentifier("cc_button_key","string", getPackageName());
            boolean isClosedCaptionEnabled = ccPrefs.getBoolean(getString(cc_button_key_id), false);
            if (this instanceof VLPlayerVideoEventListener) {
                VLPlayerVideoEventListener vlPlayerVideoEventListener = (VLPlayerVideoEventListener) this;
                vlPlayerVideoEventListener.onFullscreenChange(seekPosition, false);
            }
            vlPlayer.setCcButton(isClosedCaptionEnabled);
            int current_video_position_id = getResources().getIdentifier("current_video_position","string", getPackageName());
            playlistPosition = data.getExtras().getInt(this.getString(current_video_position_id), 0);
            if (playlistPosition > 0 && playlistPosition != vlPlayer.currentVideoPosition) {
                vlPlayer.playVideoInPlaylist(playlistPosition);
                startNewVideo = true;
            } else {
                vlPlayer.pause();
                vlPlayer.seekTo(seekPosition);
            }
        }
    }

Lastly, ensure that the versions of com.android.support:* libraries match between your app and inside the VLPlayer SDK.

Notes

Sometimes, lint may report an error when you try to access resources from the library using com.vl.viewlift.player.R. However, the project will still work fine if you run it

To solve this lint error, you can use the following code:

getResources().getIdentifier(String name, String defType, String defPackage)

Where:

name is the name of the resource.

defType is the type of resource.

defPackage is the package name of your app.

For example, to access a string resource, the syntax would be :

getResources().getIdentifier("cc_button_pref_name","string", getPackageName())

Similarly, to access a layout resource, you would use the following syntax:

getResources().getIdentifier("vl_playback_control_view","layout", getPackageName());

And for an image resource, please use the following syntax:

getResources().getIdentifier("ic_fullscreen_24dp","drawable", getPackageName())



Was this article helpful?