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

📄 wplayer.java

📁 支持播放列表一系列操作 支持歌词秀 音频支持格式wav snd midi
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

   		return clip;
	}
	
	static String getFileName(String musicPath)
	{
		int index1 = musicPath.lastIndexOf(File.separator);
		int index2 = musicPath.lastIndexOf(".");  
		
  		if(index2 < 0 || index2 == musicPath.length()-1)   
      		return "";   
  		return musicPath.substring(index1 + 1, index2);		
	}
	

	
	String getPath()
	{
		return path;
	}
	
	String getName()
	{
		return name;
	}
	
	long getLength()
	{
		return length;
	}
	
	String musicToString()
	{
		String label = "";
		if(path == null)
			return null;
		long min = length / 60000;
		long sec = length % 60000 / 1000;
		String time;
		if(sec > 9)
			time = min + ":" + sec;
		else
			time = min + ":0" + sec;
		if(name.getBytes().length > 21 - time.length() - 1)
		{
			String lname = name;
			while(lname.getBytes().length > 21 - time.length() - 3)
				lname = lname.substring(0, lname.length() - 1);
			label += lname;
			label += "...";
		}
		else
		{
			label += name;
			
		}
		for(int i = label.getBytes().length; i < 22 - time.length() - 1; i++)
			label += " ";
		label += time;
		return label;	
	}
}

class PlayMode
{
	static final int SINGLE = 1;
	static final int SINGLE_LOOP = 2;
	static final int ALL_LOOP = 3;
	static final int RANDOM = 4;
}

class SortMode
{
	static final int LENGTH_SORT = 1;
	static final int NAME_SORT = 2;
	static final int RANDOM_SORT = 3;
}

class RollingLyric extends JPanel implements Runnable
{
	int centerX = 0;
	int centerY = 0;
	Thread runThread;
	int lineIndex = 0;
	int pixelIndex = 0;
	int fontSize = 12;
	int verticalSpacing = fontSize / 4; 
	Font wordFont = new Font("宋体", Font.PLAIN, fontSize);
	Toolkit tl = Toolkit.getDefaultToolkit();   
  	Dimension screenSize = tl.getScreenSize();
  	int screenWidth = (int)screenSize.getWidth();
  	int screenHeight = (int)screenSize.getHeight();
  	Lyric lyric;
  	int[] lineStartPixel = null;
  	LyricLine[] lyricLine = null;
  	
  	int pixel = 0;
  	Color clNormal = Color.BLACK;
  	Color clHighlight = Color.RED;

	RollingLyric(String path, int width, int height)
	{
		setDoubleBuffered(true);
		setBackground(Color.white);
		centerX = width / 2;
		centerY = height / 2;
		lyric = new Lyric(path);
		lyricLine = lyric.getLyricLines();
		lineStartPixel = new int[lyricLine.length];
		for(int i = 0; i < lyricLine.length; i++)
		{
			lineStartPixel[i] = centerX - lyricLine[i].getLyric().getBytes().length * fontSize / 2 / 2;
		}
		setTime(0);
	}
	
	void setLyric(String path)
	{
		setBackground(Color.white);

		lyric = new Lyric(path);
		lyricLine = lyric.getLyricLines();
		lineStartPixel = new int[lyricLine.length];
		for(int i = 0; i < lyricLine.length; i++)
		{
			lineStartPixel[i] = centerX - lyricLine[i].getLyric().getBytes().length * fontSize / 2 / 2;
		}
	}
	
	void setTime(long time)
	{
		if(lyricLine != null && lyricLine.length > 0)
		{
			if(time <= lyricLine[0].getTime())
			{
				int p = (int)Math.round((lyricLine[0].getTime() - time) / 100.0);
				lineIndex = p / (fontSize + verticalSpacing);
				pixelIndex = p % (fontSize + verticalSpacing);
				lineIndex = -lineIndex - 1;
				pixelIndex = (fontSize + verticalSpacing) - pixelIndex - 1;
				pixel = lineIndex * (fontSize + verticalSpacing) + pixelIndex;
	
			}
			else if(time <= lyricLine[lyricLine.length - 1].getTime())
			{
				int i = 0;
				for( ; lyricLine[i].getTime() < time ; i++)
					;
				lineIndex = i - 1;
				pixelIndex	= (int)Math.round((double)(time - lyricLine[lineIndex].getTime())
							/ (double)(lyricLine[lineIndex + 1].getTime() - lyricLine[lineIndex].getTime()) 
							* (fontSize + verticalSpacing));
				pixel = lineIndex * (fontSize + verticalSpacing) + pixelIndex;							
			}
			else
			{
				lineIndex = lyricLine.length;
				pixelIndex = 0;
				pixel = lineIndex * (fontSize + verticalSpacing) + pixelIndex;
			}
		}
	}
	
	public void start()
	{
		if(runThread == null)
		{
			runThread = new Thread(this);
			runThread.start();
		}
	}
	
	public void stop()
	{
		if(runThread != null)
		{
			runThread.stop();
			runThread = null;
		}
	}
	
	public void update(Graphics g)   
	{   
    	paint(g);   
	}   
	
	public void run()
	{
		while(true)
		{
		
			try
			{
				if(pixel < 0)
				{
					System.out.println("1000");	
					Thread.sleep(100);
					
				}
				else if(pixel < (lyricLine.length - 1) * (fontSize + verticalSpacing))
				{
			
					System.out.println(lyricLine[lineIndex + 1].getTime() - lyricLine[lineIndex].getTime());
					Thread.sleep(Math.round((double)(lyricLine[lineIndex + 1].getTime() - lyricLine[lineIndex].getTime()) / (fontSize + verticalSpacing)));
				}
				else
				{
					System.out.println("1000");	
					Thread.sleep(100);
				}
			}
			catch(InterruptedException e){};
			pixel++;
			repaint();

			
			lineIndex = pixel / (fontSize + verticalSpacing);
			pixelIndex = pixel % (fontSize + verticalSpacing);
		}
	}
	
	void modifySize(int width, int height)
	{
		centerX = width / 2;
		centerY = height / 2;
		for(int i = 0; i < lyricLine.length; i++)
		{
			lineStartPixel[i] = centerX - lyricLine[i].getLyric().getBytes().length * fontSize / 2 / 2;
		}
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		int tempPixel = pixel;
		g.setFont(wordFont);		
		g.setColor(clNormal);
		lineIndex = pixel / (fontSize + verticalSpacing);
		pixelIndex = pixel % (fontSize + verticalSpacing);
		
		for(int i = 0; i < lyricLine.length; i ++)
		{
			if(lineIndex == i && pixel >= 0)
			{
				if(pixelIndex < fontSize)
				{			
					g.setColor(clHighlight);
					g.drawString(lyricLine[i].getLyric(), lineStartPixel[i], centerY + (fontSize + verticalSpacing) * i - tempPixel);
					g.setColor(clNormal);
				}
				else
				{
					int red = (int)(clHighlight.getRed() * ((double)(fontSize + verticalSpacing - pixelIndex) / verticalSpacing) + clNormal.getRed() * ((double)(pixelIndex - fontSize) / verticalSpacing));

					int green = (int)(clHighlight.getGreen() * ((double)(fontSize + verticalSpacing - pixelIndex) / verticalSpacing) + clNormal.getGreen() * ((double)(pixelIndex - fontSize) / verticalSpacing));
					int blue = (int)(clHighlight.getBlue() * ((double)(fontSize + verticalSpacing - pixelIndex) / verticalSpacing) + clNormal.getBlue() * ((double)(pixelIndex - fontSize) / verticalSpacing));
					g.setColor(new Color(red, green, blue));
					g.drawString(lyricLine[i].getLyric(), lineStartPixel[i], centerY + (fontSize + verticalSpacing) * i - tempPixel);
					g.setColor(clNormal);
				}
			}
			else if(i == lineIndex + 1 && pixel >= 0)
			{
				if(pixelIndex < fontSize)
				{
					g.drawString(lyricLine[i].getLyric(), lineStartPixel[i], centerY + (fontSize + verticalSpacing) * i - tempPixel);
				}
				else
				{	
					
					int red = (int)(clNormal.getRed() * ((double)(fontSize + verticalSpacing - pixelIndex) / verticalSpacing) + clHighlight.getRed() * ((double)(pixelIndex - fontSize) / verticalSpacing));
					int green = (int)(clNormal.getGreen() * ((double)(fontSize + verticalSpacing - pixelIndex) / verticalSpacing) + clHighlight.getGreen() * ((double)(pixelIndex - fontSize) / verticalSpacing));
					int blue = (int)(clNormal.getBlue() * ((double)(fontSize + verticalSpacing - pixelIndex) / verticalSpacing) + clHighlight.getBlue() * ((double)(pixelIndex - fontSize) / verticalSpacing));
					g.setColor(new Color(red, green, blue));
					g.drawString(lyricLine[i].getLyric(), lineStartPixel[i], centerY + (fontSize + verticalSpacing) * i - tempPixel);
					g.setColor(clNormal);
				
				}
			}
			else
				g.drawString(lyricLine[i].getLyric(), lineStartPixel[i], centerY + (fontSize + verticalSpacing) * i - tempPixel);

		}
	}
}

class Lyric
{
	String		artist		= "";
	String		title		= "";
	String		album		= "";
	String		editedBy	= "";
	long		offset		= 0;	
	int			total		= 0;
	static int	increment	= 20;
	LyricLine[]	lyricLine	= new LyricLine[100];
	
	Lyric(String path)
	{
		File lyricFile = new File(path);
		try
		{
			BufferedReader in = new BufferedReader(new FileReader(path));
			String line;
			while((line = in.readLine()) != null)
			{
				int count = 0;
				int left = 0;
				int right = 0;
				while(true)
				{
					left	=	line.indexOf("[", right);
					if(left < 0)
					{
						for( ; count > 0; count--)
						{
							lyricLine[total - count].setLyric(line.substring(right + 1, line.length()));
						}	
						break;
					}
					if(left > right + 1)
					{
						for( ; count > 0; count--)
						{
							lyricLine[total - count].setLyric(line.substring(right + 1, left));
						}	
					}
					right	=	line.indexOf("]", left);
					if(right < 0)
						break;
					String tag = line.substring(left + 1, right);
					int type = LyricTag.type(tag);
					if(type == LyricTag.ID_TAG)
					{
						String keyword = LyricTag.getKeyword(tag);
						if(keyword.equalsIgnoreCase("ar"))
							artist = LyricTag.getValue(tag);
						else if(keyword.equalsIgnoreCase("ti"))
							title = LyricTag.getValue(tag);
						else if(keyword.equalsIgnoreCase("al"))
							album = LyricTag.getValue(tag);
						else if(keyword.equalsIgnoreCase("by"))
							editedBy = LyricTag.getValue(tag);
					}
					else if(type == LyricTag.TIME_TAG)
					{
						count++;
						addLyricLine(LyricTag.getTime(tag));
					}
				}
			}
			in.close();
			qSort(0, total - 1);
		}
		catch(Exception e){};
	}
	
	LyricLine[] getLyricLines()
	{
		LyricLine[] temp = new LyricLine[total];
		for(int i = 0; i < total; i++)
			temp[i] = lyricLine[i]; 
		return temp;
	}
	
	void addLyricLine()
	{
		if(total >= lyricLine.length)
		{	
			LyricLine[] temp = new LyricLine[lyricLine.length + increment];
			for(int i = 0; i < lyricLine.length; i++)
			{
				temp[i] = lyricLine[i];
			}
			lyricLine = temp;
		}
		total++;
	}
	
	void addLyricLine(long time)
	{
		addLyricLine();
		total--;
		lyricLine[total] = new LyricLine(time);
		total++;
	}
	
	void addLyricLine(String lyric)
	{
		addLyricLine();
		total--;
		lyricLine[total] = new LyricLine(lyric);
		total++;
	}
	
	void addLyricLine(String lyric, long time)
	{
		addLyricLine();
		total--;
		lyricLine[total] = new LyricLine(lyric, time);
		total++;
	}
	
	String getLineLyric(int index)
	{
		if(index >= total)
			return null;
		return lyricLine[index].getLyric();
	}
	
	void setLineLyric(String lyric, int index)
	{
		if(index < total)
			lyricLine[index].setLyric(lyric);
	}
	

	
	private void qSort(int low, int high)
	{
		if(low < high)
		{
			int pivotLoc = partition(low, high);
			qSort(low, pivotLoc - 1);
			qSort(pivotLoc + 1, high);
		}
	}
	
	private int partition(int low, int high)
	{
		LyricLine temp = lyricLine[low];
		long pivotKey = lyricLine[low].getTime();
		while(low < high)
		{
			while(low < high && lyricLine[high].getTime() >= pivotKey)
				--high;
			lyricLine[low] = lyricLine[high];
			while(low < high && lyricLine[low].getTime() <= pivotKey)
				++low;
			lyricLine[high] = lyricLine[low];
		}
		lyricLine[low] = temp;
		return low;
	}
	
	void showAllLyric()
	{
		System.out.println(artist + title + album + editedBy + offset);
		for(int i = 0; i < total; i++)
		{
			System.out.println( lyricLine[i].getTime() + lyricLine[i].getLyric());
		}
	}
}

class LyricLine
{
	String 	lyric	= "";
	long 	time 	= 0;
	
	LyricLine(long time)
	{
		this.time = time;
	}
	
	LyricLine(String lyric)
	{
		this.lyric = lyric;
	}
	
	LyricLine(String lyric, long time)
	{
		this.lyric = lyric;
		this.time = time;
	}
	
	void setLyric(String lyric)
	{
		this.lyric = lyric;
	}
	
	void setTime(long time)
	{
		this.time = time;
	}
	
	String getLyric()
	{
		return lyric;
	}
	
	long getTime()
	{
		return time;
	}
}

class LyricTag
{
	static 	int	TIME_TAG	= 1;
	static 	int	ID_TAG		= 2;
	static	int ERROR_TAG	= 3;
	
	static	int type(String tag)
	{
		int colon = tag.indexOf(":");
		if(colon < 0)
			return ERROR_TAG;
		try
		{
			Integer.parseInt(tag.substring(0, colon));
		}
		catch(NumberFormatException e)
		{
			return ID_TAG;
		}
		try
		{
			Double.parseDouble(tag.substring(colon + 1, tag.length()));
		}
		catch(NumberFormatException e)
		{
			return ERROR_TAG;
		}
		return TIME_TAG;
	}
	
	static String getKeyword(String tag)
	{
		int colon = tag.indexOf(":");
		return tag.substring(0, colon);
	}
	
	static String getValue(String tag)
	{
		int colon = tag.indexOf(":");
		return tag.substring(colon + 1, tag.length());
	}
	
	static long getTime(String tag)
	{
		int colon = tag.indexOf(":");
		double time = 0;
		try
		{
			time = (double)Integer.parseInt(tag.substring(0, colon));
		}
		catch(NumberFormatException e){};
		time *= 60;
		try
		{
			time += Double.parseDouble(tag.substring(colon + 1, tag.length()));
		}
		catch(NumberFormatException e){};
		time *= 1000;
		return Math.round(time);
	}
}

⌨️ 快捷键说明

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