Using mp4parser , how can I handle videos that are taken from Uri and ContentResolver?

后端 未结 2 882
再見小時候
再見小時候 2021-01-07 21:43

Background

We want to let the user choose a video from any app, and then trim a video to be of max of 5 seconds.

The problem

For getting a Uri to

2条回答
  •  终归单人心
    2021-01-07 22:03

    Next a snipped shows how to open a MediaStore Uri with IsoFile from Mp4Parser. So, you can see how to get a FileChannel from a Uri.

    public void test(@NonNull final Context context, @NonNull final Uri uri) throws IOException
    {
        ParcelFileDescriptor fileDescriptor = null;
    
        try
        {
            final ContentResolver resolver = context.getContentResolver();
            fileDescriptor = resolver.openFileDescriptor(uri, "rw");
    
            if (fileDescriptor == null)
            {
                throw new IOException("Failed to open Uri.");
            }
    
            final FileDescriptor  fd          = fileDescriptor.getFileDescriptor();
            final FileInputStream inputStream = new FileInputStream(fd);
            final FileChannel     fileChannel = inputStream.getChannel();
    
            final DataSource channel = new FileDataSourceImpl(fileChannel);
            final IsoFile    isoFile = new IsoFile(channel);
    
            ... do what you need ....
        }
        finally
        {
            if (fileDescriptor != null)
            {
                fileDescriptor.close();
            }
        }
    }
    

提交回复
热议问题