簡體   English   中英

如何在C#格式應用程序中多次播放mp3文件?

[英]How to play mp3 file several times in C# form Application?

我是編程新手,我創建了一個簡單的mp3播放器。 有一個文本框txtCount ,在該文本框中輸入4時需要播放mp3 4次。

在這里,我的代碼總是播放一次,fo​​r循環也無法正常工作。

如果在第一時間播放mp3文件時在文本框中輸入4,則顯示為播放3次 ,還需要播放1次,這也是錯誤的,我找不到代碼錯誤。 請幫我解決問題

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();
            }



        }

}

從循環內部刪除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顯示最新的更新值。

編輯:您可以使用后台工作程序來播放音樂,然后GUI將等待更新直到音樂結束。

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";

            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM