用VideoView和SurfaceView播放视频
播放视频可以用ViewView,也可以用SurfaceView,VideoView的方式是最简单的,代码如下:
定义一个<VideoView>标签
<VideoView android:id="@+id/videoView" android:layout_width="320px"
android:layout_height="240px" />
Java代码:
videoView.setVideoURI(Uri.parse("file:///sdcard/test.3gp"));
videoView.setMediaController(new MediaController(this));
videoView.start();
虽然VideoView可以很容易地播放视频,但播放位置和播放大小并不受控制,因此,需要用SurfaceView来播放视频,代码如下:
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFixedSize(100, 100);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try
{
mediaPlayer.setDataSource("sdcard/test.3gp");
mediaPlayer.prepare();
mediaPlayer.start();
}
catch (Exception e)
{
}
定义一个<VideoView>标签
<VideoView android:id="@+id/videoView" android:layout_width="320px"
android:layout_height="240px" />
Java代码:
videoView.setVideoURI(Uri.parse("file:///sdcard/test.3gp"));
videoView.setMediaController(new MediaController(this));
videoView.start();
虽然VideoView可以很容易地播放视频,但播放位置和播放大小并不受控制,因此,需要用SurfaceView来播放视频,代码如下:
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFixedSize(100, 100);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try
{
mediaPlayer.setDataSource("sdcard/test.3gp");
mediaPlayer.prepare();
mediaPlayer.start();
}
catch (Exception e)
{
}