yixia / VitamioBundle

Vitamio for Android
http://www.vitamio.org/en/
Other
5.27k stars 2.07k forks source link

MediaMetaDataRetriever not working in Parallel AsyncTask #56

Open mafshin opened 10 years ago

mafshin commented 10 years ago

The MediaMetaDataRetriever class doesn't work as expected. It stuck on setDataSource and never returns the frame (Its in an AsyncTask so it does not block)

MediaMetadataRetriever metaData = new MediaMetadataRetriever(); metaData.setDataSource(path); Bitmap frame = metaData.getFrameAtTime(position * 1000);

Tested on GS4.

[See my last post]

crossle commented 10 years ago

metaData.getFrameAtTime(position) Vitamio demo have sample.

mafshin commented 10 years ago

The undocumented fix was this just after getFrameAt

metaData.release();

Finally, using metaData.release(); I got it working, but yet there's another problem and it's the Parallel Execution. The Android SDK MetaDataRetriever works very well when called in a parallel AsyncTask

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            frameGeneratorAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args);
        else
            frameGeneratorAsyncTask.execute(args);

but, the Vitamio MetaDataRetriever only works in sequential mode.

FrameGeneratorAsyncTask:

    protected Bitmap doInBackground(Object... objects) {
        Context context = (Context) objects[0];
        String path = objects[1].toString();
        long position = (Long) objects[2];
        Bitmap frame = null;
        MediaMetadataRetriever metaData = null;
        try {
            metaData = new MediaMetadataRetriever(context);
            metaData.setDataSource(path);
            frame = metaData.getFrameAtTime(position);
        } catch (Exception e) {
            Log.e(TAG, "FrameGeneratorAsyncTask Error");
        }finally {
            if(metaData != null)
                metaData.release();
        }
        return frame;
    }

In my case, I had to wait till all previous frames (for previous items) get generated (sequential) while using Android SDK MetaDataRetriever, the frames are generated in parallel. In other words, the app can't benefit from modern smartphones multi-core power.

lucalooz commented 10 years ago

I need this too, is useful to generate thumbnail with multithread