2015-03-19 39 views
0

我是編程新手,我創建了一個簡單的MP3播放器。有一個文本框txtCount,當在該文本框中輸入4需要播放MP3 4倍..如何在C#表單應用程序中多次播放mp3文件?

這裏我的代碼它總是播放一次,fo​​r循環也不起作用。

如果我在文本框中輸入4時,在拳頭時間的MP3文件播放它顯示爲3次playd1次需要發揮這也錯了,我無法找到的代碼錯誤。請幫我解決這個問題

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading.Tasks; 

namespace mp3Player 
{ 
    class MusicPlayer 
    { 
     [DllImport("Winmm.dll")] 
     private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback); 

     public void open(string file) 
     { 
      string command = "open \"" + file + "\" type MPEGVideo alias MyMp3"; 
      mciSendString(command, null, 0, 0); 
     } 

     public void play() 
     { 
      string command = "play MyMp3"; 
      mciSendString(command, null, 0, 0); 
     } 

     public void stop() 
     { 
      string command = "stop MyMp3"; 
      mciSendString(command, null, 0, 0); 

      command = "close MyMp3"; 
      mciSendString(command, null, 0, 0); 
     } 
    } 
} 

請參見注釋爲錯誤

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 

    namespace mp3Player 
    { 
     public partial class Form1 : Form 
     { 
      int count, times = 0; 
      MusicPlayer player = new MusicPlayer(); 

      public Form1() 
      { 
       InitializeComponent(); 
      } 

      private void txtOpen_Click(object sender, EventArgs e) 
      { 
       openFileDialog1.ShowDialog(); 
       txtCount.Enabled = true; 
      } 

      private void openFileDialog1_FileOk(object sender, CancelEventArgs e) 
      { 
       label1.Text = openFileDialog1.SafeFileName; 
       player.open(openFileDialog1.FileName); 
      } 

      private void txtPlay_Click(object sender, EventArgs e) 
      {   
       if (txtCount.Text != "") 
       { 
        count = Int32.Parse(txtCount.Text); 
        txtCount.Enabled = false; 


    // This is does not work 

        for (times = 0; times < count; times++) 
        { 
         player.play(); 
         times++;      
         lblCompleted.Text = times + " times Played"; 
         lblPending.Text = (count - times) + " times need to play"; 

        } 


       } 
       else { 

        txtCount.Focus(); 
       } 

      } 

      private void txtStop_Click(object sender, EventArgs e) 
      { 
       player.stop(); 
      } 



     } 

} 
+0

嗨..我做了調試代碼和我發現的是雖然MP3播放代碼雖然循環。它不等到改變標籤,直到MP3完成播放...請幫我解決 – DSM 2015-03-19 11:28:11

+0

這是另一種問題,不一樣的是什麼你剛剛問過,所以你需要提出另一個問題的答案,但在你提出另一個問題之前,請閱讀C#中的線程問題# – 2015-03-19 11:30:05

回答

0

從循環中刪除times++

for (times = 0; times < count; times++) 
       { 
        player.play(); 
        //times++; //remove this line 
        //At the end of loop 
        //lblCompleted.Text = 3 
        //lblPending.Text = 1 
        lblCompleted.Text = times + " times Played"; 
        lblPending.Text = (count - times) + " times need to play"; 

       } 

lblCompleted.TextlblPending.Text顯示最新更新的值。

編輯: 可以使用background worker用於播放音樂,並在那個時候GUI將等待更新,直到音樂結束。

+0

嗨..我確實調試過代碼,而我發現的是在mp3玩循環播放代碼。它不等待改變標籤,直到MP3完成播放...請幫我解決這個問題 – DSM 2015-03-19 11:28:05

+0

查看編輯部分 – vikky 2015-03-19 11:32:49

0

for循環以0開頭,但播放後但在更新標籤之前,times變量增加1。 (你不需要增加可變倍++在for循環內(除非你想讓它)

我想你應該改變你的代碼,這樣的事情:

for (times = 1; times <= count; times++) 
      { 
       player.play();     
       lblCompleted.Text = times + " times Played"; 
       lblPending.Text = (count - times) + " times need to play"; 

      } 
+0

嗨..我沒有調試代碼,我發現的是當MP3播放代碼通過循環。它不等待改變標籤,直到MP3完成播放...請幫我解決 – DSM 2015-03-19 11:30:16

+0

vikky建議使用應該解決您的問題的背景工作(播放是在後臺完成..) – chroni 2015-03-19 12:00:45

相關問題