A Preliminary Study on Android Development Video Playback
/*
* Android development: Video Playback
* Beijing Android Club group: 167839253
* Created on: 2011-8-24
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
There are some differences between video playback and audio playback in Android. A space is required to store video files. It takes a bit of effort to play a video. The program code is very simple. However, this problem may occur during playback. The following describes the steps:
1. Create an image file for sdcard. You can enter mksdcard 512 m c: \ sdcard. IMG in the command line.
2. Find the preferences menu in the eclipse window, click Android-> launch, and enter "-sdcard c: \ sdcard. IMG" in default emulator options"
3. Run the simulator
In some cases, it cannot be successful and can be pushed directly to the simulator. Be sure to select a smaller 3GP file.
The Code is as follows:
/** Playback of Android development videos * myvideo. java * created on: 2011-8-24 * Author: blueeagle * Email: liujiaxiang@gmail.com */package COM. blueeagle; import android. app. activity; import android. graphics. pixelformat; import android. media. audiomanager; import android. media. mediaplayer; import android. OS. bundle; import android. view. surfaceholder; import android. view. surfaceview; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class myvideo extends activity implements onclicklistener, surfaceholder. callback {string Path = "/data/222.3gp"; button play_button; button pause_button; Boolean ispause = false; surfaceview mysurfaceview; mediaplayer mymediaplayer; surfaceholder; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); play_button = (button) findviewbyid (R. id. mybutton); play_button.setonclicklistener (this); pause_button = (button) findviewbyid (R. id. mybutton2); pause_button.setonclicklistener (this); getwindow (). setformat (pixelformat. unknown); mysurfaceview = (surfaceview) findviewbyid (R. id. mysurfaceview); surfaceholder = mysurfaceview. getholder (); surfaceholder. addcallback (this); surfaceholder. setfixedsize (176,144); surfaceholder. settype (surfaceholder. surface_type_push_buffers); mymediaplayer = new mediaplayer () ;}@ overridepublic void onclick (view v) {// todo auto-generated method stubif (V = play_button) {ispause = false; system. out. println (PATH); playvideo (PATH);} else if (V = pause_button) {If (ispause = false) {mymediaplayer. pause (); ispause = true;} else {mymediaplayer. start (); ispause = false ;}} private void playvideo (string strpath) {If (mymediaplayer. isplaying () = true) {mymediaplayer. reset ();} mymediaplayer. setaudiostreamtype (audiomanager. stream_music); mymediaplayer. setdisplay (surfaceholder); // set the video to play with surfaceholder try {mymediaplayer. setdatasource (strpath); mymediaplayer. prepare ();} catch (exception e) {e. printstacktrace ();} mymediaplayer. start () ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stub} @ overridepublic void surfacecreated (surfaceholder holder) {// todo auto-generated method stub} @ overridepublic void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stub }}
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <surfaceview Android: id = "@ + ID/mysurfaceview" Android: layout_width = "320px" Android: layout_height = "200px"> </surfaceview> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <buttonandroid: Id = "@ + ID/mybutton" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = ""/> <buttonandroid: Id = "@ + ID/mybutton2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "pause"/> </linearlayout>
The running result is as follows: