📄 baseplaylist.java
字号:
/*
* BasePlaylist.
*
* JavaZOOM : jlgui@javazoom.net
* http://www.javazoom.net
*
*-----------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*----------------------------------------------------------------------
*/
package javazoom.jlgui.player.amp.playlist;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import javazoom.jlgui.player.amp.util.Config;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* BasePlaylist implementation.
* This class implements Playlist interface using a Vector.
* It support .m3u and .pls playlist format.
*/
public class BasePlaylist implements Playlist
{
protected Vector _playlist = null;
protected int _cursorPos = -1;
protected boolean isModified;
protected String M3UHome = null;
protected String PLSHome = null;
private static Log log = LogFactory.getLog(BasePlaylist.class);
/**
* Constructor.
*/
public BasePlaylist()
{
_playlist = new Vector();
}
public boolean isModified()
{
return isModified;
}
/**
* Loads playlist as M3U format.
*/
public boolean load(String filename)
{
setModified(true);
boolean loaded = false;
if ((filename != null) && (filename.toLowerCase().endsWith(".m3u")))
{
loaded = loadM3U(filename);
}
else if ((filename != null) && (filename.toLowerCase().endsWith(".pls")))
{
loaded = loadPLS(filename);
}
return loaded;
}
/**
* Load playlist from M3U format.
*
* @param filename
* @return
*/
protected boolean loadM3U(String filename)
{
Config config = Config.getInstance();
_playlist = new Vector();
boolean loaded = false;
BufferedReader br = null;
try
{
// Playlist from URL ? (http:, ftp:, file: ....)
if (Config.startWithProtocol(filename))
{
br = new BufferedReader(new InputStreamReader((new URL(filename)).openStream()));
}
else
{
br = new BufferedReader(new FileReader(filename));
}
String line = null;
String songName = null;
String songFile = null;
String songLength = null;
while ((line = br.readLine()) != null)
{
if (line.trim().length() == 0) continue;
if (line.startsWith("#"))
{
if (line.toUpperCase().startsWith("#EXTINF"))
{
int indA = line.indexOf(",", 0);
if (indA != -1)
{
songName = line.substring(indA + 1, line.length());
}
int indB = line.indexOf(":", 0);
if (indB != -1)
{
if (indB < indA) songLength = (line.substring(indB + 1, indA)).trim();
}
}
}
else
{
songFile = line;
if (songName == null) songName = songFile;
if (songLength == null) songLength = "-1";
PlaylistItem pli = null;
if (Config.startWithProtocol(songFile))
{
// URL.
pli = new PlaylistItem(songName, songFile, Long.parseLong(songLength), false);
}
else
{
// File.
File f = new File(songFile);
if (f.exists())
{
pli = new PlaylistItem(songName, songFile, Long.parseLong(songLength), true);
}
else
{
// Try relative path.
f = new File(config.getLastDir() + songFile);
if (f.exists())
{
pli = new PlaylistItem(songName, config.getLastDir() + songFile, Long.parseLong(songLength), true);
}
else
{
// Try optional M3U home.
if (M3UHome != null)
{
if (Config.startWithProtocol(M3UHome))
{
pli = new PlaylistItem(songName, M3UHome + songFile, Long.parseLong(songLength), false);
}
else
{
pli = new PlaylistItem(songName, M3UHome + songFile, Long.parseLong(songLength), true);
}
}
}
}
}
if (pli != null) this.appendItem(pli);
songFile = null;
songName = null;
songLength = null;
}
}
loaded = true;
}
catch (Exception e)
{
log.debug("Can't load .m3u playlist", e);
}
finally
{
try
{
if (br != null)
{
br.close();
}
}
catch (Exception ioe)
{
log.info("Can't close .m3u playlist", ioe);
}
}
return loaded;
}
/**
* Load playlist in PLS format.
*
* @param filename
* @return
*/
protected boolean loadPLS(String filename)
{
Config config = Config.getInstance();
_playlist = new Vector();
boolean loaded = false;
BufferedReader br = null;
try
{
// Playlist from URL ? (http:, ftp:, file: ....)
if (Config.startWithProtocol(filename))
{
br = new BufferedReader(new InputStreamReader((new URL(filename)).openStream()));
}
else
{
br = new BufferedReader(new FileReader(filename));
}
String line = null;
String songName = null;
String songFile = null;
String songLength = null;
while ((line = br.readLine()) != null)
{
if (line.trim().length() == 0) continue;
if ((line.toLowerCase().startsWith("file")))
{
StringTokenizer st = new StringTokenizer(line, "=");
st.nextToken();
songFile = st.nextToken().trim();
}
else if ((line.toLowerCase().startsWith("title")))
{
StringTokenizer st = new StringTokenizer(line, "=");
st.nextToken();
songName = st.nextToken().trim();
}
else if ((line.toLowerCase().startsWith("length")))
{
StringTokenizer st = new StringTokenizer(line, "=");
st.nextToken();
songLength = st.nextToken().trim();
}
// New entry ?
if (songFile != null)
{
PlaylistItem pli = null;
if (songName == null) songName = songFile;
if (songLength == null) songLength = "-1";
if (Config.startWithProtocol(songFile))
{
// URL.
pli = new PlaylistItem(songName, songFile, Long.parseLong(songLength), false);
}
else
{
// File.
File f = new File(songFile);
if (f.exists())
{
pli = new PlaylistItem(songName, songFile, Long.parseLong(songLength), true);
}
else
{
// Try relative path.
f = new File(config.getLastDir() + songFile);
if (f.exists())
{
pli = new PlaylistItem(songName, config.getLastDir() + songFile, Long.parseLong(songLength), true);
}
else
{
// Try optional PLS home.
if (PLSHome != null)
{
if (Config.startWithProtocol(PLSHome))
{
pli = new PlaylistItem(songName, PLSHome + songFile, Long.parseLong(songLength), false);
}
else
{
pli = new PlaylistItem(songName, PLSHome + songFile, Long.parseLong(songLength), true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -