Asynchronous caching and playback of video in a sliding list

Source: Internet
Author: User

Video in the sliding list of asynchronous caching and playback, to a large number of high-quality game application source of the crowdfunding forum

Http://www.zccode.com/forum.php?mod=viewthread&tid=679&extra=

Recently seen on GitHub, Videoplayermanager is a project that plays a small video in the ListView and Recyclerview, mimicking the video that starts playing when you swipe to a visible video item in Instagram, Swipe to stop video playback when not visible

However, there are several issues with the project:

    1. After you swipe the list up and down quickly, you can no longer play the video and sometimes crash directly

    2. Asynchronous caching for network video is not supported

Therefore, it is optimized on the basis of this project and supports the asynchronous cache of network video.

Asynchronous caching of network video

The video cache is basically the same as the picture cache, now the picture cache framework is many, but the fundamental principle is the network download + memory cache + Local cache the three large components. And the video cache only need to skip the memory cache, when the video file is not downloaded to download and local cache, the next time directly from the local cache to read the video file information, so based on the picture cache framework is not difficult to implement the video file caching function.

Here I use glide to implement the video cache, glide not only support the image cache also supports the normal file cache, so the use of glide can be very simple to implement the video file cache

Textureview-based video playback controls

Android native provides a video playback control-Videoview, but Videoview is based on surfaceview implementation, Surfaceview will be a separate window to draw, it is not in view hierachy, The display is also not controlled by the properties of the view, cannot be translated, scaled, etc., and is difficult to put in a ListView or ScrollView, and some of the features in the view cannot be used.

In order to compensate for Surfaceview's shortcomings, Android added Textureview in 4.0, and it did not create a separate window for drawing, which allowed it to perform some transformations like the normal view, set the transparency, and easily put it in other viewgroup.

So to play the video in the ListView or Recyclerview, we need to implement the Textureview-based Videoview, which implements the code reference Viewvideo.

Auto-play and stop video in sliding list

To achieve automatic playback and stop of video, we need to calculate the visible ratio in the list in each item. For example, when an item has a visible ratio greater than 70%, the item is considered visible and the video playback is activated. Otherwise considered invisible, stop video playback

Here simply say the principle of implementation, mainly divided into the following three steps

    1. To determine the slide direction when the list is sliding

    2. According to the direction of the slide to determine whether the next item is visible, such as in the slide list, the current visible item is gradually decreasing the visible ratio, and the lower visible than the current item is less than 70% to stop playback, the next visible ratio is greater than 70% to start playing

    3. When a quick slide list is not detected, item changes (avoid stalling), and when the slide stops, finds the item that is visible in the currently visible item, and if the item is not the same as the item that was previously visible, activates the item

Performance issues with video playback in the list

The video playback is mainly using the Mediaplayer,mediaplayer state diagram as follows:

State diagram

As you can see, the video needs to be initialized first through Setdatasource () before it starts playing, and then ready to play through prepare () or Prepareasync () before the video starts playing through the start () action before it is ready to complete

where the prepare () operation is quite time-consuming, this step should never be called in the UI thread, while Prepareasync () is invoked asynchronously, so playing the video in the list listing should use Prepareasync () to prepare the video

by Prepareasync () This step is not enough to ensure that the list slides with no more than 16ms per frame, such as Setdatasource (), Reset (), release () These operations are time-consuming, although not up to the extent of the resulting ANR, But the smoothness of the list slide has a big impact.

Solution Solutions

Here I took all the operations of the MediaPlayer in a separate thread, and the event callback is Handler post back to the UI thread through the UI, so that the smoothness of the list slide is guaranteed

Effect Preview

Code specific use and detailed implementation methods have been put on GitHub

Project Address: Videolistplayer

Welcome, everybody, shoot bricks.

Original source: blog.waynell.com

Asynchronous caching and playback of video in a sliding list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.