baseplaylist.java

来自「java平台的图形音乐播放器」· Java 代码 · 共 586 行 · 第 1/2 页

JAVA
586
字号
                            }
                        }
                    }
                    if (pli != null) this.appendItem(pli);
                    songName = null;
                    songFile = null;
                    songLength = null;
                }
            }
            loaded = true;
        }
        catch (Exception e)
        {
            log.debug("Can't load .pls playlist", e);
        }
        finally
        {
            try
            {
                if (br != null)
                {
                    br.close();
                }
            }
            catch (Exception ioe)
            {
                log.info("Can't close .pls playlist", ioe);
            }
        }
        return loaded;
    }

    /**
     * Saves playlist in M3U format.
     */
    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");
                bw.newLine();
                Iterator it = _playlist.iterator();
                while (it.hasNext())
                {
                    PlaylistItem pli = (PlaylistItem) it.next();
                    bw.write("#EXTINF:" + pli.getM3UExtInf());
                    bw.newLine();
                    bw.write(pli.getLocation());
                    bw.newLine();
                }
                return true;
            }
            catch (IOException e)
            {
                log.info("Can't save playlist", e);
            }
            finally
            {
                try
                {
                    if (bw != null)
                    {
                        bw.close();
                    }
                }
                catch (IOException ioe)
                {
                    log.info("Can't close playlist", ioe);
                }
            }
        }
        return false;
    }

    /**
     * Adds item at a given position in the playlist.
     */
    public void addItemAt(PlaylistItem pli, int pos)
    {
        _playlist.insertElementAt(pli, pos);
        setModified(true);
    }

    /**
     * Searchs and removes item from the playlist.
     */
    public void removeItem(PlaylistItem pli)
    {
        _playlist.remove(pli);
        setModified(true);
    }

    /**
     * Removes item at a given position from the playlist.
     */
    public void removeItemAt(int pos)
    {
        _playlist.removeElementAt(pos);
        setModified(true);
    }

    /**
     * Removes all items from the playlist.
     */
    public void removeAllItems()
    {
        _playlist.removeAllElements();
        _cursorPos = -1;
        setModified(true);
    }

    /**
     * Append item at the end of the playlist.
     */
    public void appendItem(PlaylistItem pli)
    {
        _playlist.addElement(pli);
        setModified(true);
    }

    /**
     * Sorts items of the playlist.
     */
    public void sortItems(int sortmode)
    {
        // TODO
    }

    /**
     * Shuffles items in the playlist randomly
     */
    public void shuffle()
    {
        int size = _playlist.size();
        if (size < 2) { return; }
        Vector v = _playlist;
        _playlist = new Vector(size);
        while ((size = v.size()) > 0)
        {
            _playlist.addElement(v.remove((int) (Math.random() * size)));
        }
        begin();
    }

    /**
     * Moves the cursor at the top of the playlist.
     */
    public void begin()
    {
        _cursorPos = -1;
        if (getPlaylistSize() > 0)
        {
            _cursorPos = 0;
        }
        setModified(true);
    }

    /**
     * Returns item at a given position from the playlist.
     */
    public PlaylistItem getItemAt(int pos)
    {
        PlaylistItem pli = null;
        pli = (PlaylistItem) _playlist.elementAt(pos);
        return pli;
    }

    /**
     * Returns a collection of playlist items.
     */
    public Collection getAllItems()
    {
        // TODO
        return null;
    }

    /**
     * Returns then number of items in the playlist.
     */
    public int getPlaylistSize()
    {
        return _playlist.size();
    }

    // Next methods will be used by the Player
    /**
     * Returns item matching to the cursor.
     */
    public PlaylistItem getCursor()
    {
        if ((_cursorPos < 0) || (_cursorPos >= _playlist.size())) { return null; }
        return getItemAt(_cursorPos);
    }

    /**
     * Computes cursor position (next).
     */
    public void nextCursor()
    {
        _cursorPos++;
    }

    /**
     * Computes cursor position (previous).
     */
    public void previousCursor()
    {
        _cursorPos--;
        if (_cursorPos < 0)
        {
            _cursorPos = 0;
        }
    }

    public boolean setModified(boolean set)
    {
        isModified = set;
        return isModified;
    }

    public void setCursor(int index)
    {
        _cursorPos = index;
    }

    /**
     * Returns selected index.
     */
    public int getSelectedIndex()
    {
        return _cursorPos;
    }

    /**
     * Returns index of playlist item.
     */
    public int getIndex(PlaylistItem pli)
    {
        int pos = -1;
        for (int i = 0; i < _playlist.size(); i++)
        {
            pos = i;
            PlaylistItem p = (PlaylistItem) _playlist.elementAt(i);
            if (p.equals(pli)) break;
        }
        return pos;
    }

    /**
     * Get M3U home for relative playlist.
     *
     * @return
     */
    public String getM3UHome()
    {
        return M3UHome;
    }

    /**
     * Set optional M3U home for relative playlist.
     *
     * @param string
     */
    public void setM3UHome(String string)
    {
        M3UHome = string;
    }

    /**
     * Get PLS home for relative playlist.
     *
     * @return
     */
    public String getPLSHome()
    {
        return PLSHome;
    }

    /**
     * Set optional PLS home for relative playlist.
     *
     * @param string
     */
    public void setPLSHome(String string)
    {
        PLSHome = string;
    }
}

⌨️ 快捷键说明

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