我打从SD卡一个加密的mp4视频文件。为此我做什么.....
第1步我复制文件从SD卡内部存储器和我写在世界可读和可写的世界格式文件,这样,我可以播放
第2步。我解密了temprary文件夹中的文件
第3步。现在我玩这个,文件播放没有问题
但问题是这个过程需要时间,因为文件很大大约25 MB,而这个过程(复制和解密)大约需要2分钟时间
在一些阅读,我发现这个问题的解决办法,我可以通过使用时间缩短 libmedia库,但我面临着一些问题 当我玩这个使用libmedia库 错误出现在日志中的猫
6月10日至10号:33:42.908:E / MediaPlayer的(9249):错误(1,-2147483648)
在活动屏幕我得到了在对话框的消息 无法播放视频
libmedia库链接 http://libeasy.alwaysdata.net/
感谢您的宝贵建议和帮助。
我的活动code是
包com.example.playvideo2;
进口java.io.IOException异常;
进口java.security.InvalidKeyException;
进口java.security.NoSuchAlgorithmException;
进口javax.crypto.Cipher中;
进口javax.crypto.KeyGenerator;
进口javax.crypto.NoSuchPaddingException;
进口javax.crypto.SecretKey;
进口fr.maxcom.http.LocalSingleHttpServer;
进口fr.maxcom.libmedia.Licensing;
进口android.media.MediaPlayer;
进口android.media.MediaPlayer.OnCompletionListener;
进口android.os.Bundle;
进口android.os.Environment;
进口android.app.Activity;
进口android.view.Menu;
进口android.widget.MediaController;
进口android.widget.VideoView;
公共类MainActivity扩展活动实现OnCompletionListener
{
LocalSingleHttpServer mServer上;
VideoView mVideoView;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.video_view);
Licensing.allow(本);
playENCVideo(Environment.getExternalStorageDirectory()
+/encVideo.mp4);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
公共无效playENCVideo(字符串路径){
尝试 {
密码破译= NULL;
的KeyGenerator kgen = KeyGenerator.getInstance(AES);
SecretKey的SKEY = kgen.generateKey();
解密= Cipher.getInstance(AES);
decipher.init(Cipher.DECRYPT_MODE,SKEY);
mServer上=新LocalSingleHttpServer();
mServer.setCipher(解码);
mServer.start();
PATH = mServer.getURL(路径);
mVideoView =(VideoView)findViewById(R.id.videoView1);
mVideoView.setVideoPath(路径);
// mVideoView.setMediaController(新的MediaController(本));
// mVideoView.requestFocus();
mVideoView.start();
}赶上(InvalidKeyException将E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(抛出:NoSuchAlgorithmException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(NoSuchPaddingException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
@覆盖
公共无效onCompletion(MediaPlayer的MP){
// MediaPlayer.OnCompletionListener接口
mServer.stop();
}
}
我的清单文件
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.playvideo2
安卓版code =1
机器人:VERSIONNAME =1.0
>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =18/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=com.example.playvideo2.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
&所述;元数据
机器人:名称=fr.maxcom.libmedia.apiKey
android:value="qV1T3Zf+IyCpb1jomsomS3NUXBk7LdDaV31DcwmVIoMpvWWK8xAQIl1RUVWQegSt2jJbwH+cleik7YCH2/chP1JWdhpSfgMaNRl3fDMOez8mGn98Jhp7KyAffjc5RA==" />
< /用途>
< /舱单>
这是我用于加密和解密
这code 的FileInputStream FIS =新的FileInputStream(新文件(D:/QLurnVideo/inputVideo.mp4));
文件OUTFILE =新的文件(D:/QLurnVideo/encVideo.mp4);
INT读取;
如果(!outfile.exists())
outfile.createNewFile();
文件decfile =新的文件(D:/QLurnVideo/decVideo.mp4);
如果(!decfile.exists())
decfile.createNewFile();
FileOutputStream中FOS =新的FileOutputStream(OUTFILE);
的FileInputStream encfis =新的FileInputStream(OUTFILE);
FileOutputStream中decfos =新的FileOutputStream(decfile);
密码加密器= Cipher.getInstance(AES);
密码译码= Cipher.getInstance(AES);
的KeyGenerator kgen = KeyGenerator.getInstance(AES);
SecretKey的SKEY = kgen.generateKey();
// LGO
encipher.init(Cipher.ENCRYPT_MODE,SKEY);
CipherInputStream顺=新CipherInputStream(FIS,加密器);
decipher.init(Cipher.DECRYPT_MODE,SKEY);
CipherOutputStream COS =新CipherOutputStream(decfos,破译);
而((读= cis.read())!= - 1)
{
fos.write((char)的读取);
fos.flush();
}
fos.close();
而((读= encfis.read())!= - 1)
{
cos.write(读);
cos.flush();
}
cos.close();
解决方案
您是通过加密文字图象,这要花费很多时间去更好地与逐行通过使用缓冲区这样的..
字节[] BUFF =新的字节[1024];
而((读= cis.read(BUFF))!= - 1)
{
fos.write(BUFF,0,读);
fos.flush();
}
fos.close();
而((读= encfis.read(BUFF))!= - 1)
{
cos.write(BUFF,0,读);
cos.flush();
}
cos.close();
I am playing a encrypted mp4 video file from sdcard. for this what i do .....
STEP 1. i copy file from sdcard in internal memory and i write that file in world readable and world writeable format so that that i could Play
STEP 2. I decrypt that file in a temprary folder
STEP 3. now i play this , file plays no problem
Problem is this process takes time , because file is large about 25 mb , and this process ( copying and decryption ) takes about 2 minutes time
after some reading i found a solution of this problem i could reduce time by using libmedia library , but i am facing some problem when i play this by using libmedia library error comes in log cat
10-10 06:33:42.908: E/MediaPlayer(9249): error (1, -2147483648)
on Activity screen i got a message in dialog "can't play video"
libmedia library link is http://libeasy.alwaysdata.net/
thanks for your valuable suggestion and help
my activity code is
package com.example.playvideo2;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import fr.maxcom.http.LocalSingleHttpServer;
import fr.maxcom.libmedia.Licensing;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity implements OnCompletionListener
{
LocalSingleHttpServer mServer;
VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_view);
Licensing.allow(this);
playENCVideo(Environment.getExternalStorageDirectory()
+ "/encVideo.mp4");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void playENCVideo(String path) {
try {
Cipher decipher = null;
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecretKey skey = kgen.generateKey();
decipher = Cipher.getInstance("AES");
decipher.init(Cipher.DECRYPT_MODE, skey);
mServer = new LocalSingleHttpServer();
mServer.setCipher(decipher);
mServer.start();
path = mServer.getURL(path);
mVideoView = (VideoView) findViewById(R.id.videoView1);
mVideoView.setVideoPath(path);
// mVideoView.setMediaController(new MediaController(this));
// mVideoView.requestFocus();
mVideoView.start();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onCompletion(MediaPlayer mp) {
// MediaPlayer.OnCompletionListener interface
mServer.stop();
}
}
my manifest file is
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playvideo2"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.playvideo2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="fr.maxcom.libmedia.apiKey"
android:value="qV1T3Zf+IyCpb1jomsomS3NUXBk7LdDaV31DcwmVIoMpvWWK8xAQIl1RUVWQegSt2jJbwH+cleik7YCH2/chP1JWdhpSfgMaNRl3fDMOez8mGn98Jhp7KyAffjc5RA==" />
</application>
</manifest>
This is that code which i used for encryption and decryption
FileInputStream fis = new FileInputStream(new File("D:/QLurnVideo/inputVideo.mp4"));
File outfile = new File("D:/QLurnVideo/encVideo.mp4");
int read;
if(!outfile.exists())
outfile.createNewFile();
File decfile = new File("D:/QLurnVideo/decVideo.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();
//Lgo
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();
解决方案
You are encrypting video by character, this take much time better go with line by line by using buffer like this..
byte[] buff=new byte[1024];
while((read = cis.read(buff))!=-1)
{
fos.write(buff,0,read);
fos.flush();
}
fos.close();
while((read=encfis.read(buff))!=-1)
{
cos.write(buff,0,read);
cos.flush();
}
cos.close();