准备工作:
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();