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:
After you swipe the list up and down quickly, you can no longer play the video and sometimes crash directly
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
To determine the slide direction when the list is sliding
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
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