preface

Make a functional requirement of Taobao Toutiao according to project requirementsImplemented byandvideoThe mixed

I wrote about it in a previous blog postHeadline scroll demo———The demo address of Toutiao

Click this control and enter the page of ↓↓↓ headlines

This blog post mainly documents the use of video for text loading see my other blog Android rich text and examples

The first step is importing the dependency

Github’s official address is:

The compile 'cn. JZVD: jiaozivideoplayer: 6.2.12'Copy the code

Change the value to:

Implementation 'cn. JZVD: jiaozivideoplayer: 6.2.12'Copy the code

Compile is out of date. Compile is out of date. As for why we need to change compile to implementation, I searched and got the answer is: 1. Speed up compilation. 2. Hide unnecessary external interfaces. Specific details of their own search answers

Speaking of which, I have to say more about myself when adding this library also encountered a problem to share! In order to play this video, the SDK version of the third party control must be greater than 15, that is, at least 16. Below I post my own configuration, minSdkVersion 16 is the one I changed. If you encounter this problem, just change minSdkVersion to 16 and it will compile

 defaultConfig {
        applicationId "com.jxty.app.garden"
        minSdkVersion 16
        targetSdkVersion TARGET_SDK_VERSION as int
        versionCode VERSION_CODE as int
        versionName VERSION_NAME
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    }
Copy the code

Step 2 Add the layout file

Set the properties according to the requirements

 <cn.jzvd.JZVideoPlayerStandard
        android:id="@+id/m_video"
        android:layout_width="0dp"
        android:layout_height="200dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
Copy the code

Step 3 Set the video address, thumbnail address and title

JZVideoPlayerStandard videoPlayer; TextView mTvExplain, mTvExplainDetails; Private void init() {// Initialize component videoPlayer = findViewById(R.i.m.m_video); mTvExplain = findViewById(R.id.tv_explain); mTvExplainDetails = findViewById(R.id.tv_explain_details); for (int i = 0; i < headLineVideoList.size(); i++) { Article article = headLineVideoList.get(i); String videoUrl = article.getVideoUrl(); / / return from the background of the collection for broadcast video Url / / set the video address, thumbnails, title videoPlayer. SetUp (videoUrl, JZVideoPlayerStandard SCREEN_WINDOW_NORMAL, "Beautiful Fields "); // Set the video's title and introductory text box mtvexplain.settext (article.getTitle()); videoDetails = article.getVideoSummary(); / / if the video introduction to return empty He took video up mTvExplainDetails. SetText (videoDetails equals (" ")? article.getTitle() : videoDetails); }}Copy the code

The fourth step performs the pause and return calls

In contains fragments of the player or the Activity’s onPause () method call JCVideoPlayer. ReleaseAllVideos (). Corresponding operations should be performed in the corresponding life cycle:

 @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
Copy the code

As a qualified code farmers must have a curiosity to see the method here I can not afford to be curious or point into the source code to understand jZVideoPlayer.backpress () is the original RxJava back pressure concept

@ What is back pressure?

  • The observed produces events more slowly, and the observer processes events more quickly. Then the observer will wait for the observed to send the event (as if the observer is waiting for the rice to pan, and the program is waiting, which is fine).
  • The observed generates events quickly, and the observer processes them slowly. That’s a problem, because if left unchecked, events can pile up and eventually overwhelm your memory, causing your program to crash. (Like the rice produced by observers that no one eats and eventually rots when piled up).

When to suspend execution method JZVideoPlayer. Below releaseAllVideos () only literal meaning, We can also think of a way to release resources and I clicked on the source code which means that if the current time minus the time to click exit full screen is greater than the normal delay of full screen then the video of releasing the buffer is executed

if ((System.currentTimeMillis() - CLICK_QUIT_FULLSCREEN_TIME) > FULL_SCREEN_NORMAL_DELAY) {
            Log.d(TAG, "releaseAllVideos");
            JZVideoPlayerManager.completeAll();
            JZMediaManager.instance().positionInList = -1;
            JZMediaManager.instance().releaseMediaPlayer();
        }
Copy the code

Step 5 configure in androidmanifest.xml

 <activity
            android:name=".main.headline.HeadlineVideoActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:screenOrientation="portrait" />
Copy the code

The android: configChanges = “orientation” | screenSize | keyboardHidden “is somehow the screen switch, Prohibit the activity to create The android: screenOrientation = “portrait” limit the page number vertical screen display

Run to see the effect

Small screen playback

Full screen after landscape effect

Those who are careful will notice that the progress bar has a thin bar underneath that pops up the pause and return buttons when you touch the screen

Writing a simple thing will often encounter all kinds of small problems in solving these problems at the same time we learn to grow! Hope oneself continue to work hard!!

Share with you

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day