我是編程新手,我創建了一個簡單的MP3播放器。有一個文本框txtCount,當在該文本框中輸入4需要播放MP3 4倍..如何在C#表單應用程序中多次播放mp3文件?
這裏我的代碼它總是播放一次,for循環也不起作用。
如果我在文本框中輸入4時,在拳頭時間的MP3文件播放它顯示爲3次playd和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();
}
}
}
嗨..我做了調試代碼和我發現的是雖然MP3播放代碼雖然循環。它不等到改變標籤,直到MP3完成播放...請幫我解決 – DSM 2015-03-19 11:28:11
這是另一種問題,不一樣的是什麼你剛剛問過,所以你需要提出另一個問題的答案,但在你提出另一個問題之前,請閱讀C#中的線程問題# – 2015-03-19 11:30:05