android捕获视频帧

前端之家收集整理的这篇文章主要介绍了android捕获视频帧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要获得一个视频文件的框架(它可能在SD卡,缓存目录或应用程序目录).我在我的应用程序中有 android.media包,里面有MediaMetadataRetriever类.要将第一帧放入位图,我使用代码
  1. public static Bitmap getVideoFrame(Context context,Uri videoUri) {
  2. MediaMetadataRetriever retriever = new MediaMetadataRetriever();
  3. try {
  4. retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
  5. retriever.setDataSource(context,videoUri);
  6. return retriever.captureFrame();
  7. } catch (IllegalArgumentException ex) {
  8. throw new RuntimeException();
  9. } catch (RuntimeException ex) {
  10. throw new RuntimeException();
  11. } finally {
  12. retriever.release();
  13. }
  14. }

但这不起作用.当我设置数据源时,它抛出异常(java.lang.RuntimeException:setDataSource Failed:status = 0x80000000).你知道怎么让这段代码工作吗?或者你有没有使用ffmpeg或其他外部库的类似(简单)解决方案? videoUri是一个有效的uri(媒体播放器可以播放来自该URI的视频)

解决方法

以下为我工作:
  1. public static Bitmap getVideoFrame(FileDescriptor FD) {
  2. MediaMetadataRetriever retriever = new MediaMetadataRetriever();
  3. try {
  4. retriever.setDataSource(FD);
  5. return retriever.getFrameAtTime();
  6. } catch (IllegalArgumentException ex) {
  7. ex.printStackTrace();
  8. } catch (RuntimeException ex) {
  9. ex.printStackTrace();
  10. } finally {
  11. try {
  12. retriever.release();
  13. } catch (RuntimeException ex) {
  14. }
  15. }
  16. return null;
  17. }

如果您使用路径而不是filedescriptor也可以.

猜你在找的Android相关文章