PlayvodActivity.java:
package com.zxn;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.widget.TextView;
import android.widget.Toast;
public class PlayvodActivity extends Activity implements Callback,
MediaPlayer.OnPreparedListener, OnBufferingUpdateListener,
OnCompletionListener
{
/** Called when the activity is first created. */
private TextView mTextView01;
private MediaPlayer mMediaPlayer01;
private SurfaceView mSurfaceView01;
private SurfaceHolder mSurfaceHolder01;
private String strVideoPath = "";
private static final String TAG = "MediaPlayer";
private int mVideoWidth;
private int mVideoHeight;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (!checkSDCard()) {
/* 提醒User未安装SD存储卡 */
mMakeTextToast(getResources().getText(R.string.str_err).toString(),
true);
}
mTextView01 = (TextView) findViewById(R.id.myTextView1);
mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1);
mSurfaceHolder01 = mSurfaceView01.getHolder();
mSurfaceHolder01.addCallback(this);
// mSurfaceHolder01.setFixedSize(520, 220);
mSurfaceHolder01.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private boolean checkSDCard() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
public void mMakeTextToast(String str, boolean isLong) {
if (isLong == true) {
Toast.makeText(PlayvodActivity.this, str, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(PlayvodActivity.this, str, Toast.LENGTH_SHORT)
.show();
}
}
private void playVideo() {
strVideoPath = "/sdcard/test.3gp";
// strVideoPath = "/sdcard/VIDEO0008.3gp";
mMediaPlayer01 = new MediaPlayer();
try {
mMediaPlayer01.setDataSource(strVideoPath);
} catch (Exception e) {
mTextView01.setText("setDataSource Exceeption:" + e.toString());
e.printStackTrace();
}
mMediaPlayer01.setDisplay(mSurfaceHolder01);
try {
// mMediaPlayer01.prepare();
mMediaPlayer01.prepareAsync();
} catch (Exception e) {
// TODO Auto-generated catch block
mTextView01.setText("prepare Exceeption:" + e.toString());
e.printStackTrace();
}
mMediaPlayer01.setOnBufferingUpdateListener(this);
mMediaPlayer01.setOnCompletionListener(this);
mMediaPlayer01.setOnPreparedListener(this);
mMediaPlayer01.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
Log.i(TAG, "Surface Changed");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.i(TAG, "Surface Changed");
playVideo();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.i(TAG, "Surface Destroyed");
super.onDestroy();
if (mMediaPlayer01 != null) {
mMediaPlayer01.release();
mMediaPlayer01 = null;
}
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.v(TAG, "onPrepared called");
mVideoWidth = mMediaPlayer01.getVideoWidth();
mVideoHeight = mMediaPlayer01.getVideoHeight();
if (mVideoWidth != 0 && mVideoHeight != 0)
{
/* 设置视频的宽度和高度 */
mSurfaceHolder01.setFixedSize(mVideoWidth, mVideoHeight);
/* 开始播放 */
mMediaPlayer01.start();
mTextView01.setText(R.string.str_play);
}
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
}
@Override
public void onCompletion(MediaPlayer mp) {
// 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"
>
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/blue"
android:text="@string/hello"
/>
<SurfaceView
android:id="@+id/mSurfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</SurfaceView>
/>
</LinearLayout>
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="blue">#0000FF</drawable>
</resources>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PlayvodActivity!</string>
<string name="app_name">Playvod</string>
<string name="str_err">※未发现SD卡※</string>
<string name="str_play">播放影片中</string>
</resources>
注意:将音乐放在SD卡里面.