📄 basicplaylist.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.hadeslee.yoyoplayer.playlist;import com.hadeslee.yoyoplayer.util.*;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.Iterator;import java.util.List;import java.util.StringTokenizer;import java.util.Vector;/** * 一个播放列表的基本实现 * @author hadeslee */public class BasicPlayList implements PlayList { private static final long serialVersionUID = 20071214L; protected Vector<PlayListItem> playList; protected int currentIndex = -1; protected boolean isModified; protected String M3UHome;//MP3U格式列表的位置 protected String PLSHome;//PLS格式列表的位置 private String name;//表示此播放列表的名字 private Config config;//全局的配置对象 private PlayListItem playing;//正在播放的项 public BasicPlayList(Config config) { this.config = config; playList = new Vector<PlayListItem>(); } 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; } protected boolean loadM3U(String filename) { 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) { } finally { try { if (br != null) { br.close(); } } catch (Exception ioe) { } } name = Util.getSongName(new File(filename)); return loaded; } protected boolean loadPLS(String filename) { 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); } } } } } if (pli != null) { this.appendItem(pli); } songName = null; songFile = null; songLength = null; } } loaded = true; } catch (Exception e) { } finally { try { if (br != null) { br.close(); } } catch (Exception ioe) { } } name = Util.getSongName(new File(filename)); return loaded; } public boolean save(String filename) { // Implemented by C.K if (playList != null) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename)); bw.write("#EXTM3U");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -