视频播放的代码
2013-01-04 21:54
302 查看
1。j界面设计代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFCC"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/video_text" />
<EditText
android:id="@+id/videoFileEt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cat.3gp" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:stretchColumns="*" >
<TableRow>
<ImageButton
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/pauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/resetBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reset" />
<ImageButton
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop" />
</TableRow>
</TableLayout>
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="240dip" />
</LinearLayout>
2.string的文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">视频播放器</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="video_text">视频文件</string>
<string name="notfoundsdcard_error">找不到Sd卡</string>
<string name="notfoundfile_error">找不到视频文件</string>
</resources>
3。activity的java代码
package com.example.radioplayer1;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "VideoPlayerActivity";
private ImageButton playBtn, pauseBtn, resetBtn, stopBtn;
private EditText fileEt;
private SurfaceView videoSv;
private MediaPlayer mediaPlayer;
private SurfaceHolder holder;
private int position = 0;
File videoFile = null;
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
position = savedInstanceState.getInt("position");
String fn = savedInstanceState.getString("fileName");
if(fn!=null){
videoFile = new File(Environment.getExternalStorageDirectory(),fn);
}
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("position", position);
outState.putString("fileName", videoFile.getName());
super.onSaveInstanceState(outState);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
mediaPlayer = new MediaPlayer();
}
private void findViews() {
fileEt = (EditText) this.findViewById(R.id.videoFileEt);
playBtn = (ImageButton) this.findViewById(R.id.playBtn);
pauseBtn = (ImageButton) this.findViewById(R.id.pauseBtn);
resetBtn = (ImageButton) this.findViewById(R.id.resetBtn);
stopBtn = (ImageButton) this.findViewById(R.id.stopBtn);
playBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
resetBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
videoSv = (SurfaceView) this.findViewById(R.id.surfaceView);
holder = videoSv.getHolder();
holder.setFixedSize(176, 144); // 设置分辨率
/* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(new SurfaceCallback());
}
public void onClick(View v) {
if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {
String fileName = fileEt.getText().toString().trim();
videoFile = new File(
Environment.getExternalStorageDirectory(), fileName);
if (videoFile.exists()) {
try {
switch (v.getId()) {
case R.id.playBtn:
playVideo(videoFile);
break;
case R.id.pauseBtn:
if(mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
/* if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}*/
break;
case R.id.resetBtn:
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}else{
playVideo(videoFile);
}
break;
case R.id.stopBtn:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
} else {
showToast(R.string.notfoundfile_error);
}
} else {
showToast(R.string.notfoundsdcard_error);
}
}
private void playVideo(File videoFile) throws IOException {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(videoFile.getAbsolutePath());
mediaPlayer.setDisplay(holder);
mediaPlayer.prepare();
mediaPlayer.start();
}
private void showToast(int resId) {
Toast.makeText(this, resId, 1).show();
}
protected void onDestroy() {
if(mediaPlayer != null){
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.release();
}
super.onDestroy();
}
/* @Override
protected void onPause() {
if(mediaPlayer != null && mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if(position >0 && videoFile != null){
try {
playVideo(videoFile);
Log.i(TAG,"position="+ position);
mediaPlayer.seekTo(position);
position = 0;
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}
super.onResume();
}*/
private final class SurfaceCallback implements Callback{
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(position >0 && videoFile != null){
try {
playVideo(videoFile);
Log.i(TAG,"position="+ position);
mediaPlayer.seekTo(position);
position = 0;
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}
Log.i(TAG, "surfaceCreated");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mediaPlayer != null && mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
Log.i(TAG, "surfaceDestroyed");
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFCC"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/video_text" />
<EditText
android:id="@+id/videoFileEt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cat.3gp" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:stretchColumns="*" >
<TableRow>
<ImageButton
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/pauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/resetBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reset" />
<ImageButton
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop" />
</TableRow>
</TableLayout>
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="240dip" />
</LinearLayout>
2.string的文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">视频播放器</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="video_text">视频文件</string>
<string name="notfoundsdcard_error">找不到Sd卡</string>
<string name="notfoundfile_error">找不到视频文件</string>
</resources>
3。activity的java代码
package com.example.radioplayer1;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "VideoPlayerActivity";
private ImageButton playBtn, pauseBtn, resetBtn, stopBtn;
private EditText fileEt;
private SurfaceView videoSv;
private MediaPlayer mediaPlayer;
private SurfaceHolder holder;
private int position = 0;
File videoFile = null;
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
position = savedInstanceState.getInt("position");
String fn = savedInstanceState.getString("fileName");
if(fn!=null){
videoFile = new File(Environment.getExternalStorageDirectory(),fn);
}
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("position", position);
outState.putString("fileName", videoFile.getName());
super.onSaveInstanceState(outState);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
mediaPlayer = new MediaPlayer();
}
private void findViews() {
fileEt = (EditText) this.findViewById(R.id.videoFileEt);
playBtn = (ImageButton) this.findViewById(R.id.playBtn);
pauseBtn = (ImageButton) this.findViewById(R.id.pauseBtn);
resetBtn = (ImageButton) this.findViewById(R.id.resetBtn);
stopBtn = (ImageButton) this.findViewById(R.id.stopBtn);
playBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
resetBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
videoSv = (SurfaceView) this.findViewById(R.id.surfaceView);
holder = videoSv.getHolder();
holder.setFixedSize(176, 144); // 设置分辨率
/* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(new SurfaceCallback());
}
public void onClick(View v) {
if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {
String fileName = fileEt.getText().toString().trim();
videoFile = new File(
Environment.getExternalStorageDirectory(), fileName);
if (videoFile.exists()) {
try {
switch (v.getId()) {
case R.id.playBtn:
playVideo(videoFile);
break;
case R.id.pauseBtn:
if(mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
/* if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}*/
break;
case R.id.resetBtn:
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}else{
playVideo(videoFile);
}
break;
case R.id.stopBtn:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
} else {
showToast(R.string.notfoundfile_error);
}
} else {
showToast(R.string.notfoundsdcard_error);
}
}
private void playVideo(File videoFile) throws IOException {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(videoFile.getAbsolutePath());
mediaPlayer.setDisplay(holder);
mediaPlayer.prepare();
mediaPlayer.start();
}
private void showToast(int resId) {
Toast.makeText(this, resId, 1).show();
}
protected void onDestroy() {
if(mediaPlayer != null){
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.release();
}
super.onDestroy();
}
/* @Override
protected void onPause() {
if(mediaPlayer != null && mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if(position >0 && videoFile != null){
try {
playVideo(videoFile);
Log.i(TAG,"position="+ position);
mediaPlayer.seekTo(position);
position = 0;
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}
super.onResume();
}*/
private final class SurfaceCallback implements Callback{
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(position >0 && videoFile != null){
try {
playVideo(videoFile);
Log.i(TAG,"position="+ position);
mediaPlayer.seekTo(position);
position = 0;
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}
Log.i(TAG, "surfaceCreated");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mediaPlayer != null && mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
Log.i(TAG, "surfaceDestroyed");
}
}
}
相关文章推荐
- HTML5 播放视频代码
- 网页播放的视频代码
- jquery html5 视频播放控制代码
- 网页中插入视频播放代码全集
- HTML播放视频代码
- jsp视频播放代码 avi
- 视频播放代码 正在做 还没有调试 成功
- 网页中插入视频播放代码集
- 在线视频播放代码
- 在网页中插入视频播放代码全集
- 网页播放的视频代码
- iphone模拟器播放视频代码
- HTML5 播放视频代码
- 网页播放视频解决代码
- 常用的视频格式文件WEB端播放代码
- 网页中播放视频文件的代码
- Android 仿今日头条视频播放控件JieCaoVideoPlayer(几行代码快速实现)
- 播放源代码器全集(网页中插入视频播放代码)
- Web网页中使用Media Player播放MPG视频的代码,并且支持JavaScript控制播放,暂停等功能
- OpenCV 播放视频的QT 代码