我已经从服务器下载文件并使用jetpack安全文件加密保存,加密和解密对于图像文件工作正常,但对于音频文件,我能够通过解密获得字节数组,但我无法使用exoplayer播放它。
加密类
public class MyEncryption {
Context context;
MasterKey mainKey;
public MyEncryption(Context context) {
this.context = context;
try {
mainKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
public void encryptFile(final Context context, final File target) throws GeneralSecurityException, IOException {
int size = (int) target.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(target));
buf.read(bytes, 0, bytes.length);
buf.close();
final EncryptedFile file = new EncryptedFile.Builder(context,
new File(target.getAbsolutePath()), mainKey, EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB)
.build();
target.delete();
final OutputStream stream = file.openFileOutput();
stream.write(bytes);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public byte[] decryptFile(File file) throws GeneralSecurityException, IOException {
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
file,
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
InputStream inputStream = encryptedFile.openFileInput();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int nextByte = inputStream.read();
while (nextByte != -1) {
byteArrayOutputStream.write(nextByte);
nextByte = inputStream.read();
}
byte[] plaintext = byteArrayOutputStream.toByteArray();
return plaintext;
}
public Bitmap decryptImage(File file) throws GeneralSecurityException, IOException {
EncryptedFile encryptedFile = new EncryptedFile.Builder(context,
file,
mainKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build();
InputStream inputStream = encryptedFile.openFileInput();
return BitmapFactory.decodeStream(inputStream);
}
}
我尝试通过以下方式播放音频:
private void prepareExoPlayerFromByteArray(byte[] data) {
TrackSelector trackSelector = new DefaultTrackSelector(getContext());
LoadControl loadControl = new DefaultLoadControl();
exoPlayer = new SimpleExoPlayer.Builder(getContext()).setTrackSelector(trackSelector).setLoadControl(loadControl).build();
exoPlayer.addListener(eventListener);
final MByteArrayDataSource byteArrayDataSource = new MByteArrayDataSource(data);
Log.i(TAG, "ByteArrayDataSource constructed.");
Log.d(TAG, "prepareExoPlayerFromByteArray: " + byteArrayDataSource.getUri());
DataSource.Factory factory = () -> byteArrayDataSource;
Log.i(TAG, "DataSource.Factory constructed.");
MediaSource audioSource = new ProgressiveMediaSource.Factory(factory).createMediaSource(byteArrayDataSource.getUri());
Log.i(TAG, "Audio source constructed.");
exoPlayer.prepare(audioSource);
}
此代码不播放音频,它说
2021-03-31 08:35:16.881 4261-4779/com.xxx.xxx E/LoadTask: Unexpected exception loading stream
java.lang.NullPointerException
at com.google.android.exoplayer2.util.Assertions.checkNotNull(Assertions.java:147)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:84)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:961)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
2021-03-31 08:35:16.890 4261-4778/com.bipin.koreanvasa E/ExoPlayerImplInternal: Source error.
com.google.android.exoplayer2.upstream.Loader$UnexpectedLoaderException: Unexpected NullPointerException: null
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:413)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.lang.NullPointerException
at com.google.android.exoplayer2.util.Assertions.checkNotNull(Assertions.java:147)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:84)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:961)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
他们有什么办法解决这个问题吗,我参考了Link