目的:实现在程序中播放本地视频。想到用VideoView,但是出现了问题,如题目描述。上网也查找了些资料,估计是
关键字不够精确,所以没找到答案。大家帮帮忙,能告诉我出现这个问题的原因或者给我一些提示、思路怎样实现在
程序中播放本地视频(有什么插件之类的吗?)
------解决方案--------------------
停止播放是否出现什么提示?如果有错误提示,将Logcat中的出错信息发上来。
------解决方案--------------------
- Java code
package com.zhu.view;import java.io.File;import android.app.Activity;import android.content.Intent;import android.media.MediaPlayer;import android.media.MediaPlayer.OnPreparedListener;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.KeyEvent;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Toast;import com.zhu.R;public class VideoPlay extends Activity implements Callback { private String path; private MediaPlayer mediaPlayer; String pathload; private SurfaceView surfaceView; private boolean pause; private String filenames; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 隐藏状态栏 */ this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); /* 隐藏标题栏 */ requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.video); mediaPlayer = new MediaPlayer(); Bundle bundle = this.getIntent().getExtras(); filenames = bundle.getString("filenames"); pathload = bundle.getString("pathload"); surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView); surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceView.getHolder().setFixedSize(320, 180); surfaceView.getHolder().setKeepScreenOn(true); surfaceView.getHolder().addCallback(this); playOn(); } public void mediaplay(View v) { switch (v.getId()) { case R.id.playbutton: if (pause && mediaPlayer!=null) { mediaPlayer.start(); pause = false; } else{ String filename = filenames.toString(); File file = new File(Environment.getExternalStorageDirectory(), filename); if (file.exists()) { path = file.getAbsolutePath(); play(0); } else { path = null; Toast.makeText(this, R.string.filenoexsit, Toast.LENGTH_LONG).show(); } } break; case R.id.pausebutton: if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); pause = true; } else { if (pause) { mediaPlayer.start(); pause = false; } } break; case R.id.resetbutton: if (mediaPlayer.isPlaying()) { mediaPlayer.seekTo(0); } else { if (path != null) { play(0); } } break; case R.id.stopbutton: if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } break; } } private void play(int position) { try { mediaPlayer.reset(); mediaPlayer.setDataSource(path); mediaPlayer.setDisplay(surfaceView.getHolder()); mediaPlayer.prepare();// 缓冲 mediaPlayer.setOnPreparedListener(new PrepareListener(position)); } catch (Exception e) { e.printStackTrace(); } } private void playOn() { try { mediaPlayer.reset(); mediaPlayer.setDataSource(pathload); mediaPlayer.setDisplay(surfaceView.getHolder()); mediaPlayer.prepare();// 缓冲 mediaPlayer.start(); } catch (Exception e) { e.printStackTrace(); } } private final class PrepareListener implements OnPreparedListener { private int position; public PrepareListener(int position) { this.position = position; } public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); if (position > 0) mediaPlayer.seekTo(position); } } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub }}