How to play a video from Byte array?

2019-03-16 01:27发布

I am working on an app which needs encryption of video files which is working quite well.But the method I am using to decrypt returns the video as in Byte array. So is there anyway that I can play the video using that array without creating a new file.

My method decryption:

  private static byte[] decrypt(byte[] raw, byte[] encrypted) throws EncrypterException {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
try {
    final Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);

    return cipher.doFinal(encrypted);

} catch (Exception e) {
    throw new EncrypterException(e);
}
}

Please help me I am stuck here ?

3条回答
Viruses.
2楼-- · 2019-03-16 01:57

After lots of search, this answer could be a sum up. All credits should go the solution owners.

Since VideoView only accept URL or File, we have two solution;

Solution 1; Create temp File with stream and provide that file to videoView.

Ref; https://stackoverflow.com/a/21549067/1847645

Solution 2; Create media server on Android Local System and provide stream to media server where videoView is directed to the localhost for streaming.

Ref; https://stackoverflow.com/a/9096241/1847645

Other Ref; Which is very useful, thanks to libeasy; https://stackoverflow.com/a/15668803/1847645

查看更多
Lonely孤独者°
3楼-- · 2019-03-16 02:01

You should not feed VideoView with a static file but with a stream. How to produce this stream? Refer to Android Supported Media Formats. The simplest may be a local http server. LocalSingleHttpServer is an example of a library component implementing this kind of solution.

查看更多
神经病院院长
4楼-- · 2019-03-16 02:04

How do you usually play video? You send InputStrem instance to player. So you ByteArrayInputStream that wraps your byte array and send it to the player.

查看更多
登录 后发表回答