MediaPlayer+SurfaceView 视频截图

来源:互联网 发布:无线端淘宝收藏店铺 编辑:程序博客网 时间:2024/06/05 16:26

其中MediaMetadataRetriever类在2.3api之后 才提供,大致就是这样的,系统点的代码 以后再补- -

private void savaScreenShot()
    {

        mMediaPlayer.pause();

        Bitmap bitmap = null;

        // 2.3api下可用
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try
        {
            retriever.setDataSource("/mnt/sdcard/oppo.mp4");//资源路径
            String timeString =
                    retriever
                            .extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); //这一句是必须的

            long time = Long.parseLong(timeString) * 1000; //获取总长度,这一句也是必须的

          //mSeekBarProgress是一个显示当前播放进度的进度条,因为之前通过mSeekBarProgress.setMax(mMediaPlayer.getDuration());设置过了,所以

           //mSeekBarProgress.getMax()获取的时这个视频的总时间,而mSeekBarProgress.getProgress()是播放的当前进度

            long currentPostion =
                    time * mSeekBarProgress.getProgress()
                            / mSeekBarProgress.getMax();     //通过这个计算出想截取的画面所在的时间
            bitmap = retriever.getFrameAtTime(currentPostion);// 按当前播放位置选择帧
        }
        catch (IllegalArgumentException ex)
        {

        }
        catch (RuntimeException ex)
        {
.
        }
        finally
        {
            try
            {
                retriever.release();
            }
            catch (RuntimeException ex)
            {

            }

//截图保存路径

       String mScreenshotPath =
                Environment.getExternalStorageDirectory() + "/droidnova";
        String path = mScreenshotPath + "/" + mMyTime.getDate() + ".jpg";
        
        File file = new File(path);
        
        FileOutputStream fos;
        try
        {
            Toast.makeText(VideoPlayerActivity.this, "截图成功,保存为" + path, Toast.LENGTH_SHORT).show();
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        }
        catch (FileNotFoundException e)
        {
            Log.e("Panel", "FileNotFoundException", e);
        }
        catch (IOException e)
        {
            Log.e("Panel", "IOEception", e);
        }
        mMediaPlayer.start();
    }
原创粉丝点击