程序员 书:VB.net中如何添加声音?

来源:百度文库 编辑:高校问答 时间:2024/04/27 22:40:31
我在用VB.net做个计时器,需要在一定的时间有提示音,请问如何添加?
另外,做好的程序怎么发布啊?我只需要做成一个exe文件即可,不需要做安装程序那么麻烦。但是要求复制到别的电脑上也能用,有没有什么简单的方法啊?
我不说什么了,我只想要一个准确点的回答,不过还是要谢谢你

添加一个WindowsMediaPlayer控件放到窗体里,然后写代码:

Private Sub Command1_Click()
WindowsMediaPlayer1.URL = "123.mp3" '这句代码的意思是播放程序所在文件夹中的123.mp3

'URL后面添地址,可以是网址

Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call mciExecute("play F:\MUSIC\WMA\guitar\强调乐队-无声的叹息.mp3 ")
End Sub

复制可不怎么好吧,那些dll你都要有才行,vb.net做安装程序也不麻烦,只要添加一个新项目--安装向导,根据提示按下去就是了

哎~~~都说得很清楚了,你还......再给个提示吧,用timeofday来提取当前时间,和自定义时间对比

//c#做的,呵呵
--------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Runtime.InteropServices;

namespace nl
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.IContainer components;

[DllImport("winmm.dll")]
public static extern long PlaySound(String fileName,long a,long b);
[DllImport("winmm.dll")]
public static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("宋体", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox1.Location = new System.Drawing.Point(64, 112);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(520, 53);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "2005-7-6 10:12:32";
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(64, 200);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(520, 40);
this.label1.TabIndex = 1;
this.label1.Text = "2005-7-6 10:12:32";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(64, 272);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(520, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "textBox2";
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 304);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "OPEN";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(328, 304);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 24);
this.button2.TabIndex = 4;
this.button2.Text = "STOP";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(656, 357);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// 播放音乐文件
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public static void PlayMusic(string p_FileName,bool repeat)
{
try
{
//SoundingFileName=p_FileName;
mciSendString(@"close " +p_FileName ," ",0,0);
mciSendString(@"open " + p_FileName," ",0,0);
if(repeat)
mciSendString(@"play " + p_FileName+" repeat" ," ",0,0);
else
mciSendString(@"play " + p_FileName ," ",0,0);

}
catch
{
}

}

/// <summary>
/// 停止当前音乐播放
/// </summary>
/// <param name="p_FileName">音乐文件名称</param>
public static void StopMusic(string p_FileName)
{
try
{
//SoundingFileName="";
mciSendString(@"close " + p_FileName," ",0,0);

}
catch{}

}

private void timer1_Tick(object sender, System.EventArgs e)
{
this.label1.Text=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
if(this.label1.Text==this.textBox1.Text)
{

StopMusic(this.textBox2.Text);
PlayMusic(this.textBox2.Text,true);

}
}

private void button1_Click(object sender, System.EventArgs e)
{
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.textBox2.Text=this.openFileDialog1.FileName;
// PlayMusic(this.textBox2.Text);
}

}

private void button2_Click(object sender, System.EventArgs e)
{
StopMusic(this.textBox2.Text);
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.textBox1.Text=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}