我正在创建一个录像机,并希望创建视频播放器来预览录制的视频.修改代码from this page我已经通过以下方式创建了一个MediaPrevIEw类:
public class MediaPrevIEw extends Activity implements one rrorListener, OnBufferingUpdateListener,OnCompletionListener, OnPreparedListener, SurfaceHolder.Callback{private static final String TAG = "MediaPrevIEw";private MediaPlayer mp;private SurfaceVIEw mPrevIEw;private SurfaceHolder holder;private button btnPlay;private button btnPause;private button btnreset;private button btnStop;private String mPath;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.media_prevIEw); mPrevIEw = (SurfaceVIEw)findVIEwByID(R.ID.mPrevIEw); btnPlay = (button)findVIEwByID(R.ID.btnPlay); btnPause = (button)findVIEwByID(R.ID.btnPause); btnreset = (button)findVIEwByID(R.ID.btnreset); btnStop = (button)findVIEwByID(R.ID.btnStop); getPathFromParentDialog(); btnPlay.setonClickListener(new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { playVIDeo(); } }); btnPause.setonClickListener(new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub if(mp != null){ mp.pause(); } } }); btnreset.setonClickListener(new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { if(mp != null){ mp.seekTo(0); } } }); btnStop.setonClickListener(new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { if(mp != null){ mp.stop(); mp.release(); } } }); getwindow().setFormat(PixelFormat.transparent); holder = mPrevIEw.getHolder(); holder.addCallback(this); holder.setFixedSize(400, 300);}@OverrIDeprotected voID onResume() { setRequestedOrIEntation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); super.onResume();}@OverrIDepublic boolean one rror(MediaPlayer mp, int what, int extra) { if(mp != null){ mp.stop(); mp.release(); } return false;}@OverrIDepublic voID onBufferingUpdate(MediaPlayer mp, int percent) { // Todo auto-generated method stub}@OverrIDepublic voID onCompletion(MediaPlayer mp) { // Todo auto-generated method stub}@OverrIDepublic voID onPrepared(MediaPlayer mp) { // Todo auto-generated method stub}@OverrIDepublic voID surfaceChanged(SurfaceHolder holder, int format, int wIDth, int height) { // Todo auto-generated method stub}@OverrIDepublic voID surfaceCreated(SurfaceHolder holder) { // Todo auto-generated method stub}@OverrIDepublic voID surfaceDestroyed(SurfaceHolder holder) { // Todo auto-generated method stub}private voID playVIDeo() { try{ mp = new MediaPlayer(); mp.setonErrorListener(this); mp.setonBufferingUpdateListener(this); mp.setonCompletionListener(this); mp.setonPreparedListener(this); mp.setAudioStreamType(2); mp.setdisplay(mPrevIEw.getHolder()); Runnable r = new Runnable(){ @OverrIDe public voID run() { try{ setDataSource(mPath); } catch(Exception ex){ Log.e(TAG, ex.getMessage()); } try { mp.prepare(); Log.v(TAG, "Duration: ===> " + mp.getDuration()); } catch (IllegalStateException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); Log.e(TAG, e.getMessage()); } mp.start(); } }; new Thread(r).start(); } catch(Exception ex){ String sDummy = ex.toString(); if(mp != null){ mp.stop(); mp.release(); } }}private voID setDataSource(String path) throws IOException { if(!URLUtil.isNetworkUrl(mPath)){ mp.setDataSource(mPath); } else{ URL url = new URL(mPath); URLConnection cn = url.openConnection(); cn.connect(); inputStream stream = cn.getinputStream(); if(stream == null){ throw new RuntimeException("stream is null"); } file fileTemp = file.createTempfile("mediaplayerTmp", "dat"); String tempPath = fileTemp.getabsolutePath(); fileOutputStream out = new fileOutputStream(fileTemp); byte buf[] = new byte[128]; do{ int numRead = stream.read(buf); if(numRead <= 0){ break; } out.write(buf, 0, numRead); }while(true); mp.setDataSource(tempPath); try{ stream.close(); } catch(Exception ex){ String sDummy = ex.toString(); } }}private voID getPathFromParentDialog(){ Intent intent = getIntent(); mPath = intent.getExtras().getString(Medialibrary.filename);}}
代码成功执行(没有任何例外),直到mp.start();,但屏幕为空(屏幕上只有按钮).
有谁知道上面的代码中可能有什么问题,或者是否有任何可用于网络的例子?
我将衷心感谢您的帮助
解决方法:
关闭袖口,我认为你的一些回调方法需要更多的逻辑,比如surfaceCreated().
总结以上是内存溢出为你收集整理的Android:如何创建视频播放器?全部内容,希望文章能够帮你解决Android:如何创建视频播放器?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)