截图:
其实视频播放器的实现与音乐播放器的实现没有太大的区别。主要体现在:
1)main.xml
增加了<ImageView />(用来显示图片按钮)、<SurfaceView />(用来作为屏幕)
2)MainActivity
其实大部分还是一样的。只是在MP3音乐播放器的基础上早合适的位置上增加了以下:
SurfaceHolder holder = surfaceView.getHolder(); holder.setFixedSize(176, 144);//设置分辨率 holder.setKeepScreenOn(true);//保持屏幕高亮 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//不维持缓冲,得到数据后直接输出
mp.setDisplay(surfaceView.getHolder());//设置显示画面
代码如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/white"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="女神牌mp4视频播放器" /> <EditText android:id="@+id/et_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="oppo.mp4" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/play" android:onClick="play" android:layout_marginRight="10dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pause" android:onClick="pause" android:layout_marginRight="10dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop" android:onClick="stop" android:layout_marginRight="10dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/reset" android:onClick="reset" android:layout_marginRight="10dp" /> </LinearLayout> <SurfaceView android:id="@+id/sv_info" android:layout_width="fill_parent" android:layout_height="224dp" /> </LinearLayout>
2、MainActivity
package com.NJUPT.mp4_1; import java.io.File; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.view.Menu; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private MediaPlayer mp; private File file; private EditText et_name; private boolean pause; private int position = 0; private SurfaceView surfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_name = (EditText) findViewById(R.id.et_name); surfaceView = (SurfaceView) findViewById(R.id.sv_info); SurfaceHolder holder = surfaceView.getHolder(); holder.setFixedSize(176, 144); holder.setKeepScreenOn(true); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void play(View v) { String name = et_name.getText().toString(); file = new File(Environment.getExternalStorageDirectory(), name); if (!file.exists()) { Toast.makeText(this, "sorry,视频文件不存在", 1).show(); } else { play(); } } public void play() { try { mp = new MediaPlayer(); mp.reset(); mp.setDataSource(file.getAbsolutePath()); mp.setDisplay(surfaceView.getHolder()); mp.prepare(); mp.setOnPreparedListener(new MyOnPrepareListener()); } catch (Exception e) { e.printStackTrace(); } } private class MyOnPrepareListener implements OnPreparedListener { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } } public void pause(View v) { if (mp != null) { if (mp.isPlaying()) { mp.pause(); pause = true; } else { mp.start(); pause = false; } } } public void stop(View v) { if (mp != null) { mp.stop(); } } public void reset(View v) { stop(v); play(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
http://download.csdn.net/detail/caihongshijie6/6285127