使用MediaPlayer可以播放音频,也可以播放视频,
播放视频时考虑到图片刷新频率高,可以使用SerfaceView组件完成。
本程序只能播放.3gp格式的视频,要想播放其他格式的视频需要使用其他算法。
在sdcard里面存放一个名为Vieo.3gp的视频文件。
在main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal">
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"/>
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"/>
</LinearLayout>
<SurfaceView
android:id="@+id/surfaceView"
android:layout_margin="50dp"
android:layout_width="fill_parent"
android:layout_height="180dp"/>
</LinearLayout>
在MyVideoDemo.java中:
package com.li.video;
import java.io.IOException;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.support.v4.app.NavUtils;
public class MyVideoDemo extends Activity {
private ImageButton play = null;
private ImageButton stop = null;
private MediaPlayer media = null;
private SurfaceView surfaceView = null;
private SurfaceHolder surfaceHolder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.play = (ImageButton)super.findViewById(R.id.play);
this.stop = (ImageButton)super.findViewById(R.id.stop);
this.surfaceView = (SurfaceView)super.findViewById(R.id.surfaceView);
this.surfaceHolder = this.surfaceView.getHolder();
this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.media = new MediaPlayer();
try {
this.media.setDataSource("/sdcard/wmv.3gp");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.play.setOnClickListener(new PlayOnClickListener());
this.stop.setOnClickListener(new StopOnClickListener());
}
private class PlayOnClickListener implements OnClickListener{
public void onClick(View v) {
MyVideoDemo.this.media.setAudioStreamType(AudioManager.STREAM_MUSIC);
MyVideoDemo.this.media.setDisplay(MyVideoDemo.this.surfaceHolder);
try {
MyVideoDemo.this.media.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MyVideoDemo.this.media.start();
}
}
private class StopOnClickListener implements OnClickListener{
public void onClick(View v) {
MyVideoDemo.this.media.stop();
}
}
}