我正在构建一个应用程序表单,用户可以下载视频,但无法使用内置视频播放器播放。 只有在app用户中才能播放。
我尝试了以下内容:
videoview
,以播放视频。但它不起作用。 我的所有视频都是.mp4格式。
以下是加密和解密视频的代码(取自另一个堆叠答案,但不再有链接)
try{
FileInputStream fis = new FileInputStream(new File(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME))));
File outfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/appvideo/1.mp4");
int read;
if(!outfile.exists())
outfile.createNewFile();
File decfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/appvideo/2.mp4");
if(!decfile.exists())
decfile.createNewFile();
FileOutputStream fos = new FileOutputStream(outfile);
FileInputStream encfis = new FileInputStream(outfile);
FileOutputStream decfos = new FileOutputStream(decfile);
Cipher encipher = Cipher.getInstance("AES");
Cipher decipher = Cipher.getInstance("AES");
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecretKey skey = kgen.generateKey();
encipher.init(Cipher.ENCRYPT_MODE, skey);
CipherInputStream cis = new CipherInputStream(fis, encipher);
decipher.init(Cipher.DECRYPT_MODE, skey);
CipherOutputStream cos = new CipherOutputStream(decfos,decipher);
while((read = cis.read())!=-1)
{
fos.write((char)read);
fos.flush();
}
fos.close();
while((read=encfis.read())!=-1)
{
cos.write(read);
cos.flush();
}
cos.close();
}catch (Exception e) {
// TODO: handle exceptione
e.printStackTrace();
}
我的视频播放代码是
try {
//set the media controller in the VideoView
videoView.setMediaController(mediaControls);
mediaControls.setAnchorView(videoView);
//set the uri of the video to be played
videoView.setVideoURI(Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/appvVideo/3.mp4")));
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
谁能告诉我我做错了什么。
什么是好方法
下载并加密视频,然后解密只能用app播放的表单应用程序?