[Android] Surfaceview life cycle

Source: Internet
Author: User

The interface layout is basically the same as the mp3 in the previous section , which requires a control to play the video Surfaceview

The Surfaceview control internally maintains a double-buffered technique that uses two threads to parse frames and display them to the interface

Inside the Activity 's onCreate () method, locate the control object

The other code is consistent with the audio playback in the previous section

Locate the MediaPlayer object that called the setdatasource () method before

Call the Setdisplay () method of the MediaPlayer object to set the movie to play in surfaceholder Way, Parameters: surfaceholder object, call the Getholder () method of the surfaceview object to get

Invokes the Prepareasync () method of the MediaPlayer object for asynchronous preparation

Call the Setonpreparedlistener () method of the MediaPlayer object , set the listener to be completed, and in the overridden method, call the the start () method of the MediaPlayer object

On the lower version of the phone, add the following code to specify that you do not maintain the buffer, waiting for MediaPlayer to push the data out

Call the Getholder () of the Surfaceview object . SetType (surfaceholder.surface_type_push_buffers)

When the interface is minimized, re-opening the Surfaceholder will be destroyed, which is the system design in order to save memory

When the interface is not visible, it should record the playback position and stop, when re-visible, re-open

Gets the Surfaceholder object that invokes the getholder () method of the surfaceview Object

Call the Addcallback () method of the surfaceholder Object , add the callback, parameter:Callback interface type, Use the anonymous inner class to implement the interface, overriding the following methods

Surfacedestroyed (), at the time of destruction

Judge MediaPlayer is not empty and mediaplayer.isplaying () is true

Keep the progress in the Activity 's member variable currentposition , calling the MediaPlayer object's getPosition ()

Stop playing

Surfacecreated (), at the time of Creation

Call the play code, pass the currentposition through the pass , in the ready to complete callback in the back of play , call MediaPlayer The seekto () of the object sets the position last position

Seems to be because there is no support for many formats, the code has not been tested successfully, do not post

[Android] Surfaceview life cycle

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.