可加解密的视频播放器打开五次自动关闭.docx
《可加解密的视频播放器打开五次自动关闭.docx》由会员分享,可在线阅读,更多相关《可加解密的视频播放器打开五次自动关闭.docx(14页珍藏版)》请在冰豆网上搜索。
可加解密的视频播放器打开五次自动关闭
package播放器;
importjava.awt.Component;
importjava.awt.Dimension;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjavax.media.ControllerEvent;
importjavax.media.ControllerListener;
importjavax.media.EndOfMediaEvent;
importjavax.media.Manager;
importjavax.media.MediaLocator;
importjavax.media.NoPlayerException;
importjavax.media.Player;
importjavax.media.PrefetchCompleteEvent;
importjavax.media.RealizeCompleteEvent;
importjavax.media.Time;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.UIManager;
publicclassMEDIAPlayerextendsJFrameimplementsControllerListener,ItemListener{
Playerplayer;
Filefile;
JLabeljl;//提示面板
JFileChooserjfc;//文件选择器
intresult;//是否选择文件的结果
InputStreamin;
OutputStreamon;
OutputStreamon2;//打开文件专用第二道流
Componentvisual;
Componentcontrol=null;
//设置窗口大小的参数
intvideoWidth=0;
intvideoHeight=0;
intcontrolHeight=30;
intinsetWidth=15;
intinsetHeight=40;
MEDIAPlayer(){
setTitle("可加密的播放器");
setLayout(null);
//加菜单栏
MenuBar();
setDefaultCloseOperation(3);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
super.windowClosing(e);
if(file!
=null){
Filenew_file=newFile(file.getParent()+"\\new.avi");
if(new_file.exists()){
//关闭播放器,否则无法删除被使用中的文件
if(player!
player.close();
}
//关闭可能占用文件的流
if(in!
try{
in.close();
}catch(IOExceptione1){
e1.printStackTrace();
if(on!
on.close();
new_file.delete();
});
jl=newJLabel();
jl.setBounds(200,120,100,30);
add(jl);
setSize(500,500);
setLocation(500,150);
setVisible(true);
//菜单栏
publicvoidMenuBar(){
JMenuBarJMB=newJMenuBar();
String[]arrayMenu={"文件","处理"};
String[][]arrayItem={
{"打开","退出"},
{"加密","解密"}};
for(inti=0;i{JMenuMenu=newJMenu(arrayMenu[i]);for(intj=0;j{JMenuItemitem=newJMenuItem(arrayItem[i][j]);item.addActionListener(newGActionListener());Menu.add(item);}JMB.add(Menu);}JMB.setBounds(0,0,500,20);this.getContentPane().add(JMB);}publicvoiditemStateChanged(ItemEvente){}publicvoidcontrollerUpdate(ControllerEventce){if(ceinstanceofRealizeCompleteEvent){//player实例化完成后进行player播放前预处理player.prefetch();}elseif(ceinstanceofPrefetchCompleteEvent){if(visual!=null)return;//取得player中的播放视频的组件,并得到视频窗口的大小//然后把视频窗口的组件添加到Frame窗口中,if((visual=player.getVisualComponent())!=null){Dimensionsize=visual.getPreferredSize();videoWidth=size.width;videoHeight=size.height;visual.setBounds(0,0,videoWidth,videoHeight);this.add(visual);}else{videoWidth=320;}//取得player中的视频播放控制条组件,并把该组件添加到Frame窗口中if((control=player.getControlPanelComponent())!=null){controlHeight=control.getPreferredSize().height;control.setBounds(0,videoHeight,videoWidth,controlHeight+insetHeight);this.add(control);}//设定Frame窗口的大小,使得满足视频文件的默认大小this.setSize(videoWidth+insetWidth,videoHeight+controlHeight+insetHeight);this.validate();//启动视频播放组件开始播放player.start();}elseif(ceinstanceofEndOfMediaEvent){//当播放视频完成后,把时间进度条恢复到开始,并再次重新开始播放player.setMediaTime(newTime(0));player.start();}}privateclassGActionListenerimplementsActionListener{@OverridepublicvoidactionPerformed(ActionEvente){newThread(newRunnable(){publicvoidrun(){if(e.getActionCommand().equals("退出")){System.exit(0);}elseif(e.getActionCommand().equals("打开")){jfc=newJFileChooser();jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);result=jfc.showDialog(newJLabel(),"选择");if(JFileChooser.APPROVE_OPTION==result){file=jfc.getSelectedFile();}else{file=null;}Filemyfile=newFile("C:/Users/a/Desktop/count.txt");BufferedReaderbr;try{br=newBufferedReader(newFileReader(myfile));Stringtemp=br.readLine();intcount=Integer.parseInt(temp);if(count>0){count-=1;Stringa=String.valueOf(count);FileWriterfw=newFileWriter("C:/Users/a/Desktop/count.txt");BufferedWriterbw=newBufferedWriter(fw);bw.write(a,0,a.length());bw.flush();bw.close();}else{System.out.println("超过使用次数!");System.exit(0);}}catch(FileNotFoundExceptione3){e3.printStackTrace();}catch(IOExceptione1){e1.printStackTrace();}if(file!=null){jl.setText("正在解密");//临时解密Filefile1=newFile(jfc.getCurrentDirectory()+"\\new.avi");Filefile2=newFile(jfc.getCurrentDirectory()+"\\new2.avi");try{in=newFileInputStream(file);on=newFileOutputStream(file1,true);on2=newFileOutputStream(file2,true);inttempbyte;inti=0;while((tempbyte=in.read())!=-1){if(i==0){if(tempbyte==5){jl.setText("无法打开");on.close();on2.close();file1.delete();file2.delete();return;}on2.write(~tempbyte);i++;continue;}elseif(i<3){on.write(~tempbyte);on2.write(tempbyte);i++;continue;}else{on.write(tempbyte);on2.write(tempbyte);i++;continue;}}in.close();on.close();on2.close();file.delete();file2.renameTo(file);MEDIAPlayer.this.remove(jl);}catch(IOExceptionee){ee.printStackTrace();return;}//播放视频try{player=Manager.createPlayer(newMediaLocator("file:"+file1.getPath()));}catch(NoPlayerExceptione1){e1.printStackTrace();}catch(IOExceptione1){e1.printStackTrace();}player.addControllerListener(MEDIAPlayer.this);player.realize();}}elseif(e.getActionCommand().equals("加密")){jfc=newJFileChooser();jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);result=jfc.showDialog(newJLabel(),"选择");if(JFileChooser.APPROVE_OPTION==result){file=jfc.getSelectedFile();}else{file=null;}if(file!=null){jl.setText("正在加密");Filenew_file=newFile(jfc.getCurrentDirectory()+"\\new.avi");try{in=newFileInputStream(file);on=newFileOutputStream(new_file,true);inttempbyte;inti=0;on.write(0);while((tempbyte=in.read())!=-1){if(i<2){on.write(~tempbyte);i++;continue;}else{on.write(tempbyte);i++;continue;}}in.close();on.close();file.delete();new_file.renameTo(file);jl.setText("加密完成");}catch(IOExceptionee){ee.printStackTrace();return;}}}elseif(e.getActionCommand().equals("解密")){jfc=newJFileChooser();jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);result=jfc.showDialog(newJLabel(),"选择");if(JFileChooser.APPROVE_OPTION==result){file=jfc.getSelectedFile();}else{file=null;}if(file!=null){jl.setText("正在解密");Filenew_file=newFile(jfc.getCurrentDirectory()+"\\new.avi");try{in=newFileInputStream(file);on=newFileOutputStream(new_file,true);inttempbyte;inti=0;while((tempbyte=in.read())!=-1){if(i==0){i++;continue;}elseif(i<3){on.write(~tempbyte);i++;continue;}else{on.write(tempbyte);i++;continue;}}in.close();on.close();file.delete();new_file.renameTo(file);jl.setText("解密完成");}catch(IOExceptionee){ee.printStackTrace();return;}}}}}).start();}}publicstaticvoidmain(String[]args)throwsException{Filefile=newFile("C:\\Users\\a\\Desktop","count.txt");if(!file.exists()){file.createNewFile();Strings="5";FileWriterfw=newFileWriter("C:/Users/a/Desktop/count.txt");BufferedWriterbw=newBufferedWriter(fw);bw.write(s,0,s.length());bw.flush();bw.close();}tr
{
JMenuMenu=newJMenu(arrayMenu[i]);
for(intj=0;j{JMenuItemitem=newJMenuItem(arrayItem[i][j]);item.addActionListener(newGActionListener());Menu.add(item);}JMB.add(Menu);}JMB.setBounds(0,0,500,20);this.getContentPane().add(JMB);}publicvoiditemStateChanged(ItemEvente){}publicvoidcontrollerUpdate(ControllerEventce){if(ceinstanceofRealizeCompleteEvent){//player实例化完成后进行player播放前预处理player.prefetch();}elseif(ceinstanceofPrefetchCompleteEvent){if(visual!=null)return;//取得player中的播放视频的组件,并得到视频窗口的大小//然后把视频窗口的组件添加到Frame窗口中,if((visual=player.getVisualComponent())!=null){Dimensionsize=visual.getPreferredSize();videoWidth=size.width;videoHeight=size.height;visual.setBounds(0,0,videoWidth,videoHeight);this.add(visual);}else{videoWidth=320;}//取得player中的视频播放控制条组件,并把该组件添加到Frame窗口中if((control=player.getControlPanelComponent())!=null){controlHeight=control.getPreferredSize().height;control.setBounds(0,videoHeight,videoWidth,controlHeight+insetHeight);this.add(control);}//设定Frame窗口的大小,使得满足视频文件的默认大小this.setSize(videoWidth+insetWidth,videoHeight+controlHeight+insetHeight);this.validate();//启动视频播放组件开始播放player.start();}elseif(ceinstanceofEndOfMediaEvent){//当播放视频完成后,把时间进度条恢复到开始,并再次重新开始播放player.setMediaTime(newTime(0));player.start();}}privateclassGActionListenerimplementsActionListener{@OverridepublicvoidactionPerformed(ActionEvente){newThread(newRunnable(){publicvoidrun(){if(e.getActionCommand().equals("退出")){System.exit(0);}elseif(e.getActionCommand().equals("打开")){jfc=newJFileChooser();jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);result=jfc.showDialog(newJLabel(),"选择");if(JFileChooser.APPROVE_OPTION==result){file=jfc.getSelectedFile();}else{file=null;}Filemyfile=newFile("C:/Users/a/Desktop/count.txt");BufferedReaderbr;try{br=newBufferedReader(newFileReader(myfile));Stringtemp=br.readLine();intcount=Integer.parseInt(temp);if(count>0){count-=1;Stringa=String.valueOf(count);FileWriterfw=newFileWriter("C:/Users/a/Desktop/count.txt");BufferedWriterbw=newBufferedWriter(fw);bw.write(a,0,a.length());bw.flush();bw.close();}else{System.out.println("超过使用次数!");System.exit(0);}}catch(FileNotFoundExceptione3){e3.printStackTrace();}catch(IOExceptione1){e1.printStackTrace();}if(file!=null){jl.setText("正在解密");//临时解密Filefile1=newFile(jfc.getCurrentDirectory()+"\\new.avi");Filefile2=newFile(jfc.getCurrentDirectory()+"\\new2.avi");try{in=newFileInputStream(file);on=newFileOutputStream(file1,true);on2=newFileOutputStream(file2,true);inttempbyte;inti=0;while((tempbyte=in.read())!=-1){if(i==0){if(tempbyte==5){jl.setText("无法打开");on.close();on2.close();file1.delete();file2.delete();return;}on2.write(~tempbyte);i++;continue;}elseif(i<3){on.write(~tempbyte);on2.write(tempbyte);i++;continue;}else{on.write(tempbyte);on2.write(tempbyte);i++;continue;}}in.close();on.close();on2.close();file.delete();file2.renameTo(file);MEDIAPlayer.this.remove(jl);}catch(IOExceptionee){ee.printStackTrace();return;}//播放视频try{player=Manager.createPlayer(newMediaLocator("file:"+file1.getPath()));}catch(NoPlayerExceptione1){e1.printStackTrace();}catch(IOExceptione1){e1.printStackTrace();}player.addControllerListener(MEDIAPlayer.this);player.realize();}}elseif(e.getActionCommand().equals("加密")){jfc=newJFileChooser();jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);result=jfc.showDialog(newJLabel(),"选择");if(JFileChooser.APPROVE_OPTION==result){file=jfc.getSelectedFile();}else{file=null;}if(file!=null){jl.setText("正在加密");Filenew_file=newFile(jfc.getCurrentDirectory()+"\\new.avi");try{in=newFileInputStream(file);on=newFileOutputStream(new_file,true);inttempbyte;inti=0;on.write(0);while((tempbyte=in.read())!=-1){if(i<2){on.write(~tempbyte);i++;continue;}else{on.write(tempbyte);i++;continue;}}in.close();on.close();file.delete();new_file.renameTo(file);jl.setText("加密完成");}catch(IOExceptionee){ee.printStackTrace();return;}}}elseif(e.getActionCommand().equals("解密")){jfc=newJFileChooser();jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);result=jfc.showDialog(newJLabel(),"选择");if(JFileChooser.APPROVE_OPTION==result){file=jfc.getSelectedFile();}else{file=null;}if(file!=null){jl.setText("正在解密");Filenew_file=newFile(jfc.getCurrentDirectory()+"\\new.avi");try{in=newFileInputStream(file);on=newFileOutputStream(new_file,true);inttempbyte;inti=0;while((tempbyte=in.read())!=-1){if(i==0){i++;continue;}elseif(i<3){on.write(~tempbyte);i++;continue;}else{on.write(tempbyte);i++;continue;}}in.close();on.close();file.delete();new_file.renameTo(file);jl.setText("解密完成");}catch(IOExceptionee){ee.printStackTrace();return;}}}}}).start();}}publicstaticvoidmain(String[]args)throwsException{Filefile=newFile("C:\\Users\\a\\Desktop","count.txt");if(!file.exists()){file.createNewFile();Strings="5";FileWriterfw=newFileWriter("C:/Users/a/Desktop/count.txt");BufferedWriterbw=newBufferedWriter(fw);bw.write(s,0,s.length());bw.flush();bw.close();}tr
JMenuItemitem=newJMenuItem(arrayItem[i][j]);
item.addActionListener(newGActionListener());
Menu.add(item);
JMB.add(Menu);
JMB.setBounds(0,0,500,20);
this.getContentPane().add(JMB);
publicvoiditemStateChanged(ItemEvente){
publicvoidcontrollerUpdate(ControllerEventce){
if(ceinstanceofRealizeCompleteEvent){
//player实例化完成后进行player播放前预处理
player.prefetch();
}elseif(ceinstanceofPrefetchCompleteEvent){
if(visual!
=null)
return;
//取得player中的播放视频的组件,并得到视频窗口的大小
//然后把视频窗口的组件添加到Frame窗口中,
if((visual=player.getVisualComponent())!
Dimensionsize=visual.getPreferredSize();
videoWidth=size.width;
videoHeight=size.height;
visual.setBounds(0,0,videoWidth,videoHeight);
this.add(visual);
}else{
videoWidth=320;
//取得player中的视频播放控制条组件,并把该组件添加到Frame窗口中
if((control=player.getControlPanelComponent())!
controlHeight=control.getPreferredSize().height;
control.setBounds(0,videoHeight,videoWidth,controlHeight+insetHeight);
this.add(control);
//设定Frame窗口的大小,使得满足视频文件的默认大小
this.setSize(videoWidth+insetWidth,videoHeight+controlHeight+insetHeight);
this.validate();
//启动视频播放组件开始播放
player.start();
}elseif(ceinstanceofEndOfMediaEvent){
//当播放视频完成后,把时间进度条恢复到开始,并再次重新开始播放
player.setMediaTime(newTime(0));
privateclassGActionListenerimplementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente){
newThread(newRunnable(){
publicvoidrun(){
if(e.getActionCommand().equals("退出")){
System.exit(0);
}elseif(e.getActionCommand().equals("打开")){
jfc=newJFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
result=jfc.showDialog(newJLabel(),"选择");
if(JFileChooser.APPROVE_OPTION==result){
file=jfc.getSelectedFile();
file=null;
Filemyfile=newFile("C:
/Users/a/Desktop/count.txt");
BufferedReaderbr;
br=newBufferedReader(newFileReader(myfile));
Stringtemp=br.readLine();
intcount=Integer.parseInt(temp);
if(count>0){
count-=1;
Stringa=String.valueOf(count);
FileWriterfw=newFileWriter("C:
BufferedWriterbw=newBufferedWriter(fw);
bw.write(a,0,a.length());
bw.flush();
bw.close();
else{
System.out.println("超过使用次数!
");
}catch(FileNotFoundExceptione3){
e3.printStackTrace();
jl.setText("正在解密");
//临时解密
Filefile1=newFile(jfc.getCurrentDirectory()+"\\new.avi");
Filefile2=newFile(jfc.getCurrentDirectory()+"\\new2.avi");
in=newFileInputStream(file);
on=newFileOutputStream(file1,true);
on2=newFileOutputStream(file2,true);
inttempbyte;
inti=0;
while((tempbyte=in.read())!
=-1){
if(i==0){
if(tempbyte==5){
jl.setText("无法打开");
on2.close();
file1.delete();
file2.delete();
on2.write(~tempbyte);
i++;
continue;
}elseif(i<3){
on.write(~tempbyte);
on2.write(tempbyte);
on.write(tempbyte);
file.delete();
file2.renameTo(file);
MEDIAPlayer.this.remove(jl);
}catch(IOExceptionee){
ee.printStackTrace();
//播放视频
player=Manager.createPlayer(newMediaLocator("file:
"+file1.getPath()));
}catch(NoPlayerExceptione1){
player.addControllerListener(MEDIAPlayer.this);
player.realize();
elseif(e.getActionCommand().equals("加密")){
jl.setText("正在加密");
Filenew_file=newFile(jfc.getCurrentDirectory()+"\\new.avi");
on=newFileOutputStream(new_file,true);
on.write(0);
if(i<2){
new_file.renameTo(file);
jl.setText("加密完成");
elseif(e.getActionCommand().equals("解密")){
jl.setText("解密完成");
}).start();
publicstaticvoidmain(String[]args)throwsException{
Filefile=newFile("C:
\\Users\\a\\Desktop","count.txt");
if(!
file.exists()){
file.createNewFile();
Strings="5";
bw.write(s,0,s.length());
tr
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1