So I am a beginner to ExoPlayer, and I'm using ExoPlayer v2.11.3 to play encrypted videos. I've checked this question here, but it did not help me to do it since it is an old post since 2017!
What I think, that I need to create a custom DataSource from "DefaultHttpDataSource
".
I'm using this "Encrypt()
" method to do the AES encryption:
private static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
if (hasFile()) {
Log.d("Encrypt", "encrypted file found, no need to recreate");
return;
}
// Video Reading.
FileInputStream fis = new FileInputStream(toBeEncryptedFile);
// This stream write the encrypted video. This stream will be wrapped by another stream.
FileOutputStream fos = new FileOutputStream(encryptedFile);
// Create cipher
cipher = Cipher.getInstance(AES_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
// Write bytes
byte[] buffer = new byte[1024 * 1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
}