1、在命令行下輸入mksdcard 512M c:\sdcard.img命令,建立sdcard鏡像文件。java
2、單擊Eclipse的菜單命令「Window/Preferences」,打開Preferences窗口,在左側選中「Android/Launch」,在右側的「Default emulator options」中填入「-sdcard c:\sdcard.img」。android
3、啓動模擬器,切換視圖到DDMS,找到「File Explorer」窗口,選中「mnt/sdcard」,執行右上角的「Push a file onto the device」,向sdcard中添加視頻文件「qtfy01.3gp」。app
4、編寫項目的佈局文件main.xml,代碼以下:ide
5、編寫MainActivity.java代碼。
- <?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/surfaceView"
- android:layout_width="fill_parent"
- android:layout_height="380px"
- />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <Button
- android:id="@+id/play_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="播放"
- />
- <Button
- android:id="@+id/pause_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="暫停"
- />
- </LinearLayout>
- </LinearLayout>
- package game.test;
- 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 MainActivity extends Activity implements SurfaceHolder.Callback {
- String path = "/sdcard/qtfy01.3gp";
- Button play_btn, pause_btn;
- boolean isPause = false;
- SurfaceHolder surfaceHolder;
- MediaPlayer mediaPlayer;
- SurfaceView surfaceView;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- play_btn = (Button) findViewById(R.id.play_btn);
- play_btn.setOnClickListener(video_listener);
- pause_btn = (Button) findViewById(R.id.pause_btn);
- pause_btn.setOnClickListener(video_listener);
- getWindow().setFormat(PixelFormat.UNKNOWN);
- surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
- surfaceHolder = surfaceView.getHolder();
- surfaceHolder.addCallback(this);
- surfaceHolder.setFixedSize(176, 144);
- surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- mediaPlayer = new MediaPlayer();
- }
- private OnClickListener video_listener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.play_btn:
- isPause = false;
- playVideo(path);
- break;
- case R.id.pause_btn:
- if (isPause == false) {
- mediaPlayer.pause();
- isPause = true;
- } else {
- mediaPlayer.start();
- isPause = false;
- }
- break;
- }
- }
- private void playVideo(String path) {
- // TODO Auto-generated method stub
- if (mediaPlayer.isPlaying() == true) {
- mediaPlayer.reset();
- }
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- mediaPlayer.setDisplay(surfaceHolder);
- try {
- mediaPlayer.setDataSource(path);
- mediaPlayer.prepare();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- mediaPlayer.start();
- }
- };
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- }