本文介绍MediaPlayer的使用。MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用MediaPlayer,要视情况选择了。MediaPlayer播放音频比较简单,但是要播放视频就需要SurfaceView。SurfaceView比普通的自定义View更有绘图上的优势,它支持完全的OpenGL ES库。
       先贴出本文程序运行结果的截图,上面是播放/停止音频,可用SeekBar来调进度,下面是播放/停止视频,也是用SeekBar来调进度:
 
       main.xml的源码:
XML/HTML代码
    - <linearlayout android:id="@+id/LinearLayout01"   
-         android:layout_width="fill_parent" android:layout_height="fill_parent"  
-         xmlns:android="http://schemas.android.com/apk/res/android"  
-         android:orientation="vertical">  
-         <seekbar android:id="@+id/SeekBar01" android:layout_height="wrap_content"   
-                 android:layout_width="fill_parent">  
-         <linearlayout android:id="@+id/LinearLayout02"   
-                 android:layout_width="wrap_content" android:layout_height="wrap_content">  
-                 <button android:id="@+id/Button01" android:layout_width="wrap_content"   
-                         android:layout_height="wrap_content" android:text="播放音频">  
-                 <button android:id="@+id/Button02" android:layout_width="wrap_content"   
-                         android:layout_height="wrap_content" android:text="停止播放">  
-           
-         <seekbar android:id="@+id/SeekBar02" android:layout_height="wrap_content"   
-                 android:layout_width="fill_parent">  
-   
-         <surfaceview android:id="@+id/SurfaceView01"   
-                 android:layout_width="fill_parent" android:layout_height="250px">  
-         <linearlayout android:id="@+id/LinearLayout02"   
-                 android:layout_width="wrap_content" android:layout_height="wrap_content">  
-                 <button android:layout_width="wrap_content"   
-                         android:layout_height="wrap_content" android:id="@+id/Button03"  
-                         android:text="播放视频">  
-                 <button android:layout_width="wrap_content"   
-                         android:layout_height="wrap_content" android:text="停止播放" android:id="@+id/Button04">  
       本文程序的源码,有点长: