来看一下我的再次打开这个VBS直接结束上次的进程方案:
- On Error Resume Next
- Dim WshShell, batchFile
- Set WshShell = CreateObject("WScript.Shell")
- If WScript.Arguments.Count = 0 Then
- batchFile = WshShell.SpecialFolders("Temp") & "\tmpxvbs.cmd"
- With CreateObject("Scripting.FileSystemObject").CreateTextFile(batchFile, True)
- .WriteLine "@echo off"
- .WriteLine "taskkill /F /IM wscript.exe >nul 2>&1"
- .WriteLine "taskkill /F /IM cscript.exe >nul 2>&1"
- .WriteLine "wmic process where ""name='wscript.exe' and CommandLine like '%" & WScript.ScriptName & "%'"" delete >nul 2>&1"
- .WriteLine "start """" """ & CreateObject("Scripting.FileSystemObject").GetFile(WScript.ScriptFullName).ShortPath & """ /restarted"
- .WriteLine "timeout /t 1 /nobreak >nul"
- .WriteLine "del /f /q """ & batchFile & """"
- .Close
- End With
- WshShell.Run """" & batchFile & """", 0, False
- WScript.Quit
- End If
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set player = CreateObject("WMPlayer.OCX")
- Set shell = CreateObject("WScript.Shell")
- Dim configRegPath
- configRegPath = "HKEY_CURRENT_USER\Software\YsVbsMusicPlayer\Config"
- Dim musicFolderPath, currentSongIndex, totalSongCount
- On Error Resume Next
- musicFolderPath = shell.RegRead(configRegPath & "MusicFolderPath")
- If Err.Number <> 0 Then
- musicFolderPath = ""
- Err.Clear
- End If
- currentSongIndex = shell.RegRead(configRegPath & "CurrentSongIndex")
- If Err.Number <> 0 Then
- currentSongIndex = 0
- Err.Clear
- End If
- totalSongCount = shell.RegRead(configRegPath & "TotalSongCount")
- If Err.Number <> 0 Then
- totalSongCount = 0
- Err.Clear
- End If
- On Error Goto 0
- Do
- musicFolderPath = InputBox(" 请看下面输入框中的音乐文件夹路径"& vbCrLf &""& vbCrLf &" 是否正确?例如:D:\我的音乐"& vbCrLf &""& vbCrLf &" 如果没问题,直接点确定开始播放。", "音乐路径输入 - 轶软VBS音乐播放器V3", musicFolderPath)
- If musicFolderPath = "" Then
- WScript.Quit
- Else
- Exit Do
- End If
- Loop
- If Not fso.FolderExists(musicFolderPath) Then
- Do
- WScript.Echo "音乐文件夹路径不存在,请输入正确的音乐文件夹路径。"
- Do
- musicFolderPath = InputBox(" 您输入的音乐文件夹路径不对哦"& vbCrLf &""& vbCrLf &" 请重新检查,例如:D:\我的音乐"& vbCrLf &""& vbCrLf &" 输入好后,直接点确定开始播放。", "音乐路径输入 - 轶软VBS音乐播放器V3")
- If musicFolderPath = "" Then
- WScript.Quit
- Else
- Exit Do
- End If
- Loop
- Loop Until fso.FolderExists(musicFolderPath)
- End If
- Dim hasMP3Files
- hasMP3Files = False
- If fso.FolderExists(musicFolderPath) Then
- Dim newTotalSongCount
- newTotalSongCount = 0
- For Each file In fso.GetFolder(musicFolderPath).Files
- If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
- hasMP3Files = True
- newTotalSongCount = newTotalSongCount + 1
- End If
- Next
- If newTotalSongCount <> totalSongCount Then
- currentSongIndex = 0
- totalSongCount = newTotalSongCount
- End If
- End If
- If Not hasMP3Files Then
- Do
- WScript.Echo "指定路径下没有MP3文件,请重新输入。"
- Do
- musicFolderPath = InputBox("请输入包含MP3文件的音乐文件夹路径", "音乐路径输入")
- If musicFolderPath = "" Then
- WScript.Quit
- Else
- Exit Do
- End If
- Loop
- hasMP3Files = False
- If fso.FolderExists(musicFolderPath) Then
- Dim newTotalSongCount2
- newTotalSongCount2 = 0
- For Each file In fso.GetFolder(musicFolderPath).Files
- If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
- hasMP3Files = True
- newTotalSongCount2 = newTotalSongCount2 + 1
- End If
- Next
- If newTotalSongCount2 <> totalSongCount Then
- currentSongIndex = 0
- totalSongCount = newTotalSongCount2
- End If
- End If
- Loop Until hasMP3Files
- End If
- shell.RegWrite configRegPath & "MusicFolderPath", musicFolderPath, "REG_SZ"
- shell.RegWrite configRegPath & "TotalSongCount", totalSongCount, "REG_DWORD"
- shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
- Set musicFolder = fso.GetFolder(musicFolderPath)
- Dim songs()
- ReDim songs(-1)
- For Each file In musicFolder.Files
- If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
- ReDim Preserve songs(UBound(songs) + 1)
- songs(UBound(songs)) = fso.GetBaseName(file.Name)
- End If
- Next
- If UBound(songs) < 0 Then
- WScript.Echo "没有找到MP3文件,请检查路径。"
- WScript.Quit
- End If
- On Error Resume Next
- Do While True
- player.URL = musicFolderPath & "" & songs(currentSongIndex) & ".mp3"
- player.settings.setMode "loop", True
- Do While player.playState <> 3 And player.playState <> 1
- WScript.Sleep 100
- Loop
- If Err.Number <> 0 Then
- WScript.Echo "播放出错:" & Err.Description
- Err.Clear
- End If
- Do While player.playState = 3
- Dim userChoice
- userChoice = MsgBox("正在播放:" & (currentSongIndex + 1) & "." & songs(currentSongIndex) & vbCrLf & _
- "点【是】-> 播放【上一曲】" & vbCrLf & _
- "点【否】-> 播放【下一曲】" & vbCrLf & _
- "点【取消】-> 选择播放模式", _
- vbYesNoCancel + vbQuestion, (currentSongIndex + 1) & "." & songs(currentSongIndex) & " - " & "轶软VBS音乐播放器V3")
- If userChoice = vbYes Then
- If currentSongIndex > 0 Then
- currentSongIndex = currentSongIndex - 1
- shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
- Else
- WScript.Echo "亲,这是已经是第一首咯。"
- End If
- shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
- player.controls.stop
- Exit Do
- ElseIf userChoice = vbNo Then
- currentSongIndex = currentSongIndex + 1
- If currentSongIndex > UBound(songs) Then
- currentSongIndex = 0
- WScript.Echo "亲,这是最后一首咯,现在从头开始播放。"
- End If
- shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
- player.controls.stop
- Exit Do
- ElseIf userChoice = vbCancel Then
- Dim loopChoice
- loopChoice = MsgBox("正在播放:" & (currentSongIndex + 1) & "." & songs(currentSongIndex) & vbCrLf & _
- "点【是】-> 全曲循环播放" & vbCrLf & _
- "点【否】-> 返回主界面" & vbCrLf & _
- "点【取消】-> 退出播放器", _
- vbYesNoCancel + vbQuestion, (currentSongIndex + 1) & "." & songs(currentSongIndex) & " - " & "音乐循环模式 - 轶软VBS音乐播放器V3")
- If loopChoice = vbYes Then
- player.settings.setMode "loop", False
- Do While True
- player.URL = musicFolderPath & "" & songs(currentSongIndex) & ".mp3"
- Do While player.playState <> 3 And player.playState <> 1
- WScript.Sleep 100
- Loop
- If Err.Number <> 0 Then
- WScript.Echo "播放出错:" & Err.Description
- Err.Clear
- End If
- Do While player.playState = 3
- WScript.Sleep 100
- Loop
- currentSongIndex = currentSongIndex + 1
- If currentSongIndex > UBound(songs) Then
- currentSongIndex = 0
- End If
- shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
- Loop
- ElseIf loopChoice = vbNo Then
- player.settings.setMode "loop", True
- Exit Do
- ElseIf loopChoice = vbCancel Then
- WScript.Quit
- End If
- End If
- Do While player.playState = 3
- WScript.Sleep 100
- Loop
- Loop
- If player.playState <> 1 Then
- player.controls.stop
- End If
- Loop
- On Error Goto 0
- player.close
- Set player = Nothing
- Set fso = Nothing
- Set shell = Nothing
复制代码 |