[Android] Simple player source code for Video Playback

Source: Internet
Author: User

1. mainactivity. Java source code

Import android. app. activity; import android. graphics. pixelformat; import android. media. audiomanager; import android. media. mediaplayer; import android. OS. bundle; import android. util. log; import android. view. surfaceholder; import android. view. surfaceview; import android. view. view; import android. widget. imagebutton; import android. widget. textview; import android. widget. toast; public class ex07_14 extends Activity Implements surfaceholder. callback {private textview mtextview01; Private Static final string tag = "hippo_mediaplayer"; // print the log flag private mediaplayer identifier; private surfaceview msurfaceview01; private surfaceholder msurfaceholder01; private imagebutton mplay; private imagebutton mpause; private imagebutton mreset; private imagebutton mstop; private Boolean bispaused = false; private Boole An bisreleased = false; private string strvideopath = ""; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); If (! Checksdcard () // if there is no SD card {mmaketexttoast (getresources (). gettext (R. string. str_err_nosd ). tostring (), true);} mtextview01 = (textview) findviewbyid (R. id. mytextview1); getwindow (). setformat (pixelformat. unknown); msurfaceview01 = (surfaceview) findviewbyid (R. id. msurfaceview1); // The container msurfaceholder01 for animation display = msurfaceview01.getholder (); msurfaceholder01.addcallback (this); msurfaceholder01.setfixedsize (176,144); msurfaceholder01.settype (surfaceholder. surface_type_push_buffers); mplay = (imagebutton) findviewbyid (R. id. play); mpause = (imagebutton) findviewbyid (R. id. pause); mreset = (imagebutton) findviewbyid (R. id. reset); mstop = (imagebutton) findviewbyid (R. id. stop); strvideopath = "/sdcard/a.3gp"; mplay. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view) {If (CH Ecksdcard () {playvideo (strvideopath) ;}}); mpause. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view) {If (checksdcard () {If (mmediaplayer01! = NULL) {If (bisreleased = false) {If (bispaused = false) {mmediaplayer01.pause (); bispaused = true; mtextview01.settext (R. string. str_pause);} else if (bispaused = true) {mmediaplayer01.start (); bispaused = false; mtextview01.settext (R. string. str_play) ;}}}}); mreset. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view) {If (checksdcard () {If (bisreleased = False) {If (mmediaplayer01! = NULL) {mmediaplayer01.seekto (0) ;}}}}); mstop. setonclicklistener (New imagebutton. onclicklistener () {public void onclick (view) {If (checksdcard () {If (mmediaplayer01! = NULL) {If (bisreleased = false) {mmediaplayer01.stop (); mmediaplayer01.release (); bisreleased = true; mtextview01.settext (R. string. str_stop) ;}}}});} private void playvideo (string strpath) {mmediaplayer01 = new mediaplayer (); mmediaplayer01.setaudiostreamtype (audiomanager. stream_music); streams (msurfaceholder01); try {mmediaplayer01.setdatasource (strpath);} catch (exception e) {// todo auto-generated Catch Block mtextview01.settext ("setdatasource exceeption:" + E. tostring ();} Try {mmediaplayer01.prepare ();} catch (exception e) {// todo auto-generated Catch Block mtextview01.settext ("Prepare exceeption:" + E. tostring ();} mmediaplayer01.start (); bisreleased = false; mtextview01.settext (R. string. str_play); mmediaplayer01.setoncompletionlistener (New mediaplayer. oncompletionlistener () {@ override public void oncompletion (mediaplayer arg0) {// todo auto-generated method stub mtextview01.settext (R. string. str_stop) ;}}) ;}private Boolean checksdcard () {If (Android. OS. environment. getexternalstoragestate (). equals (Android. OS. environment. media_mounted) {return true;} else {return false;} public void mmaketexttoast (string STR, Boolean islong) {If (islong = true) {toast. maketext (ex07_14.this, STR, toast. length_long ). show ();} else {toast. maketext (ex07_14.this, STR, toast. length_short ). show () ;}@ override public void surfacechanged (surfaceholder, int format, int W, int h) {// todo auto-generated method stub log. I (TAG, "Surface Changed") ;}@ override public void surfacecreated (surfaceholder) {// todo auto-generated method stub log. I (TAG, "Surface Changed") ;}@ override public void surfacedestroyed (surfaceholder) {// todo auto-generated method stub log. I (TAG, "surface destroyed ");}}

Ii. Main. xml source code

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:background="@drawable/white"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <TextView    android:id="@+id/myTextView1"    android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:textColor="@drawable/blue"     android:text="@string/hello"  />  <SurfaceView    android:id="@+id/mSurfaceView1"     android:layout_width="100px"     android:layout_height="100px">   </SurfaceView>    <SeekBar android:id="@+id/seekBar" android:layout_height="wrap_content" android:layout_width="fill_parent" />       <LinearLayout     android:orientation="horizontal"     android:layout_height="wrap_content"     android:layout_width="fill_parent"     android:padding="10dip"   >  <ImageButton android:id="@+id/play"     android:layout_height="wrap_content"     android:layout_width="wrap_content"    android:src="@drawable/play"  />   <ImageButton android:id="@+id/pause"    android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:src="@drawable/pause"  />   <ImageButton android:id="@+id/reset"     android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:src="@drawable/reset"  />   <ImageButton android:id="@+id/stop"     android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:src="@drawable/stop"  />   </LinearLayout> </LinearLayout>

Third, the remaining images are required in the source code. You can bring your own images. You can play the video after testing. You must input a playback file in the sdcard.

Related Article

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.