我们知道Android中可以使用VideoView播放视频,而且操作简单、方便。除此之外,我们还可以利用MediaPlayer用来播放视频,不过MediaPlayer主要用于播放音频,没有提供输出图像的输出界面,就是只能听到声音,看不到画面,这时就想到到了SurfaceView控件,将它与MediaPlayer结合起来,就能达到了视屏播放效果了,今天就做一个简单的视屏播放功能,把准备好的视屏文件test.3gp放在手机SD卡中,然后用SurfaceView来播放:
先看布局activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceVIew"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.15"
tools:context=".MainActivity" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.85">
<Button
android:id="@+id/btn_play"
android:text="开始"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_pause"
android:text="暂停"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_stop"
android:text="停止"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_low"
android:text="调低音量"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_hight"
android:text="调高音量"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
接着就是Activity:
public class MainActivity extends Activity implements OnClickListener {
/* 功能按钮 */
private Button btn_play, btn_pause, btn_stop, btn_low, btn_height;
/* SurfaceView */
private SurfaceView mSurfaceView;
/* 播放视频对象 */
private MediaPlayer mediaPlayer;
/* 系统声音 */
private AudioManager audioManager;
/* 记录播放位置 */
private int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*设置窗口无title*/
requestWindowFeature(Window.FEATURE_NO_TITLE);
/*全屏显示*/
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
findViews();
setListeners();
mediaPlayer = new MediaPlayer();
setSurfaceView();
}
// 横竖屏切换时的处理
@Override
protected void onSaveInstanceState(Bundle outState) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {// 如果在播放的时候切换屏幕则保存当前观看的位置
outState.putInt("position", mediaPlayer.getCurrentPosition());
}
super.onSaveInstanceState(outState);
}
// 横竖屏切换后的处理
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.containsKey("position")) {
position = savedInstanceState.getInt("position");// 取得切换屏幕时保存的位置
}
super.onRestoreInstanceState(savedInstanceState);
}
/* 实例化UI */
private void findViews() {
btn_play = (Button) findViewById(R.id.btn_play);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_low = (Button) findViewById(R.id.btn_low);
btn_height = (Button) findViewById(R.id.btn_hight);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceVIew);
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
}
/* 为5个按钮设置监听 */
private void setListeners() {
btn_play.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_low.setOnClickListener(this);
btn_height.setOnClickListener(this);
}
/* 播放视频 */
private void playMedia() {
/* 初始化状态 */
mediaPlayer.reset();
/* 设置声音流类型 */
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
/* 设置mp3\mp4加载路径 */
File file = new File(Environment.getExternalStorageDirectory(), "test.3gp");
try {
mediaPlayer.setDataSource(file.getAbsolutePath());
// 缓冲
mediaPlayer.prepare();
// 开始播放
mediaPlayer.start();
// 具体位置
Log.i("position", position + "------");
mediaPlayer.seekTo(position);
// 视频输出到View
mediaPlayer.setDisplay(mSurfaceView.getHolder());
// 重置位置为0
position = 0;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* 设置surfaceView视图 */
private void setSurfaceView() {
// creates a "push" surface
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 设置事件,回调函数
mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
/* SurfaceView创建时 */
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (position > 0) {
playMedia();
position = 0;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
/* SurfaceView销毁视图 */
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
/* Activity销毁时停止播放,释放资源。不做这个操作,即使退出,还是能听到视频的声音 */
mediaPlayer.release();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:// 播放
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
return;
} else {
playMedia();
}
break;
case R.id.btn_pause:// 暂停
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
} else {
return;
}
break;
case R.id.btn_stop:// 停止
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
position = 0;
} else {
return;
}
break;
case R.id.btn_low:// 调小音量
// 获取当前的音乐音量
int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.v("Volume", "volume" + volume);
// 音量>0
if (volume > 0) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, 0);
} else {
return;
}
break;
case R.id.btn_hight:// 调大音量
volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// 音量<100
if (volume < audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
} else {
return;
}
break;
}
}
}
ok,就不贴图了。