小弟是菜鸟,最近在学习C#,想写一个定时播放声音的小东西。定时使用的System.Timers.Timer,播放用的winmm.dll,然后发现,定时时能做弹出对话框之类的操作,也能通过单击按钮来播放MP3,但是就是不能定时来执行播放函数。下面是源代码,哪位兄台帮忙看下,分数送上。感激不尽。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;namespace Timer_Music
{
public partial class beep : Form
{
private bool done = false;
string nowTimeText = "";
System.Timers.Timer t;
clsMCI mp = new clsMCI();
public beep()
{
InitializeComponent();
} private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
}
} private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
} private void beep_Load(object sender, EventArgs e)
{
HourText.Text = DateTime.Now.Hour < 10 ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
MinuteText.Text = DateTime.Now.Minute < 10 ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
SecondText.Text = DateTime.Now.Second < 10 ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
}
//开始定时
private void button1_Click(object sender, EventArgs e)
{
nowTimeText = HourText.Text + ":" + MinuteText.Text + ":" + SecondText.Text;
button1.Enabled = false;
StartTimer();
//mp.play(); 这里能直接播放
} //开始侦听
private void StartTimer()
{
try
{
t = new System.Timers.Timer(1000);
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(PlayMusic);
t.Enabled = true;
}
catch
{
MessageBox.Show("Error!");
}
}
//播放
public void PlayMusic(object source, System.Timers.ElapsedEventArgs e)
{
if (nowTimeText == DateTime.Now.ToLongTimeString() && done == false)
{
try
{
//MessageBox.Show(DateTime.Now.ToLongTimeString() + nowTimeText + "is now");
mp.play();
}
catch
{
MessageBox.Show("Error!");
}
}
}
//结束
private void StopTimer()
{
done = true;
button1.Enabled = true;
} private void button2_Click(object sender, EventArgs e)
{
StopTimer();
} private void beep_FormClosing(object sender, FormClosingEventArgs e)
{
t.Dispose();
} private void button3_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "*.mp3|*.mp3";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
mp.FileName = openFileDialog1.FileName;
PlayFileName.Text = openFileDialog1.FileName;
}
}
catch
{
MessageBox.Show("出错错误!");
}
}
}
public class clsMCI
{
public clsMCI()
{
//
// TODO: 在此处添加构造函数逻辑
//
} //定义API函数使用的字符串变量
//[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string Name = "";
//[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
//private string durLength = "";
//[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
}; public structMCI mc = new structMCI(); //取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
Name = Name.PadLeft(260, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
mc.state = State.mStop;
}
catch
{
MessageBox.Show("出错错误!");
}
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
mc.state = State.mPlaying;
} //获得当前路径
private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}
} public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;namespace Timer_Music
{
public partial class beep : Form
{
private bool done = false;
string nowTimeText = "";
System.Timers.Timer t;
clsMCI mp = new clsMCI();
public beep()
{
InitializeComponent();
} private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible = true;
}
} private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
} private void beep_Load(object sender, EventArgs e)
{
HourText.Text = DateTime.Now.Hour < 10 ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
MinuteText.Text = DateTime.Now.Minute < 10 ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
SecondText.Text = DateTime.Now.Second < 10 ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
}
//开始定时
private void button1_Click(object sender, EventArgs e)
{
nowTimeText = HourText.Text + ":" + MinuteText.Text + ":" + SecondText.Text;
button1.Enabled = false;
StartTimer();
//mp.play(); 这里能直接播放
} //开始侦听
private void StartTimer()
{
try
{
t = new System.Timers.Timer(1000);
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(PlayMusic);
t.Enabled = true;
}
catch
{
MessageBox.Show("Error!");
}
}
//播放
public void PlayMusic(object source, System.Timers.ElapsedEventArgs e)
{
if (nowTimeText == DateTime.Now.ToLongTimeString() && done == false)
{
try
{
//MessageBox.Show(DateTime.Now.ToLongTimeString() + nowTimeText + "is now");
mp.play();
}
catch
{
MessageBox.Show("Error!");
}
}
}
//结束
private void StopTimer()
{
done = true;
button1.Enabled = true;
} private void button2_Click(object sender, EventArgs e)
{
StopTimer();
} private void beep_FormClosing(object sender, FormClosingEventArgs e)
{
t.Dispose();
} private void button3_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "*.mp3|*.mp3";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
mp.FileName = openFileDialog1.FileName;
PlayFileName.Text = openFileDialog1.FileName;
}
}
catch
{
MessageBox.Show("出错错误!");
}
}
}
public class clsMCI
{
public clsMCI()
{
//
// TODO: 在此处添加构造函数逻辑
//
} //定义API函数使用的字符串变量
//[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string Name = "";
//[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
//private string durLength = "";
//[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
}; public structMCI mc = new structMCI(); //取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
Name = Name.PadLeft(260, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
mc.state = State.mStop;
}
catch
{
MessageBox.Show("出错错误!");
}
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
mc.state = State.mPlaying;
} //获得当前路径
private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}
} public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}
解决方案 »
- ReSharper汉化版,ReSharper汉化帮助手册
- 求助一个表关联问题
- C# winform treeview Tag属性怎么用
- asp.net打包问题
- .net能否同时进行c/s,b/s的开发
- sqlDataAdapter1.TableMappings.AddRange ..... 这句是起什么作用的啊?
- 怎么在两个客户端之间传文件?
- Javascript 改变DropDownList的选中状态后,后台不能获取新的选中值
- 在c# 中怎样在播放的视频中叠加文字与图像?
- 辞旧迎新,散掉所有分(208分,分两贴),但愿赶走今年的晦气,明年会更好!!!顺便问一下。。
- WinFrom GridView点击单元格时,怎么获取这个单元格是哪一行,哪一列?
- 会处理GIF的大虾冲啊 小弟崩溃掉。。。
加分求解啊。哪位大侠能帮帮忙?
查下这个条件if (nowTimeText == DateTime.Now.ToLongTimeString() && done == false)单步调试一下,看看nowTimeText的值是什么
方法中设个断点.看看到底是执行不执行
timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);private void timer1_Tick(object sender, EventArgs e)
{
//....
}
TO 6 楼的兄台:在PlayMusic中我试着直接使用MessageBox.Show(),都能秀出对话框来,说明是能执行PlayMusic的。
TO 7 楼的兄台:三个Timer我都试过了,但是都不能播放。
TO 8、9 楼的兄台:我看过System.Thread.Timer的参数,就是在使用这个Timer的时候使用MessageBox.Show发现秀出来的BOX很不准时。我个人感觉可能是下面的播放的类有一些问题,那个是在网上抄的一段代码,现在想求哪位大侠能看看这个类是不是有问题。或者说能不能给我一个更好的播放MP3的办法。
再次拜谢.
你不妨把那个判断条件去掉试试
if (nowTimeText == DateTime.Now.ToLongTimeString() && done == false)明白你这个时间在这里的意义,不过,这样写并不好,建议用一个bool变量来表示。bool playNow=false;
private void button1_Click(object sender, EventArgs e)
{
nowTimeText = HourText.Text + ":" + MinuteText.Text + ":" + SecondText.Text;
button1.Enabled = false;
playNow=true;//加上这句。
StartTimer();
//mp.play(); 这里能直接播放
} //播放
public void PlayMusic(object source, System.Timers.ElapsedEventArgs e)
{
if (playNow&& !done)
{
playNow=false;
try
{
//MessageBox.Show(DateTime.Now.ToLongTimeString() + nowTimeText + "is now");
mp.play();
}
catch
{
MessageBox.Show("Error!");
}
}
}
不好意思,搞错了。。
//MessageBox.Show(DateTime.Now.ToLongTimeString() + nowTimeText + "is now");
这个能出来的话,那你得跟一跟Play()方法,看有没有执行到。。
private void timer1_Tick(object sender, EventArgs e)
{
//在这里开始播放
}
谢谢Min_jie的热心回答,我跟下看看play()行不行。哪位大侠之前做过这个的,能不能给个范例?
http://blog.csdn.net/lfywy/archive/2008/07/16/2658623.aspx
if (nowTimeText == DateTime.Now.ToLongTimeString() && done == false)
变成
if (nowTimeText <= DateTime.Now.ToLongTimeString() && done == false)
时间和double类似的,不要用“==”,要用误差判断或者不等量判断,比如“>= ”、“<=”,Timer事件本身的时间不是精确的,你用等量精确判断,出错是经常的
单做个在timer中设置定时播放音乐的,那样看着不是更明了,或单独写到一个类中,用的时候调用
你这样的集结代码很难排错的
判断时间还可以用类似绝对值的方法比如要判断的2个时间dt1,dt2,那么(long)((TimeSpan)(dt1- dt2)).TotalSeconds<3,表示dt1和dt2相差不超过3s,但是具体处理时间判断得问题应该还是用一个时间大于或者小于,然后更新其中一个参加判断的时间的方式更常用。
主要是TIMER的问题,使用FORM的TIMER问题解决了.
核心问题是使用System.Timer.Timer的时候要求使用参数,而把参数给了PLAYMUSIC后就后导致执行不成功.