📄 wplayer.java
字号:
list[total] = new MusicList(path);
total++;
}
void deleteMusicList(int index)
{
if(index < total)
{
total--;
for(list[index] = null; index < total; index++)
list[index] = list[index + 1];
}
}
String[] toStrings()
{
String[] output = new String[total];
for(int i = 0; i < total; i++)
{
output[i] = list[i].listName;
}
return output;
}
}
class MusicList
{
Music[] list = new Music[1];
String listName = null;
String listPath = null;
int total = 0;
static int increment = 20;
int current = -1;
int[] record = new int[100];
int rCurrent = 0;
int rStart = 0;
int rEnd = 0;
static int lineStart = 0;
MusicList(String listPath)
{
this.listPath = listPath;
FileInputStream playerListFileInputStream = null;
try
{
playerListFileInputStream = new FileInputStream(listPath);
}
catch(Exception e){};
if(playerListFileInputStream != null)
{
String text = null;
String line = null;
try
{
byte[] bit = new byte[playerListFileInputStream.available()];
playerListFileInputStream.read(bit);
text = new String(bit, "UTF-8");
}
catch(Exception e){};
lineStart = 0;
line = getLine(text);
if(line != null)
{
line = getLine(text);
if(line != null)
{
listName = getValue(line, "title");
line = getLine(text);
if(line != null)
{
int count = Integer.parseInt(getValue(line, "count"));
list = new Music[count];
for(int i = 0; i < count; i++)
{
line = getLine(text);
if(line == null)
continue;
String path = getValue(line, "file");
String name = getValue(line, "title");
String strLength = getValue(line, "len");
long length = 0;
try
{
length = Long.parseLong(strLength);
}
catch(NumberFormatException e){};
if(path != null)
{
if(length != 0 && name != null)
addMusic(path, name, length);
else if(length == 0 && name != null)
addMusic(path, name);
else if(length != 0 && name == null)
addMusic(path, length);
else
addMusic(path);
}
}
try
{
playerListFileInputStream.close();
}
catch(java.io.IOException e){};
//
}
}
}
}
}
void save(String path)
{
try
{
String newLine = System.getProperty("line.separator");
String text = "";
text += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" + newLine;
text += "<ttplaylist title=\"" + listName + "\" version=\"4\" generator=\"TTPlayer -- 5.0.1\">" + newLine;
text += "\t<items count=\"" + total + "\">" + newLine;
for(int i = 0; i < total; i++)
{
text += "\t\t<item file=\"" + list[i].getPath() + "\" title=\"" + list[i].getName() + "\" len=\"" + list[i].getLength() + "\"/>" + newLine;
}
text += "\t</items>" + newLine;
text += "</ttplaylist>" + newLine;
byte[] bit = text.getBytes("UTF-8");
FileOutputStream playerListFileOutputStream = new FileOutputStream(path);
playerListFileOutputStream.write(bit);
playerListFileOutputStream.close();
}
catch(Exception e){};
}
String getValue(String source, String name)
{
String value = null;
int valueStart = source.indexOf(name);
if(valueStart == -1)
return null;
int valueEnd = source.indexOf("\"", valueStart);
if(valueEnd < 0)
return null;
valueEnd = source.indexOf("\"", valueEnd + 1);
if(valueEnd < 0)
return null;
/*if(valueEnd < valueStart)
valueEnd = source.indexOf(">", valueStart);*/
if(valueEnd < valueStart)
return null;
value = source.substring(valueStart, valueEnd + 1);
value = value.substring(value.indexOf("\"") + 1, value.lastIndexOf("\""));
return value;
}
String getLine(String text)
{
int i;
boolean fileEnd = true;
if(text == null)
return null;
for(i = lineStart; text.charAt(i) != -1; i++)
{
if(text.charAt(i) == '>')
{
fileEnd = false;
break;
}
}
if(fileEnd == true)
{
lineStart = i;
return null;
}
String line = text.substring(lineStart, i + 1);
lineStart = i + 1;
int first = line.indexOf("<");
int last = line.lastIndexOf(">");
if(first < 0 || last < 0 || first > last)
return null;
line = line.substring(first, last + 1);
return line;
}
void setNewRecord(int index)
{
current = index;
record = new int[100];
rCurrent = 0;
rStart = 0;
rEnd = 0;
}
int getTotal()
{
return total;
}
void currentPP()
{
current = (current + 1) % total;
}
void currentMM()
{
current = (current - 1 + list.length) % total;
}
int getCurrent()
{
return current;
}
int getPrevious()
{
rCurrent = (rCurrent - 1 + 100) % 100;
if((rCurrent + 1) % 100 == rStart)
{
current = (int)Math.floor(Math.random() * total);
record[rCurrent] = current;
rStart = rCurrent;
if(rStart == rEnd)
{
rEnd = (rStart - 1 + 100) % 100;
}
}
current = record[rCurrent];
return current;
}
int getNext()
{
rCurrent = (rCurrent + 1) % 100;
if((rCurrent - 1 + 100) % 100 == rEnd)
{
current = (int)Math.floor(Math.random() * total);
record[rCurrent] = current;
rEnd = rCurrent;
if(rStart == rEnd)
{
rStart = (rEnd + 1) % 100;
}
}
current = record[rCurrent];
return current;
}
String getMusicPath(int index)
{
if(index < 0 || index > total)
return null;
return list[index].getPath();
}
String getNextMusicPath(int mode)
{
if(current < 0 && current >= list.length)
return null;
switch(mode)
{
case PlayMode.SINGLE:
{
return null;
}
case PlayMode.SINGLE_LOOP:
{
return list[current].getPath();
}
case PlayMode.ALL_LOOP:
{
current = (current + 1) % total;
return list[current].getPath();
}
case PlayMode.RANDOM:
{
current = getNext();
return list[current].getPath();
}
}
return null;
}
void sortByName()
{
qSort(0, total - 1, SortMode.NAME_SORT);
}
void sortByLength()
{
qSort(0, total - 1, SortMode.LENGTH_SORT);
}
void sortRandom()
{
if(total > 0)
{
for(int i = 0; i < 100; i++)
{
int a = (int)Math.floor(Math.random() * total);
int b = (int)Math.floor(Math.random() * total);
Music temp = list[a];
list[a] = list[b];
list[b] = temp;
}
}
}
void qSort(int low, int high, int key)
{
if(low < high)
{
int pivotLoc = partition(low, high, key);
qSort(low, pivotLoc - 1, key);
qSort(pivotLoc + 1, high, key);
}
}
int partition(int low, int high, int key)
{
Music temp = list[low];
switch(key)
{
case SortMode.NAME_SORT:
{
String pivotKey = list[low].getName();
while(low < high)
{
while(low < high && list[high].getName().compareToIgnoreCase(pivotKey) >= 0)
--high;
list[low] = list[high];
while(low < high && list[low].getName().compareToIgnoreCase(pivotKey) <= 0)
++low;
list[high] = list[low];
}
list[low] = temp;
return low;
}
case SortMode.LENGTH_SORT:
{
long pivotKey = list[low].getLength();
while(low < high)
{
while(low < high && list[high].getLength() >= pivotKey)
--high;
list[low] = list[high];
while(low < high && list[low].getLength() <= pivotKey)
++low;
list[high] = list[low];
}
list[low] = temp;
return low;
}
}
return -1;
}
void sort(int mode)
{
switch(mode)
{
case SortMode.LENGTH_SORT:
{
sortByLength();
break;
}
case SortMode.NAME_SORT:
{
sortByName();
break;
}
case SortMode.RANDOM_SORT:
{
sortRandom();
break;
}
default:
break;
}
}
void addMusic(String path)
{
if(total >= list.length)
{
Music[] temp = new Music[list.length + increment];
for(int i = 0; i < list.length; i++)
{
temp[i] = list[i];
}
list = temp;
}
list[total] = new Music();
list[total].path = path;
total++;
}
void addMusic(String path, String name)
{
addMusic(path);
total--;
list[total].name = name;
total++;
}
void addMusic(String path, long length)
{
addMusic(path);
total--;
list[total].length = length;
total++;
}
void addMusic(String path, String name, long length)
{
addMusic(path);
total--;
list[total].name = name;
list[total].length = length;
total++;
}
void deleteMusic()
{
total--;
list[total] = null;
}
void deleteMusic(int index)
{
if(index < total)
{
total--;
for(list[index] = null; index < total; index++)
list[index] = list[index + 1];
}
}
void deleteMusic(int[] indexs)
{
for(int i = indexs.length - 1; i >= 0; i--)
{
int index = indexs[i];
deleteMusic(index);
}
}
void deleteRepeatedMusic()
{
for(int i = total - 1; i >= 0; i--)
{
String path = list[i].getPath();
for(int j = total - 1; j > i; j--)
{
if(path.equals(list[j].getPath()))
{
deleteMusic(i);
break;
}
}
}
}
void deleteNonexistentMusic()
{
for(int i = total - 1; i >= 0; i--)
{
String path = list[i].getPath();
if(new File(path).exists() != true)
deleteMusic(i);
}
}
void deleteAllMusic()
{
while(total > 0)
{
deleteMusic();
}
}
String[] toStrings()
{
String[] output = new String[total];
for(int i = 0; i < total; i++)
{
output[i] = list[i].musicToString();
}
return output;
}
}
class Music
{
String path = null;
String name = null;
long length = 0;
void reset(String n, long l)
{
name = n;
length = l;
}
static Clip loadMusic(String musicPath)
{
Clip clip = null;
try
{
// From file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(musicPath));
// From URL
// stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
// Create the clip
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
clip = (Clip) AudioSystem.getLine(info);
// This method does not return until the audio file is completely loaded
clip.open(stream);
} catch (Exception e) {};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -