重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在.NET里,没有托管的音乐播放器,用API只能播放WAV格式,对于MP3等形式的音频文件,就要依赖于其他控件了,常用的就是
创新互联建站专注于企业成都营销网站建设、网站重做改版、金乡网站定制设计、自适应品牌网站建设、成都h5网站建设、电子商务商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为金乡等各大城市提供网站开发制作服务。
MediaPlayer。使用方法:
在工具箱上点右键,选择“选择项目(Choose
Items)”,切到COM选项卡,找到
Windows
Media
Player,
勾选,确定
在t工具箱上,把刚才加入的MediaPlayer控件,拖放到窗体上
代码:
WindowsMediaPlayer1.URL
=
"mp3文件路径,支持网络路径"
WindowsMediaPlayer1.Ctlcontrols.play
基本就OK了,至于其他的操作诸如下一曲上一曲暂停停止,直接看
WindowsMediaPlayer1.Ctlcontrols.
里面的方法吧。
先定义api函数
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrRetumString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Sub mnu_Music_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_Music.Click
mciSendString("open c:\1.mp3 alias mp3", Nothing, 0, IntPtr.Zero)
mciSendString("Play mp3", Nothing, 0, IntPtr.Zero)
End Sub
上面2行代码就可以播放了,测试通过。
就是没有其它功能,自己加上吧,
如:关闭 mciSendString("close mp3",Nothing,0,intPtr.Zero)
等等
添加一个timer控件,隔一秒(Interval = 1000)对照一次本地时间和指定的时间,如果相同就执行铃声,并停止对照。 或者启动时取得本地时间,没一秒把这时间加1秒,直到加到指定的时间执行铃声,并停止加。 播放mp3的代码 System.Runtime.InteropServices.DllImport("winmm.dll", _ CharSet:=System.Runtime.InteropServices.CharSet.Auto) _Private Shared Function mciSendString(ByVal command As String, _ ByVal buffer As System.Text.StringBuilder, _ ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As IntegerEnd FunctionPrivate aliasName As String = "MediaFile"'点击Button1(变成到指定时间就可以Private Sub Button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles Button1.Click '要播放的文件比如在C:\music.mp3 Dim fileName As String = "C:\music.mp3" Dim cmd As String '打开文件代码 cmd = "open """ + fileName + """ type mpegvideo alias " + aliasName If mciSendString(cmd, Nothing, 0, IntPtr.Zero) 0 Then Return End If '播放 cmd = "play " + aliasName mciSendString(cmd, Nothing, 0, IntPtr.Zero)End Sub'点击Button2停止播放(根据你自己的情况,比如建一个停止的按钮Private Sub Button2_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim cmd As String '播放中音乐停止 cmd = "stop " + aliasName mciSendString(cmd, Nothing, 0, IntPtr.Zero) '关闭 cmd = "close " + aliasName mciSendString(cmd, Nothing, 0, IntPtr.Zero)End Sub
方法一:调用系统DLL(这个有点不完善,也就是播放声音时窗体无法控制,效果相当于Enabled=true,但音频播放完毕后就没事了)
Private Declare Function sndPlaySound Lib "Winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SoundFile As String, Result As Long
SoundFile = "D:\KuGou\Pianoboy - 第105天.wav" '此处为路径
Result = sndPlaySound(SoundFile, 0)
End Sub
方法二:使用WindowsMediaPlayer(WindowsMediaPlayer1的可见为False)
先添加一个windowsmediaplayer控件(工具箱里没有,需要手动添加)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WindowsMediaPlayer1.visible=False
Me.WindowsMediaPlayer1.URL = "D:\KuGou\Pianoboy - 第105天.mp3"
End Sub
上面的都是点击button1(按钮1)后播放声音,若是鼠标移动到某控件上发出声音的话改一下触发事件就OK了。