Application scenarios:
Video playback using videoview is simple and convenient, but mediaplayer can also be used to play videos. However, we know that mediaplayer is mainly used to play audio. It is an output interface that does not provide output images. In this case, we use the surfaceview control to combine it with mediaplayer, the output of the video can be achieved. The surfaceview control class is introduced first.
Surfaceview class
Constructor
Method Name |
Description |
Public surfaceview (context) |
Create a surfaceview object using context |
Public surfaceview (context, attributeset attrs) |
Create a surfaceview object through context object and attributeset |
Public surfaceview (context, attributeset attrs, int defstyle) |
The context object and attributeset can be used to create and specify a style and surfaceview object. |
Common Methods
Method Name |
Description |
Public surfaceholder getholder () |
The surfaceholder object is used to manage surfaceview. |
Public void setvisibility (INT visibility) |
Specifies whether the image is visible. The value can be visible, invisible, or gone. |
Surfaceholder
It is an interface used to manage surfaceview. There are two common internal interfaces surfaceholder. Callback, surfaceholder. callback2 and callback2 are implemented in callback.
Common Methods
Method Name |
Description |
Public abstract void addcallback (surfaceholder. Callback callback) |
Add a callback object to listen to surfaceview changes |
Public abstract void removecallback (surfaceholder. Callback callback) |
Remove callback |
Public abstract void settype (INT type) |
Set surfaceview Control Mode |
Public abstract canvas lockcanvas () |
Lock the whole surfaceview object and obtain the canvas on the surface |
Public abstract canvas lockcanvas (rect dirty) |
Lock the rect area on surfaceview and obtain the canvas on the surface. |
Public abstract void unlockcanvasandpost (canvas) |
Call this method. The previously drawn image is still in the buffer. The area locked by the next lockcanvas () method may be "blocked ". |
Surfaceholder. Callback
There are three abstract methods in callback.
Method Name |
Description |
Public abstract void surfacechanged (surfaceholder holder, int format, int width, int height) |
Triggered when surfaceview changes |
Public abstract void surfacecreated (surfaceholder holder) |
Triggered when surfaceview is created |
Public abstract void surfacedestroyed (surfaceholder holder) |
Triggered when surfaceview is destroyed |
How can we understand the relationship between these classes or interfaces?
In this way, we can understand:
Surfaceview is used for display. surfaceholder is used to manage the surfaceview object of the display. But how does surfaceholder manage this object? This uses the surfceholder. addcallback () method to add three abstract methods for the Internal interface of a surfaceholder interface for management or monitoring surfaceview. In this way, surfaceview is managed.
Example: Use mediaplayer and surfaceview to play a video
Effect:
Steps:
1) create a mediaplayer object and set the loaded video file (setdatasource ())
2) define the surfaceview control in the interface layout File
3) Use mediaplayer. setdisplay (surfaceholder SH) to specify the video image output to surfaceview.
4) Other mediaplayer methods are used to play videos.
Code implementation:
Layout file: Main. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <SurfaceView android:id="@+id/surfaceView" android:layout_width="fill_parent" android:layout_height="360px" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <ImageButton android:id="@+id/btnplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/play" /> <ImageButton android:id="@+id/btnpause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pause" /> <ImageButton android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop" /> </LinearLayout></LinearLayout>
Activity Code
Package COM. jiahui. surfaceview; import android. app. activity; import android. media. audiomanager; import android. media. mediaplayer; import android. OS. bundle; import android. view. surfaceholder; import android. view. surfaceholder. callback; import android. view. surfaceview; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imagebutton; public class extends activity implements onclicklistener {imagebutton btnplay, btnstop, btnpause; surfaceview; mediaplayer; int position; Public void oncreate (bundle entity) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btnplay = (imagebutton) This. findviewbyid (R. id. btnplay); btnstop = (imagebutton) This. findviewbyid (R. id. btnplay); btnpause = (imagebutton) This. findviewbyid (R. id. btnplay); btnstop. setonclicklistener (this); btnplay. setonclicklistener (this); btnpause. setonclicklistener (this); mediaplayer = new mediaplayer (); surfaceview = (surfaceview) This. findviewbyid (R. id. surfaceview); // set surfaceview, which is not managed by surfaceview itself. getholder (). settype (surfaceholder. surface_type_push_buffers); surfaceview. getholder (). addcallback (New callback () {@ overridepublic void surfacedestroyed (surfaceholder holder) {}@ overridepublic void surfacecreated (surfaceholder holder) {If (position> 0) {try {// start playing play (); // and play mediaplayer directly from the specified position. seekto (position); position = 0;} catch (exception e) {// todo: handle exception }}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}}) ;}@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. btnplay: Play (); break; case R. id. btnpause: If (mediaplayer. isplaying () {mediaplayer. pause ();} else {mediaplayer. start ();} break; case R. id. btnstop: If (mediaplayer. isplaying () {mediaplayer. stop ();} break; default: break; }}@ overrideprotected void onpause () {// determine whether to play if (mediaplayer. isplaying () {// if the video is being played, we will first save the position = mediaplayer. getcurrentposition (); mediaplayer. stop ();} super. onpause ();} private void play () {try {mediaplayer. reset (); mediaplayer. setaudiostreamtype (audiomanager. stream_music); // sets the mediaplayer of the video to be played. setdatasource ("/mnt/sdcard/movie.3gp"); // output the video image to surfaceviewmediaplayer. setdisplay (surfaceview. getholder (); mediaplayer. prepare (); // play mediaplayer. start () ;}catch (exception e) {// todo: handle exception }}}
If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524