关闭

VideoView以及Surface+MediaPlayer播放视频RAW下视频方式

标签: SurfaceViewVideoView播放音频mediaplayer
520人阅读 评论(0) 收藏 举报
分类:

一:首先讲VideoView

1videoView的第一种播放方式: videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.ceshi));//ceshi是在RAW目录下的mp4视频

         videoView.start();

2 videoview的第二种播放方式:

  //输入流

   InputStream in = getApplicationContext().getResources().openRawResource(R.raw.ceshi);

   //输出流

    OutputStream out = new FileOutputStream(“要写入到sd卡的路径videoPath”);

   //将资源文件welcome_media.mp4写入到sd卡
    byte[] buffer = new byte[1024];
    int length;
     while ((length = in.read(buffer)) > 0) {
     out.write(buffer, 0, length);
    }
     out.flush();       //刷新
     out.close();        //关闭
      in.close();


File file = new File(videoPath);
if (!file.exists()){
   Log.i("MainActivity0", "视频文件不存在");
}else {
    videoView.setVideoPath(file.getPath());//这个API播放的是sd卡上的视频
 
 //循环播放
 videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
       @Override
       public void onPrepared(MediaPlayer mp) {
           mp.setLooping(true);
       }
   });
     videoView.start();
    }

二:SurfaceView+MediaPlayer播放视频

sv = (SurfaceView) findViewById(R.id.sv);

//Surface视频播放
        holder = sv.getHolder();// 得到SurfaceView的容器,界面内容是显示在容器里面的。
        // 过时的api,必须写,如果4.0以上的系统,不写完全没问题, 4.0一下的系统必须要写
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        // surfaceView 被创建是需要花费一定的时间的。
        // 在oncreate方法执行的时候 surfaceViewHolder还没有完全创建出来。
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                System.out.println("surfaceview 的holder被销毁了。");
                position = mediaplayer.getCurrentPosition();// 记录上次播放的位置,然后停止。
                mediaplayer.stop();
                mediaplayer.release();
                mediaplayer = null;

            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                System.out.println("surfaceview 的holder被创建好了");
                try {

                   //     InputStream in = getApplicationContext().getResources().openRawResource(R.raw.ceshi);//如果需要将视频保存到本地sd卡上,可以用这个方法来获取到流

                    AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(R.raw.ceshi);//获取raw下的视频文件  ;
                    mediaplayer = new MediaPlayer();
                    mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaplayer.setDataSource(/*"/sdcard/ceshi.mp4"*/afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());//参数里的注释是直接播放sd卡上的视频
                    mediaplayer.setDisplay(holder);
                    mediaplayer.prepareAsync();
                    mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            Intent intent = new Intent(mContext, GuideActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                    mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mediaplayer.start();
                            if (position > 0) {
                                mediaplayer.seekTo(position);
                            }
                        }
                    });
                } catch (Exception e) {
                    System.out.println("surfaceview--失败:");
                    Toast.makeText(mContext, "播放失败", 0).show();
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
                System.out.println("surface view的大小发生变化。");
            }
        });







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44453次
    • 积分:896
    • 等级:
    • 排名:千里之外
    • 原创:45篇
    • 转载:2篇
    • 译文:1篇
    • 评论:7条