C#媒体播放器播放列表

时间:2019-09-26 11:10:16

标签: c# windows-media-player

我正在尝试用C#制作媒体播放器。我想制作一个包含歌曲名称和歌曲长度的播放列表。我尝试的代码是:

private void BtnOpenFiles_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "MP3 Audio File (*.mp3)|*.mp3|Windows Media File (*.wma)|*.wma|WAV Audio File (*.wav)|*.wav";
    openFileDialog.Multiselect = true;

    if(openFileDialog.ShowDialog()==DialogResult.OK)
    {
        files = openFileDialog.SafeFileNames;
        paths = openFileDialog.FileNames;

        for (int i = 0; i < files.Length; i++)
        {
            foreach (string track in paths)
            {
                WMPLib.IWMPMedia media = this.axWindowsMediaPlayer1.newMedia(track);
                length = media.durationString;
            }

            lstPlaylist.Items.Add(Path.GetFileNameWithoutExtension(files[i] + length));
        }
    }
}

但是它显示了我所有歌曲的持续时间。

1 个答案:

答案 0 :(得分:0)

private void BtnOpenFiles_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "MP3 Audio File (*.mp3)|*.mp3|Windows Media File (*.wma)|*.wma|WAV Audio File (*.wav)|*.wav";
    openFileDialog.Multiselect = true;
    if(openFileDialog.ShowDialog() == DialogResult.OK)
    {
        paths = openFileDialog.FileNames;
        for (int i = 0; i < paths.Length; i++)
        {
            WMPLib.IWMPMedia media = this.axWindowsMediaPlayer1.newMedia(paths[i]);
            length = media.durationString;
            lstPlaylist.Items.Add(Path.GetFileNameWithoutExtension(paths[i] + length));
        }
    }
}