C# - 如何在MP3播放器中保存和加载播放列表?

时间:2017-03-08 11:07:39

标签: c# winforms mp3

我试图让我的MP3播放器功能正常工作......我需要让它添加歌曲,保存当前播放列表并加载已保存的播放列表。但是我有一个问题 - 每次加载保存的播放列表时都没有任何反应,即使我不知道问题出在哪里,但这里是我的代码:

命名空间mp3_player {     公共部分类MainForm:表单     {

    public MainForm()
    {
        InitializeComponent();
    }

    List<Song> songs = new List<Song>();
    string[] files, paths;

    private void btnOpen_Click_1(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            files = openFileDialog1.SafeFileNames;
            paths = openFileDialog1.FileNames;
            for (int i = 0; i < files.Length; i++)
            {
                lbSongs.Items.Add(files[i]);
            }
            foreach (string d in open.FileNames)
            {
                lbSongs.Items.Add(d);
            }
        }
    }

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        axWindowsMediaPlayer1.URL = paths[lbSongs.SelectedIndex];
    }

    private void loadPlaylistToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (lbSongs.Items.Count != 0)
        {
            if (MessageBox.Show("Do you want to clear your songs list?", "Adding...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                lbSongs.Items.Clear();
                songs.Clear();
            }
        }

        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.Filter = "Playlist (*.wpl)|*.wpl";
        if (openDialog.ShowDialog() == DialogResult.OK)
        {
            using (StreamReader sr = new StreamReader(openDialog.FileName))
            {
                string line = sr.ReadLine();
                while (line != null)
                {
                    string path = line;
                    int index = path.LastIndexOf('\\');
                    string name = path.Substring(index + 1);

                    if (!CheckRepeatedSong(path))
                    {
                        songs.Add(new Song(name, path));
                    }
                    line = sr.ReadLine();
                }
            }
        }
        //RefreshList();
    }
    private bool CheckRepeatedSong(string path)
    {
        bool isExist = false;
        for (int i = 0; i < songs.Count; i++)
        {
            if (songs[i].FilePath == path)
            {
                isExist = true;
                break;
            }
        }
        return isExist;
    }

    private void savePlaylistToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (lbSongs.Items.Count != 0)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = ".wpl";
            saveDialog.Filter = "Playlist (*.wpl)|*.wpl";
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(saveDialog.FileName, false))
                {
                    for (int i = 0; i < songs.Count; i++)
                    {
                        sw.WriteLine(songs[i].FilePath);
                    }
                }
            }
        }
    }

    private void addSongsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        btnOpen_Click_1(sender, e);
    }

}

}

我还制作了一个课程&#39; Song&#39;:

{
    private string fileName;
    private string filePath;

    public Song() { }
    public Song(string fileName, string filePath)
    {
        this.fileName = fileName;
        this.filePath = filePath;
    }
    public string FileName
    {
        get
        {
            return this.fileName;
        }
        set
        {
            this.fileName = value;
        }
    }
    public string FilePath
    {
        get
        {
            return this.filePath;
        }
        set
        {
            this.filePath = value;
        }
    }
}

0 个答案:

没有答案