使用javacv ffmpeg在android中播放视频

时间:2017-02-20 16:18:43

标签: android opencv video ffmpeg javacv

我正在尝试使用javacv播放存储在SD卡中的视频。以下是我的代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playthevideo();
    }

    protected void playthevideo() {

       String imageInSD = "/storage/080A-0063/dama/" + "test3.mp4";

       FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(imageInSD);

       AndroidFrameConverter converterToBitmap = new AndroidFrameConverter();

       OpenCVFrameConverter.ToIplImage converterToipi = new OpenCVFrameConverter.ToIplImage();

           try {
               Log.d("Tag", "try");
               grabber.start();
               Log.d("Tag", "started");

               int i = 0;
               IplImage grabbedImage = null;

               ImageView mimg = (ImageView) findViewById(R.id.a);

               grabber.setFrameRate(grabber.getFrameRate());
               ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
               while (((grabbedImage = converterToipi.convert(grabber.grabImage())) != null)) {

                   Log.d("Tag", String.valueOf(i));

                   int width = grabbedImage.width();

                   int height = grabbedImage.height();

                   if (grabbedImage == null) {
                       Log.d("Tag", "error");
                   }

                   IplImage container = IplImage.create(width, height, IPL_DEPTH_8U, 4);

                   cvCvtColor(grabbedImage, container, CV_BGR2RGBA);

                   Bitmap bitmapnew = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

                   bitmapnew.copyPixelsFromBuffer(container.getByteBuffer());

                   if (bitmapnew == null) {
                       Log.d("Tag", "bit error");
                   }

                   mimg.setImageBitmap(bitmapnew);

                   mimg.requestFocus();

                   i++;
               }

               Log.d("Tag", "go");

           }
           catch(Exception e) {

          }
     }
}

只是忽略标签,因为这些仅用于我的测试目的.. 当我运行此代码时,主活动布局仍在加载,而android监视器显示“i”的值(这是当前帧编号),并且在帧编号3671代码退出while循环后突然显示imageview显示框架这不是该视频的结束帧(它在视频周围的某处)。 我无法找到一种方法来显示来自ffmpegframegrabber的抓取帧,所以我决定以这种方式在imageview中显示图像。任何人都可以告诉我为什么我收到此错误或其他错误非路径播放并在Android活动中显示视频? BTW javacv 1.3.1正确导入我的Android开发环境。谢谢。

0 个答案:

没有答案