我正在从 sdcard 播放加密的 mp4 视频文件。为此我做什么......
第1步。我从内存中的 sdcard 复制文件,并以世界可读和世界可写格式写入该文件,以便我可以玩
第 2步。我在临时文件夹中解密该文件
STEP 3.现在我玩这个,文件播放没问题
问题是这个过程需要时间,因为文件很大大约25 mb ,这个过程(复制和解密)大约需要2分钟的时间。
经过一番阅读后,我发现了这个问题的解决方案,我可以通过使用 libmedia 库来减少时间,但是当我使用 libmedia 库播放这个错误时,我遇到了一些问题 log cat
10-10 06:33:42.908: E/MediaPlayer(9249): error (1, -2147483648)
在活动屏幕上,我在对话框“无法播放视频”中收到一条消息
libmedia 库链接是http://libeasy.alwaysdata.net/
感谢您的宝贵建议和帮助
我的活动代码是
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();
}
}
我的清单文件是
<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>
这是我用于加密和解密的代码
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();