When you use audioview to play a video, is it uncomfortable? The same mode is disgusting. Here, we can wrap the mediaplayer in some ways. Surfaceview and surfaceholder are used.
Finally:
We provide four buttons for playback control.
--------------------------------------------------------------------------------
Layout file media. XML code:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" <br/> Android: orientation = "vertical"> <br/> <surfaceview Android: Id = "@ + ID/surfaceview1" <br/> Android: layout_width = "320px" Android: layout_height = "160px"> </surfaceview> <br/> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" <br/> Android: orientation = "horizontal" <br/> <imagebutton Android: Id = "@ + ID/button_play" Android: src = "@ drawable/play" Android: onclick = "buttonclick" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </imagebutton> <br/> <imagebutton Android: id = "@ + ID/button_pause" Android: src = "@ drawable/pause" Android: onclick = "buttonclick" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </imagebutton> <br/> <imagebutton Android: Id = "@ + ID/button_stop" Android: src = "@ drawable/Stop" Android: onclick = "buttonclick" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </imagebutton> <br/> <imagebutton Android: id = "@ + ID/button_reset" Android: src = "@ drawable/RESET" Android: onclick = "buttonclick" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </imagebutton> <br/> </linearlayout> <br/>
Activity Code:
Package cn.com. chenzheng_java.media; </P> <p> Import android. app. activity; <br/> Import android. media. audiomanager; <br/> Import android. media. mediaplayer; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. surfaceholder; <br/> Import android. view. surfaceview; <br/> Import android. view. view; <br/>/** <br/> * @ description implement your own player through surfaceview/surfaceholder <br/> * @ autho R chenzheng_java <br/> * @ since 2011/03/23 <br/> * @ description here is a supplementary description, you can use mediaplayer to add onpreparedlistener <br/> * And oncompletionlistener to control the operations after preparation and playback. <Br/> * when using surfaceview and surfaceholder for video playback, the structure is as follows: <br/> * 1. First, we get a surfaceview from the layout file <br/> * 2. We use surfaceview. getholder () method to obtain the surfaceholder corresponding to the container <br/> * 3. Perform some default settings for srufaceholder, such as addcallback () and settype () <br/> * 4. Use mediaplayer. the setdisplay () method links the video playback and playback container <br/> */<br/> public class mymediaplayeractivity extends activity {</P> <p> mediaplayer; // The internal implementation of the player is through mediaplayer <br/> Surface View surfaceview; // The video container <br/> surfaceholder; // controls the surfaceview attributes (dimensions, formats, etc.) object <br/> Boolean ispause; // whether it has been paused </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. media); <br/> surfaceview = (surfaceview) findviewbyid (R. id. surfaceview1); <br/>/** <br/> * obtain the surefaceholder associated with the current surfaceview. <B R/> */<br/> surfaceholder = surfaceview. getholder (); <br/>/** <br/> * Registration of the method to be executed when surfaceview is created, changed, and destroyed <br/> */<br/> surfaceholder. addcallback (New surfaceholder. callback () {</P> <p> @ override <br/> Public void surfacedestroyed (surfaceholder holder) {<br/> log. I ("notification", "surfaceholder is destroyed"); <br/> If (mediaplayer! = NULL) <br/> mediaplayer. release (); <br/>}</P> <p> @ override <br/> Public void surfacecreated (surfaceholder holder) {<br/> log. I ("notification", "surfaceholder created"); <br/>}</P> <p> @ override <br/> Public void surfacechanged (surfaceholder holder, int format, int width, <br/> int height) {<br/> log. I ("notification", "surfaceholder changed"); <br/>}< br/> }); </P> <p>/** <br/> * surfaceholder must be set here. surface_type_push_buffers. settype (surfaceholder. surface_type_push_buffers ); <br/>}</P> <p>/*** <br/> * @ Param targetbutton button clicked by the user <br/> */<br/> Public void buttonclick (view targetbutton) {<br/> int buttonid = targetbutton. GETID (); <br/> switch (buttonid) {<br/> case R. id. button_play: <br/> play (); <br/> break; <br/> case R. id. button_pause: <br/> pause (); <br/> break; <br/> case R. id. button_reset: <br/> Reset (); <br/> break; <br/> case R. id. button_stop: <br/> stop (); <br/> break; <br/> default: <br/> break; <br/>}</P> <p>/** <br/> * play <br/> */<br/> private void play () {</P> <p> mediaplayer = new mediaplayer (); <br/> // set the multimedia stream type <br/> mediaplayer. setaudiostreamtype (audiomanager. stream_music); </P> <p> // set the container used to display mediaplayer <br/> mediaplayer. setdisplay (surfaceholder); <br/> try {<br/> mediaplayer. setdatasource ("/data/jinsha.3gp"); <br/> mediaplayer. prepare (); <br/> mediaplayer. start (); <br/> ispause = false; <br/>} catch (exception e) {<br/> log. I ("notice", "An error occurred during playback "); <br/>}</P> <p>/** <br/> * suspend <br/> */<br/> private void pause () {<br/> log. I ("notification", "click the pause button"); <br/> If (ispause = false) {<br/> mediaplayer. pause (); <br/> ispause = true; <br/>} else {<br/> mediaplayer. start (); <br/> ispause = false; <br/>}</P> <p>/** <br/> * reset <br/> */<br/> private void reset () {<br/> log. I ("notification", "click the reset button"); <br/> // jump to the beginning of the video <br/> mediaplayer. seekto (0); <br/> mediaplayer. start (); <br/>}</P> <p>/** <br/> * Stop <br/> */<br/> private void stop () {<br/> log. I ("notification", "click the stop button"); <br/> mediaplayer. stop (); <br/> mediaplayer. release (); </P> <p >}< br/>