準備工作:
1.SurfaceView
provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen
2.SurfaceHolder
Abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. This interface is typically available through theSurfaceView
class.
3.MediaPlayer.
4.播放檔案資料來源
---------------------------------------------------------------------------------------------------------------------------------
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="320px"
android:layout_height="240px"
/>
1.獲得SurfaceView --------surfaceview=(SurfaceView)findViewById(R.id.surfaceview);
2.獲得SurfaceHolder-------surfaceholder=surfaceview.getHolder();
surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//設定顯示內容是從外部壓入的
surfaceholder.addCallback(this);//此處需要一個實現SurfaceHolder.Callback介面的類執行個體
3.執行個體化一個播放器mediaplayer= new MediaPlayer();
mediaplayer.setDataSource("http://www.bogotobogo.com/Video/sample.3gp");//設定資料來源
mediaplayer.setDisplay(surfaceholder);//設定顯示到剛才的surfaceview
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//設定流格式
mediaplayer.prepare();
mediaplayer.start();