SurfaceView Let's first introduce how most software parses a video stream. First, it needs to determine the format of the video, which is related to decoding. Different formats of video are encoded differently, which is not the focus here. After knowing the encoding format of the video, it is decoded according to the encoding format, and finally a frame of images is obtained, and these images are quickly displayed on the interface, which is to play a video. SurfaceView in Android completes this function. Since SurfaceView is used in conjunction with MediaPlayer, MediaPlayer also provides corresponding methods to set SurfaceView to display images. You only need to specify SurfaceView to display images for MediaPlayer. Its complete signature is as follows: void setDisplay(SurfaceHolder sh) It needs to pass a SurfaceHolder object. SurfaceHolder can be understood as a container for SurfaceView to load the frames of images that need to be displayed. It can be obtained through the SurfaceHolder.getHolder() method. The steps for using MediaPlayer with SurfaceView to play videos are basically the same as using MediaPlayer to play MP3. You only need to set up the display SurfaceView. SurfaceView double buffering As mentioned above, SurfaceView, like most video applications, parses the video stream into frames for display. However, if this parsing process is completed in one thread, the next frame may not be parsed in time after the previous frame has been displayed, which will cause problems such as unsmooth images or asynchrony between sound and video. Therefore, SurfaceView, like most video applications, displays frames through a double buffering mechanism. So what is double buffering? Double buffering can be understood as two threads taking turns to parse the frame images of the video stream. When one thread finishes parsing the frame image, it renders the image into the interface, and at the same time, the other thread starts parsing the next frame image, so that the two threads take turns to parse the video stream to achieve a smooth playback effect. SurfaceHolder SurfaceView implements a double buffering mechanism internally, but this function consumes a lot of system memory. Due to the limitations of mobile devices, Android stipulates in its design that when SurfaceView is visible to the user, SurfaceHolder of SurfaceView is created to display the frame images parsed by the video stream. If SurfaceView becomes invisible to the user, SurfaceHolder of SurfaceView is destroyed immediately to save system resources. If the developer does not maintain SurfaceHolder, after minimizing the program, when you open the application again, the sound of the video continues to play, but the picture is not displayed. This is because when the SurfaceView is not visible to the user, the previous SurfaceHolder has been destroyed. When you enter again, the SurfaceHolder on the interface is already a new SurfaceHolder. Therefore, SurfaceHolder requires our developers to code and maintain it. To maintain SurfaceHolder, you need to use its callback, SurfaceHolder.Callback(), which needs to implement the following three methods:
The following is the calling process of these three methods. These three methods are implemented for SurfaceHolder in the application. First enter the application, SurfaceHolder is created, and the size of SurfaceHolder is changed after creation. Then press the Home button to return to the desktop and destroy SurfaceHolder. Finally, enter the application again, recreate SurfaceHolder and change its size. SurfaceView Demo Example So much about SurfaceView has been talked about above. The following is a demo to demonstrate how SurfaceView plays videos. A scroll bar is added to show the progress. You can also drag the scroll bar to select the playback position. The comments of the Demo are relatively complete, so I will not repeat them here. The video is found randomly on the Internet. When running, make sure that there is this file in the directory /sdcard/ykzzldx.mp4. Layout file: activity_main.xml Implementation code:
Source code download address: http://pan.baidu.com/s/1lgKLS Original address: Android, SurfaceView video playback source code |
<<: What does iOS 8 mean for app design?
>>: The love and hate of a pixel, the troubles between programmers and designers
1 What is a brand? It seems that there are very f...
" Faceu has already ranked first on the free...
" Exercise for less than 30 minutes is equiv...
Nowadays, it is not uncommon for us to achieve us...
Community economy has become a trend. Whoever has...
These rules are based on the Huawei Developer Ser...
Today I want to share with you: How to operate Zh...
Let me first briefly talk about the steps of list...
On October 4, luxury phone maker Vertu launched a...
[[131851]] Beijing time, April 15th morning news,...
The Spring Festival is approaching. Although I do...
In the past 2017, the mobile marketing market has...
iOS has become a company of its own with its power...
SEVEN paint · 2022 TIKTOK practical training camp...
Review expert: Peng Guoqiu, deputy chief physicia...