直接从CipherInputStream解密和读取mp3文件,而无需存储



我正在我的安卓应用程序中下载和加密mp3文件。但是我正在尝试解密和读取文件,而无需将文件写回文件系统。 当我尝试使用CipherInputStream将mp3流式传输到android媒体播放器API时,它无法识别该文件。但是,当我尝试转换为字节数组并将其流式传输为 ByteArrayInputStream 时,它可以工作,但我不想这样做,因为对于较大的文件和将数据存储在 JVM 中可能需要很长时间。

这是我的代码

FileInputStream fis = new FileInputStream(file);
CipherInputStream cis = new CipherInputStream(fis, cipher);
mbuffer = new ByteArrayInputStream(getByte(cis));
Response streamResponse = new Response(Status.OK, mimeType, mbuffer);

上面的代码工作正常,但问题在于此

new ByteArrayInputStream(getByte(cis));

getByte方法中,我将CipherInputStream转换为字节,然后将其转换回InputStream

我正在尝试将顺式直接流式传输到

Response streamResponse = new Response(Status.OK, mimeType, cis);

但不适用于媒体播放器

         File tempMp3 = File.createTempFile("kurchina", "mp3", getCacheDir());
         tempMp3.deleteOnExit();
         FileOutputStream fos = new FileOutputStream(tempMp3);
         fos.write(mp3SoundByteArray);
         fos.close();

         FileInputStream fis = new FileInputStream(tempMp3);
         mp.setDataSource(fis.getFD());

         mp.prepare();
         mp.start();

相关内容

最新更新