2017-04-12 97 views
0

我正在使用WMPLib在c#winforms中構建MP3播放器。我有允許用戶將MP3文件上傳到listbox1的代碼。我希望用戶能夠隨機播放listbox1中的項目,以便隨機播放項目(MP3)。目前,我有代碼混洗listbox1中的項目,但初始索引中的項目播放。我希望這是有道理的。c#WMPLib隨機播放列表框中的MP3文件

這是我的代碼,供用戶上傳他們的文件。

private void uploadSongs() 
    { 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
     openFileDialog1.Multiselect = true; 

     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      files = openFileDialog1.SafeFileNames; 
      paths = openFileDialog1.FileNames; 
      for (int i = 0; i < files.Length; i++) 
      { 
       filelist.Items.Add(files[i]); 

      } 

     } 

    } 

這裏是洗牌爲ListBox項目代碼:

private void shuffleBttn_Click(object sender, EventArgs e) 
    { 

     ListBox.ObjectCollection list = fielist.Items; 
     Random random = new Random(); 
     int w = list.Count; 
     filelist.BeginUpdate(); 

     while (w > 1) 
     { 
      w--; 
      int u = random.Next(w + 1); 
      object value = list[u]; 
      list[u] = list[w]; 
      list[w] = value; 
     } 

     filelist.EndUpdate(); 
     filelist.Invalidate(); 

    } 

我加入以下代碼播放列表到shuffleBttn_Click

WMPLib.IWMPPlaylist playlist = WMPPlayer.playlistCollection.newPlaylist("myplaylist"); 
     WMPLib.IWMPMedia media; 
     foreach (object item in filelist.Items) 
     { 
      media = WMPPlayer.newMedia((string)item); 
      playlist.appendItem(media); 
     } 
     WMPPlayer.currentPlaylist = playlist; 
     WMPPlayer.Ctlcontrols.play(); 
+0

注:我使用的 「文件列表」,而不是ListBox1中。 – gsanchez

回答

0

在VS調試器中進入你的代碼會發現listBox1.SelectedIndex的值在while循環中不變。所以你最終多次使用列表中的相同項目設置WMPPlayer.URL

考慮創建一個播放列表。

... 
//remove these 2 lines that cause the problem 
//value = listBox1.SelectedIndex; 
//WMPPlayer.URL = filepaths[listBox1.SelectedIndex]; 
... 
listBox1.EndUpdate(); 
listBox1.Invalidate(); 

//after shuffling the items in listbox, create a playlist from the listbox 

WMPLib.IWMPPlaylist playlist = WMPPlayer.playlistCollection.newPlaylist("myplaylist"); 
WMPLib.IWMPMedia media; 
foreach (object item in listBox1.Items) 
{ 
    media = WMPPlayer.newMedia((string)item); 
    playlist.appendItem(media); 
}  
WMPPlayer.currentPlaylist = playlist; 
WMPPlayer.Ctlcontrols.play(); 

編輯:完整的代碼,我的設備上工作

private void uploadSongs(object sender, EventArgs e) 
    { 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
     openFileDialog1.Multiselect = true; 

     if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      var files = openFileDialog1.FileNames; //I also changed this line 
      for (int i = 0; i < files.Length; i++) 
      { 
       filelist.Items.Add(files[i]); 
      } 
     } 
    } 

    private void shuffleBttn_Click(object sender, EventArgs e) 
    { 
     ListBox.ObjectCollection list = filelist.Items; 
     Random random = new Random(); 
     int w = list.Count; 
     filelist.BeginUpdate(); 

     while (w > 1) 
     { 
      w--; 
      int u = random.Next(w + 1); 
      object value = list[u]; 
      list[u] = list[w]; 
      list[w] = value; 
     } 

     filelist.EndUpdate(); 
     filelist.Invalidate(); 

     WMPLib.IWMPPlaylist playlist = WMPPlayer.playlistCollection.newPlaylist("myplaylist"); 
     WMPLib.IWMPMedia media; 
     foreach (object item in filelist.Items) 
     { 
      media = WMPPlayer.newMedia((string)item); 
      playlist.appendItem(media); 
     } 
     WMPPlayer.currentPlaylist = playlist; 
     WMPPlayer.Ctlcontrols.play(); 
    } 
+0

我已經實現了上面的代碼,但我仍然遇到同樣的問題。原始索引中的歌曲仍在播放。 @kennyzx – gsanchez

+0

shuffle_button_click現在的代碼是什麼?你可以發佈嗎? – kennyzx

+0

我已將推薦的更改發佈到shuffleBttn_Click @kennyzx – gsanchez