I wonder what the best way is to play a local aes128-cbc encrypted mp3 file with ExoPlayer. There is a class Aes128DataSource which seems to exist for that purpose, but I can't get ExoPlayer to play the file. It always returns -1 as the track duration which indicates that the file somehow is corrupt. There is not much information about what the error is since there is nothing in the logs. The mp3 just does not get played. I guess the file is not decrypted correctly. Can somebody give an example of how to do it? Here is what I do:
Key and iv:
private byte[] iv = hexToBytes("..."); //32 hex symbols here as String
private byte[] key = hexToBytes("..."); //32 hex symbols here as String
hexToBytes function which converts the given key and iv from hex String to byte[]:
private byte[] hexToBytes(String str) {
if (str == null) {
return null;
}
else if (str.length() < 2) {
return null;
}
else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i = 0; i < len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
}
return buffer;
}
}
And here what I do to play the encrypted file:
Uri uri = Uri.parse("android.resource://" + context.getPackageName() + File.separator + R.raw.track01_cbc);
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
String userAgent = getUserAgent(context, ...);
DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent);
Aes128DataSource aes128DataSource = new Aes128DataSource(dataSource, key, iv);
SampleSource sampleSource = new ExtractorSampleSource(uri, aes128DataSource, new Mp3Extractor(), RENDERER_COUNT, 5000);
MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
exoPlayer.prepare(audioRenderer);
exoPlayer.setPlayWhenReady(true);