⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playtitle.java

📁 模拟播放SRC文件的程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class PlayTitle extends Frame implements Runnable
{
	private String fileName;
	private Label lable;
	private Button btnRun;
	private TextField txtTime;
	private Panel panel;
	private Thread thread;
	private Vector vt;
	private int startTime=0;

	public static void main(String[] args){
		if(args.length==0){
			System.err.println("Usage: java ChangeTime <file> <delay>");
			System.exit(1);
		}

		PlayTitle playTitle=new PlayTitle(args[0]);
		playTitle.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});

		playTitle.setSize(700,80);
		playTitle.show();
		playTitle.play();
	}

	public PlayTitle(String fileName){
		this.fileName=fileName;
		setBackground(Color.black);
		setLayout(new BorderLayout());
		lable=new Label("OK");
		lable.setForeground(Color.white);
		lable.setFont(new Font(lable.getName(),Font.BOLD,16));
		add(lable,BorderLayout.CENTER);
		panel=new Panel();
		panel.setLayout(new BorderLayout());
		txtTime=new TextField("00:00:00",8);
		txtTime.setFont(new Font(lable.getName(),Font.BOLD,12));
		btnRun=new Button("开始");
		btnRun.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				startTime=toTime(txtTime.getText());
//				if(!thread.isAlive())
				if(btnRun.getLabel().equals("开始"))
//				if(!thread.isInterrupted())
				{
					thread.start();
					btnRun.setLabel("停止");
				}else{
					try{
						thread.interrupt();
//					thread.sleep(360000000);
//					}catch(InterruptedException ex){}
					}catch(SecurityException ex){}
//					thread.yield();
					btnRun.setLabel("开始");
				}
	//			play();
			}
		});
		panel.add(txtTime,BorderLayout.CENTER);
		panel.add(btnRun,BorderLayout.SOUTH);
		add(panel,BorderLayout.EAST);
	}

	private int toTime(String timeZone){
			int hour=Integer.parseInt(timeZone.substring(0,2));
			int minute=Integer.parseInt(timeZone.substring(3,5));
			int second=Integer.parseInt(timeZone.substring(6,8));
	//		int msec=Integer.parseInt(timeZone.substring(9));
			return (((hour*60)+minute)*60+second)*1000;
		}

	public void play() {

		BufferedReader bio=null;
//		BufferedWriter bout=null;
		String line;
		vt=new Vector();
		
		try{
		bio=new BufferedReader(new FileReader(fileName));
		}
		catch(FileNotFoundException e){
			System.err.println("File not found");
			System.exit(1);
		}

		try{
//			line=bio.readLine();
		while((line=bio.readLine())!=null){
//		  while((line!=null)&&(line.length()<>0)){
			int no=Integer.parseInt(line);
			String timeZone=bio.readLine();
			String text="";
			while(((line=bio.readLine())!=null) &&
				(line.length()!=0)){
				text+=line+"\n";
			}
//			System.out.println(text);
			FilmFrame ff=new FilmFrame(no,timeZone,text);
			vt.add(ff);
//			System.out.println(""+no);
		}
		}
		catch(IOException e){
			System.err.println("File read error");
			System.exit(1);
		}
				if(thread==null)
					thread=new Thread(this);

	}

	public void run(){
		int etime=0;
		boolean go=false;
		for (int i=0;i<vt.size() ;i++ )
		{
			FilmFrame ff=(FilmFrame)vt.elementAt(i);
			int time=0;
			if(ff.getBeginTime()>=startTime)
				go=true;
			if(go){
			String str=ff.getText();
			time=ff.getTime();
			if(i==0)
			{
				time+=ff.getBeginTime();
			}else{
				if(ff.getBeginTime()-etime>0)
					time+=ff.getBeginTime()-etime;
			}
			lable.setText(ff.getText());
			time-=time*1/450;
			if(time>0)
			try{
				Thread.sleep(time);
			}catch(InterruptedException e){}
			}
			etime=ff.getEndTime();

		}
	}

	class FilmFrame implements Serializable
	{
		private int beginTime;
		private int endTime;
		private int seqence;
		private String text;

		public FilmFrame(int seqence,int beginTime,int endTime,String text){
			this.seqence=seqence;
			this.beginTime=beginTime;
			this.endTime=endTime;
			this.text=text;
		}
		public FilmFrame(int seqence,String timeZone,String text){
			this.seqence=seqence;
			this.beginTime=toTime(timeZone.substring(0,12));
			this.endTime=toTime(timeZone.substring(17));
			this.text=text;
		}

		public int getSequence(){
			return seqence;
		}

		public String getText(){
			return text;
		}

		public void changeTime(int delay){
			beginTime+=delay;
			endTime+=delay;
		}
		
		private int toTime(String timeZone){
			int hour=Integer.parseInt(timeZone.substring(0,2));
			int minute=Integer.parseInt(timeZone.substring(3,5));
			int second=Integer.parseInt(timeZone.substring(6,8));
			int msec=Integer.parseInt(timeZone.substring(9));
			return (((hour*60)+minute)*60+second)*1000+msec;
		}

		private String toTimeZone(int time){
			int hour;
			int minute;
			int second;
			int msec;
			String result="";

			hour=(int)Math.floor(time/3600000);
			minute=(int)Math.floor((time % 3600000)/60000);
			second=(int)Math.floor(((time % 3600000) %60000 )/1000);
			msec=time-hour*3600000-minute*60000-second*1000;
			
			result+=((hour>=10)?(""+hour):("0"+hour))+":";
			result+=((minute>=10)?(""+minute):("0"+minute))+":";
			result+=(second>=10)?(""+second):("0"+second);
			result+=","+((msec>=100)?(""+msec):((msec>=10)?("0"+msec):("00"+msec)));

			return result;

		}

		public String getTimeZone(){
			return toTimeZone(beginTime)+" --> "+toTimeZone(endTime);
		}

		public String toString(){
			return seqence+"\n"+
				getTimeZone()+"\n"+
				text+"\n";
		}

		public int getBeginTime(){
			return beginTime;
		}

		public int getTime(){
			return endTime-beginTime;
		}

		public int getEndTime(){
			return endTime;
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -