您的位置:首页 > 其它

MediaPlayer+SurfaceView 视频截图

2012-07-25 17:16 309 查看
其中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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: