求用VB做一个到时间自动播放音乐的代码

2025-05-14 14:02:08
推荐回答(4个)
回答(1):

我先简单的说一下,看看对你有没有帮助:
我建议吧你要播放的音乐,路径名记住,然后用代码调用路径运行这个音乐程序,不过这样即使成功了,也不太方便,太浪费资源!
有些书上写得可以通过basic语言来制作一首音乐,不过那是最后几页的,Quick BASIC书上看的,你在网上查查应该有方法,不建议你现在做,因为很不容易读懂,(不过确实是一种好办法!)或者你已经对basic非常熟悉了才行!
还有,我的VB书的最后几页有>>其他高级应用>>ActiveX控件>>>这一小节,里面介绍可以调用电脑里面的Media Player,从而达到发生目的,你可以看看书本最后几页,里面仔细找找应该有!
下面书上抄的代码,仅供参考:

'开始播放
Private Sub cmdStart_Click()
MediaPlayer1.play
End Sub
'停止播放
Private Sub cmdStop_Click()
MediaPlayer1.Stop
End Sub

回答(2):

Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Private Sub Form_Load()

WindowsMediaPlayer1.Visible = False
appdisk = Trim(App.Path)
If Right(appdisk, 1) <> "\" Then appdisk = appdisk & "\"
aa = "你的歌曲名.mp3"
If InStr(aa, " ") > 0 Then '判断歌曲名是否有空格,有的话就去掉,并拷备为没空格的新歌曲名(在同一路径下)
songname = Replace(aa, " ", "")
FileCopy appdisk & aa, appdisk & songname
Else
songname = aa
End If

If InStr(appdisk, " ") > 0 Then '判断路径是否有空格
FileCopy appdisk & songname, Left(appdisk, 3) & songname '将歌曲拷到windows系统盘符下.
songname = Left(appdisk, 3) & songname
Else
songname = appdisk & songname
End If
mciSendString "play " & songname & " repeat", vbNullString, 0, 0 '歌曲循环播放,去除 & " repeat" 则取消循环
end sub

调用你自己的倒计时代码 就可以了

回答(3):

Option Explicit
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, _
ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, _
ByVal hRgn As Long, ByVal bRedraw As Long) As Long

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long
Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function mciSendCommand Lib "winmm.dll" Alias "mciSendCommandA" (ByVal wDeviceID As Long, ByVal uMessage As Long, ByVal dwParam1 As Long, ByVal dwParam2 As Any) As Long
Private Declare Function MessageBeep Lib "user32" (ByVal wType As Long) As Long
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Public PlayError As Boolean '是否错误
Private PLAYMusicFileName As Integer '播放那一首音乐
Public Function TestSound() As Boolean '测试是否安装了声卡
Dim Ret As Long
Ret& = waveOutGetNumDevs
If Ret > 0 Then
TestSound = True
Else
TestSound = False 'TestSound = False
End If
End Function
Public Sub PlayMusic(FileName As String) '播放音乐mp3,wav,mid等
Dim Buffer As String * 128
Dim Ret As Long
Dim PlayStatus As String * 20
Dim ShortFileName As String
mciExecute "close all"
If Dir(FileName) = "" Then PlayError = True: Exit Sub
ShortFileName = ShortName(FileName)
mciSendString "open " & ShortFileName & " alias mp3", Buffer, Ret, 0
mciSendString "play mp3", Buffer, Ret, 0
PlayError = False
End Sub

Public Sub StopMusic() '停止播放
Dim Buffer As String * 128
Dim Ret As Long
mciSendString "stop mp3", Buffer, Ret, 0
End Sub

Public Function GetPlayMode() As String '得到播放状态
Dim Buffer As String * 128
Dim pos As Integer
mciSendString "status mp3 mode", Buffer, 128, 0&
pos = InStr(Buffer, Chr(0))
GetPlayMode = Left(Buffer, pos - 1)
End Function

Function ShortName(LongPath As String) As String '得到文件短文件名
Dim ShortPath As String
Dim pos As String
Dim Ret As Long
Const MAX_PATH = 260
If LongPath = "" Then Exit Function
ShortPath = Space$(MAX_PATH)
Ret& = GetShortPathName(LongPath, ShortPath, MAX_PATH)
If Ret& Then
pos = InStr(1, ShortPath, " ")
ShortName = Left$(ShortPath, pos - 2)
End If
End Function

Function ParseFileName(sFileIn As String) As String '此函数从字符串中分离出文件名
Dim i As Integer
For i = Len(sFileIn) To 1 Step -1
If InStr("\", Mid$(sFileIn, i, 1)) Then Exit For
Next
ParseFileName = Mid$(sFileIn, i + 1, Len(sFileIn) - i)
End Function

Private Sub Command1_Click() '播放音乐
PlayMusic ("C:\我爱你.mp3") '播放音乐mp3,wav,mid等
End Sub
Private Sub Command2_Click() '停止播放
StopMusic
End Sub

Private Sub Timer1_Timer() '播放完后重新播放
If GetPlayMode = "stopped" Then '停止播放后做什么
PlayMusic ("C:\我爱你.mp3") '播放音乐mp3,wav,mid等
End If
End Sub

回答(4):

你可以添加一个windows media player控件,设置当时间到的时候就播放音乐就可以了。
具体的我也有点不记得了。不好意思啊、