主程序:
public class MovePlayList {
static
{
System.out.println("author weiyong");
System.out.println("version 1.0");
}
public static void main(String[] args) {
List<String> pathList=new ArrayList<String>();//创建集合存储已选择播放器路径,视屏文件夹路径,第二次打开默认路径
System.out.println("请输入播放器.exe的绝对路径:");
String playerPath=SelectWindowTool.selectFile();
pathList.add(playerPath);
System.out.println("请输入播放器视屏文件夹的绝对路径:");
String path=SelectWindowTool.selectFileDerectory();
pathList.add(path);
File file1=new File(path);
File[] listFiles = file1.listFiles(new VedioFileFilter());//视屏文件过滤器
Set<String> filePathSet=new TreeSet<String>();//存放所有文件绝对路径
System.out.println("当前目录播放文件:");
for (File file2 : listFiles) {
String path2 = file2.getPath();
filePathSet.add(path2);//添加文件绝对路径
}
int i=0;
Map<String,String> filePathMap=new HashMap<String,String>();
for (Iterator<String> iterator = filePathSet.iterator(); iterator.hasNext();i++) {//将文件路径按顺序存入TreeMap集合中
String pathStr = iterator.next();
filePathMap.put(i+"", pathStr);
}
for (String num : filePathMap.keySet()) {
System.out.println(num+":"+filePathMap.get(num));
}
Scanner input=new Scanner(System.in);
while(true){
System.out.println("选择播放模式:1、目录循环播放 2、播放目的文件 3、退出播放");
String selectNum=input.nextLine();
if("1".equals(selectNum)){
PlayVideoFile.playAllVedio(filePathSet,playerPath);//目录顺序播放
}else if("2".equals(selectNum)){
PlayVideoFile.playVedio(filePathMap,playerPath);//指定文件播放
}else if("3".equals(selectNum)){
break;
}
else{
System.out.println("哥们!!!不要瞎选哟");
continue;
}
}
}
}
操作类:
public class PlayVideoFile {
/**
* @param fileSet 文件夹中视屏文件的所有绝对路径
* @param playerPath 播放器的绝对路径
*/
public static void playAllVedio(Set<String> fileSet,String playerPath) {
Runtime rn = Runtime.getRuntime();
Process p = null;
Scanner input=new Scanner(System.in);
try {
for (String path : fileSet) {
System.out.println(path);
p = rn.exec(playerPath+" "+path);
System.out.println("输入任意字符播放下一个(输入exit退出)");
String s=input.nextLine();
if("exit".equals(s)){
p.destroy();
return;
}else{
p.destroy();
continue;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void playVedio(Map<String ,String> filePathMap,String playerPath){
Runtime rn = Runtime.getRuntime();
Process p = null;
Scanner input=new Scanner(System.in);
//打印播放列表
System.out.println("播放列表:");
for (int i = 0; i < filePathMap.size(); i++) {
String absolutePath=filePathMap.get(i+"");
//打印文件名
System.out.println(i+": "+absolutePath.substring(absolutePath.lastIndexOf(File.separator)+1, absolutePath.length()));
}
int i=0;
while(true){
try {
System.out.println("请输入0-"+(filePathMap.size()-1)+"选择播放的视屏(输入exit可退出)");
String s=input.nextLine();
if("exit".equals(s)){
break;
}else if(Integer.parseInt(s)<0|Integer.parseInt(s)>filePathMap.size()){
System.out.println("输入错误,请重新输入");
continue;
}else{
if(i>0){
p.destroy();
}
p = rn.exec(playerPath+" "+filePathMap.get(s));
i++;
}
} catch (IOException e) {
e.printStackTrace();
}catch(NumberFormatException e){
System.out.println("输入错误,请重新输入");
continue;
}
}
}
}
过滤器:
public class VedioFileFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
//本程序只选择播放avi的,可以自己改动,播放更多格式的
boolean flag=pathname.getName().endsWith(".itcast")|pathname.getName().endsWith(".avi");
return flag;
}
}
文件、文件夹选择窗口:
public class SelectWindowTool {
/**
* @return 选择的文件夹绝对路径
*/
public static String selectFileDerectory() {
JFileChooser jchoose = new JFileChooser();
jchoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jchoose.setDialogTitle("请选择视屏播放文件的文件夹");
int returnVal = jchoose.showOpenDialog(null);
File selectedFile = jchoose.getSelectedFile();//获取到选择的文件夹
File[] listFiles = selectedFile.listFiles(new VedioFileFilter());//过滤出视屏文件
if(listFiles.length==0){
System.out.println("所选文件夹下没有视屏文件,请选择视屏文件的根文件夹");
selectFileDerectory();
}
System.out.println(selectedFile.getAbsolutePath());
return selectedFile.getAbsolutePath();
}
/**
* @return 返回的选择的文件绝对路径
*/
public static String selectFile() {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("请选择视屏播放器");
int returnVal = chooser.showOpenDialog(null);
File selectedFile = chooser.getSelectedFile();
if(selectedFile.getAbsolutePath().endsWith(".exe")){
System.out.println(selectedFile.getAbsolutePath());
return selectedFile.getAbsolutePath();
}else{
System.out.println("选择错误!重新选择!!!");
selectFile();
}
return null;
}
}