android在视频上放view,android在SurfaceView上播放视频
首先,创建sdcard镜像文件,在命令行下输入:mksdcard 512M c:\sdcard.img命令启动eclipse,单击window/preferences菜单,找到Launch项,在Default emulator options 中填上c:\sdcard.img然后OK找到eclipse的File Explorer 窗口, 在/mnt/sdcard中导入3gp文件(我用的是zho..
首先,创建sdcard镜像文件,在命令行下输入:
mksdcard 512M c:\sdcard.img命令
启动eclipse,单击window/preferences菜单,找到Launch项,
在Default emulator options 中填上 c:\sdcard.img
然后OK
找到eclipse的File Explorer 窗口, 在/mnt/sdcard中导入 3gp文件(我用的是zhoupkding.3gp)
然后开始程序:
界面上:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/surfaceView"
android:layout_width="320px"
android:layout_height="200px"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
/>
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
/>
源码: public class VideoPlayActivity extends Activity implements OnClickListener, SurfaceHolder.Callback{
// 视频的路径
String videoPath = "/mnt/sdcard/zhoupkding.3gp";
Button playButton;
Button pauseButton;
boolean isPause = false;
SurfaceHolder surfaceHolder;
MediaPlayer mediaPlayer;
SurfaceView surfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playButton = (Button)findViewById(R.id.play);
playButton.setOnClickListener(this);
pauseButton = (Button)findViewById(R.id.pause);
pauseButton.setOnClickListener(this);
// 让window自己设置程序的背景样式
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
// 让surfaceView确定大小
surfaceHolder.setFixedSize(176, 144);
//Surface type: creates a "push" surface,
//that is a surface that doesn't owns its buffers.
//With such a surface lockCanvas will fail.
// 不加这个没有视频,只有声音,文档上说是需要的时候可以自动设置,但是并没有
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
}
@Override
public void onClick(View v) {
if (v == playButton) {
isPause = false;
playVideo(videoPath);
} else if (v == pauseButton) {
if (isPause == false) {
mediaPlayer.pause();
isPause = true;
} else {
mediaPlayer.start();
isPause = false;
}
}
}
private void playVideo(String path) {
if (mediaPlayer.isPlaying() == true) {
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//Sets the SurfaceHolder to use for displaying the video portion of the media.
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
更多推荐
所有评论(0)