使用Facebook Conceal库将加密的视频文件流式传输到VideoView

时间:2015-01-23 17:11:50

标签: android encryption android-videoview bufferedinputstream facebook-conceal

我有一个大约130M的加密mp4文件,我想在VideoView中播放。流式传输的最佳方式是什么?我不想复制到未加密的文件然后使用,因为在较慢的设备上解密它所花费的时间是禁止的。到目前为止我所拥有的是复制到文件并通过在解密完成之前为其提供缓冲文件来启动VideoView。

public class DecryptVideoTask extends AsyncTask<Void, File, File> {
    public static final String TAG = DecryptVideoTask.class.getCanonicalName();
    public static final int READ_AHEAD_BYTES = 65469100;

    private Context context;
    private VideoView videoView;
    private String encryptedFilePath;
    private ProgressDialog progressDialog;

    /**
     *
     * @param context currentContext
     * @param videoView VideoView that will play the video
     * @param encryptedFilePath conceal encrypted video
     * @param progressDialog progressDialog to hide when the video starts
     */
    public DecryptVideoTask(Context context, VideoView videoView, String encryptedFilePath, ProgressDialog progressDialog) {
        this.context = context;
        this.videoView = videoView;
        this.encryptedFilePath = encryptedFilePath;
        this.progressDialog = progressDialog;
    }

    @Override
    protected File doInBackground(Void... params) {
        File bufferFile = null;

        try {

            bufferFile = File.createTempFile("temp", ".mp4", context.getCacheDir());

            BufferedOutputStream bufferOS = new BufferedOutputStream(
                    new FileOutputStream(bufferFile));

            // getting CipherInputStream.  encryptedFilePath is the path to a conceal encrypted file
            InputStream is = Application.getInstance().getCryptoManager().decrypt(new File(encryptedFilePath));
            BufferedInputStream bis = new BufferedInputStream(is, 524288);

            byte[] buffer = new byte[32768];
            int numRead;
            long totalRead = 0;
            boolean started = false;

            long startTime = System.currentTimeMillis();
            while ((numRead = bis.read(buffer)) != -1) {

                bufferOS.write(buffer, 0, numRead);
                bufferOS.flush();
                totalRead += numRead;
                Log.d(TAG, "total read: " + totalRead);
                if (totalRead > READ_AHEAD_BYTES && !started) {
                    Log.d(TAG, "decryption took: " + (System.currentTimeMillis() - startTime) + "ms");
                    Log.d(TAG, "starting video: " + encryptedFilePath);
                    publishProgress(bufferFile);
                    started = true;
                }

            }

            bufferOS.close();
            bis.close();

            if (!started) {
                Log.d(TAG, "file is decrypted but video has not been started, starting now: " + encryptedFilePath);
                publishProgress(bufferFile);
                started = true;
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
            return bufferFile;
        }

        return bufferFile;
    }

    @Override
    protected void onProgressUpdate(File... values) {
        videoView.setVideoURI(Uri.parse("file://" + values[0].getAbsolutePath()));
        videoView.requestFocus();
        videoView.start();

        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}

视频开始但是根据我操作的缓冲区大小(READ_AHEAD_BYTES),视频可能会在几秒钟后出错

01-23 09:54:13.219 12087-12099/com.github.browep.privatephotovault E/MediaPlayer﹕ error (1, -1004) 01-23 09:54:13.219 12087-12087/com.github.browep.privatephotovault E/MediaPlayer﹕ Error (1,-1004) 还有一个弹出窗口显示&#34;无法播放此视频&#34;。有一个更好的方法吗?的FileDescriptor?什么?

0 个答案:

没有答案