/*
* Android开发之初探视频的播放
* 北京Android俱乐部群:167839253
* Created on: 2011-8-24
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
Android下视频的播放跟音频的播放是有一些区别的。需要有一个空间来存放视频文件。关于视频的播放其实费了一点功夫,程序代码很简单,但是对于播放,可能会出这样那样的问题,现在总结一下,步骤如下:
1.创建sdcard的镜像文件,可以在命令行下输入mksdcard 512M C:\sdcard.img
2.在Eclipse的window下找到Preferences菜单,然后点击Android->launch然后在Default emulator Options中填上“-sdcard C:\sdcard.img”
3.运行模拟器
有些时候,这样并不能成功,可以直接push到模拟器中。注意一定要选择一个小一点的3gp文件。
代码如下:
- /*
- * Android开发之视频的播放
- * MyVideo.java
- * Created on: 2011-8-24
- * Author: blueeagle
- * Email: liujiaxiang@gmail.com
- */
- package com.blueeagle;
- import android.app.Activity;
- import android.graphics.PixelFormat;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MyVideo extends Activity implements OnClickListener,SurfaceHolder.Callback{
- String path = "/data/222.3gp";
- Button play_button;
- Button pause_button;
- boolean isPause = false;
- SurfaceView mySurfaceView;
- MediaPlayer myMediaPlayer;
- SurfaceHolder surfaceHolder;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- play_button = (Button)findViewById(R.id.myButton);
- play_button.setOnClickListener(this);
- pause_button = (Button)findViewById(R.id.myButton2);
- pause_button.setOnClickListener(this);
- getWindow().setFormat(PixelFormat.UNKNOWN);
- mySurfaceView = (SurfaceView)findViewById(R.id.mySurfaceView);
- surfaceHolder = mySurfaceView.getHolder();
- surfaceHolder.addCallback(this);
- surfaceHolder.setFixedSize(176,144);
- surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- myMediaPlayer = new MediaPlayer();
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(v==play_button)
- {
- isPause = false;
- System.out.println(path);
- playVideo(path);
- }
- else if(v==pause_button){
- if(isPause == false){
- myMediaPlayer.pause();
- isPause = true;
- }
- else{
- myMediaPlayer.start();
- isPause = false;
- }
- }
- }
- private void playVideo(String strPath){
- if(myMediaPlayer.isPlaying()==true){
- myMediaPlayer.reset();
- }
- myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- myMediaPlayer.setDisplay(surfaceHolder);//设置Video影片以SurfaceHolder播放
- try{
- myMediaPlayer.setDataSource(strPath);
- myMediaPlayer.prepare();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- myMediaPlayer.start();
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <SurfaceView
- android:id = "@+id/mySurfaceView"
- android:layout_width="320px"
- android:layout_height="200px">
- </SurfaceView>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <Button
- android:id="@+id/myButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="播放"/>
- <Button
- android:id="@+id/myButton2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="暂停"/>
- </LinearLayout>
- </LinearLayout>
运行结果如下图: