找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 12260|回复: 11

[问题求助] 求助VBS播放器:全曲循环播放不能显示界面的问题

[复制链接]
发表于 2025-3-6 13:36:35 | 显示全部楼层 |阅读模式
下面是一个VBS做的播放器,点了取消进入播放模式选择后,选是进入全曲循环就会在后台播放,不能显示循环播放模式的界面了,有大佬能给改一下吗?谢谢了
  1. Set fso = CreateObject("Scripting.FileSystemObject")
  2. Set player = CreateObject("WMPlayer.OCX")
  3. Set shell = CreateObject("WScript.Shell")
  4. Dim configRegPath
  5. configRegPath = "HKEY_CURRENT_USER\Software\YsVbsMusicPlayer\Config"
  6. Dim musicFolderPath, currentSongIndex, totalSongCount
  7. On Error Resume Next
  8. musicFolderPath = shell.RegRead(configRegPath & "MusicFolderPath")
  9. If Err.Number <> 0 Then
  10.     musicFolderPath = ""
  11.     Err.Clear
  12. End If

  13. currentSongIndex = shell.RegRead(configRegPath & "CurrentSongIndex")
  14. If Err.Number <> 0 Then
  15.     currentSongIndex = 0
  16.     Err.Clear
  17. End If

  18. totalSongCount = shell.RegRead(configRegPath & "TotalSongCount")
  19. If Err.Number <> 0 Then
  20.     totalSongCount = 0
  21.     Err.Clear
  22. End If
  23. On Error Goto 0
  24. Do
  25.     musicFolderPath = InputBox(" 请看下面输入框中的音乐文件夹路径"& vbCrLf &""& vbCrLf &" 是否正确?如需更换,请输入新路径"& vbCrLf &""& vbCrLf &" 如果没问题,直接点确定开始播放。", "音乐路径输入", musicFolderPath)
  26.     If musicFolderPath = "" Then
  27.         WScript.Quit
  28.     Else
  29.         Exit Do
  30.     End If
  31. Loop

  32. If Not fso.FolderExists(musicFolderPath) Then
  33.     Do
  34.         WScript.Echo "音乐文件夹路径不存在,请输入正确的音乐文件夹路径。"
  35.         Do
  36.             musicFolderPath = InputBox("请输入有效的MP3音乐文件夹路径", "音乐路径输入")
  37.             If musicFolderPath = "" Then
  38.                 WScript.Quit
  39.             Else
  40.                 Exit Do
  41.             End If
  42.         Loop
  43.     Loop Until fso.FolderExists(musicFolderPath)
  44. End If
  45. Dim hasMP3Files
  46. hasMP3Files = False
  47. If fso.FolderExists(musicFolderPath) Then
  48.     Dim newTotalSongCount
  49.     newTotalSongCount = 0
  50.     For Each file In fso.GetFolder(musicFolderPath).Files
  51.         If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
  52.             hasMP3Files = True
  53.             newTotalSongCount = newTotalSongCount + 1
  54.         End If
  55.     Next
  56.     If newTotalSongCount <> totalSongCount Then
  57.         currentSongIndex = 0
  58.         totalSongCount = newTotalSongCount
  59.     End If
  60. End If
  61. If Not hasMP3Files Then
  62.     Do
  63.         WScript.Echo "指定路径下没有MP3文件,请重新输入。"
  64.         Do
  65.             musicFolderPath = InputBox("请输入包含MP3文件的音乐文件夹路径", "音乐路径输入")
  66.             If musicFolderPath = "" Then
  67.                 WScript.Quit
  68.             Else
  69.                 Exit Do
  70.             End If
  71.         Loop
  72.         hasMP3Files = False
  73.         If fso.FolderExists(musicFolderPath) Then
  74.             Dim newTotalSongCount2
  75.             newTotalSongCount2 = 0
  76.             For Each file In fso.GetFolder(musicFolderPath).Files
  77.                 If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
  78.                     hasMP3Files = True
  79.                     newTotalSongCount2 = newTotalSongCount2 + 1
  80.                 End If
  81.             Next
  82.             If newTotalSongCount2 <> totalSongCount Then
  83.                 currentSongIndex = 0
  84.                 totalSongCount = newTotalSongCount2
  85.             End If
  86.         End If
  87.     Loop Until hasMP3Files
  88. End If
  89. shell.RegWrite configRegPath & "MusicFolderPath", musicFolderPath, "REG_SZ"
  90. shell.RegWrite configRegPath & "TotalSongCount", totalSongCount, "REG_DWORD"
  91. shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
  92. Set musicFolder = fso.GetFolder(musicFolderPath)
  93. Dim songs()
  94. ReDim songs(-1)
  95. For Each file In musicFolder.Files
  96.     If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
  97.         ReDim Preserve songs(UBound(songs) + 1)
  98.         songs(UBound(songs)) = fso.GetBaseName(file.Name)
  99.     End If
  100. Next
  101. If UBound(songs) < 0 Then
  102.     WScript.Echo "没有找到MP3文件,请检查路径。"
  103.     WScript.Quit
  104. End If
  105. On Error Resume Next
  106. Do While True
  107.     player.URL = musicFolderPath & "" & songs(currentSongIndex) & ".mp3"
  108.     player.settings.setMode "loop", True

  109.     Do While player.playState <> 3 And player.playState <> 1
  110.         WScript.Sleep 100
  111.     Loop
  112.     If Err.Number <> 0 Then
  113.         WScript.Echo "播放出错:" & Err.Description
  114.         Err.Clear
  115.     End If

  116.     Do While player.playState = 3
  117.         Dim userChoice
  118.         userChoice = MsgBox("正在播放:" & (currentSongIndex + 1) & "." & songs(currentSongIndex) & vbCrLf & _
  119.                             "点【是】-> 播放【上一曲】" & vbCrLf & _
  120.                             "点【否】-> 播放【下一曲】" & vbCrLf & _
  121.                             "点【取消】-> 选择播放模式", _
  122.                             vbYesNoCancel + vbQuestion, (currentSongIndex + 1) & "." & songs(currentSongIndex) & " - " & "VBS音乐播放器V3")
  123.         If userChoice = vbYes Then
  124.             If currentSongIndex > 0 Then
  125.                 currentSongIndex = currentSongIndex - 1
  126.             Else
  127.                 WScript.Echo "亲,这是已经是第一首咯。"
  128.             End If
  129.             player.controls.stop
  130.             Exit Do
  131.         ElseIf userChoice = vbNo Then
  132.             currentSongIndex = currentSongIndex + 1
  133.             If currentSongIndex > UBound(songs) Then
  134.                 currentSongIndex = 0
  135.                 WScript.Echo "亲,这是最后一首咯,现在从头开始播放。"
  136.             End If
  137.             player.controls.stop
  138.             Exit Do
  139.         ElseIf userChoice = vbCancel Then
  140.             Dim loopChoice
  141.             loopChoice = MsgBox("正在播放:" & (currentSongIndex + 1) & "." & songs(currentSongIndex) & vbCrLf & _
  142.                                 "点【是】-> 全曲循环播放" & vbCrLf & _
  143.                                 "点【否】-> 返回主界面" & vbCrLf & _
  144.                                 "点【取消】-> 退出播放器", _
  145.                         vbYesNoCancel + vbQuestion, "音乐循环模式")
  146.             If loopChoice = vbYes Then
  147.                 player.settings.setMode "loop", False
  148.                 Do While True
  149.                     player.URL = musicFolderPath & "" & songs(currentSongIndex) & ".mp3"
  150.                     Do While player.playState <> 3 And player.playState <> 1
  151.                         WScript.Sleep 100
  152.                     Loop
  153.                     If Err.Number <> 0 Then
  154.                         WScript.Echo "播放出错:" & Err.Description
  155.                         Err.Clear
  156.                     End If
  157.                     Do While player.playState = 3
  158.                         WScript.Sleep 100
  159.                     Loop
  160.                     currentSongIndex = currentSongIndex + 1
  161.                     If currentSongIndex > UBound(songs) Then
  162.                         currentSongIndex = 0
  163.                     End If
  164.                     shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
  165.                 Loop
  166.             ElseIf loopChoice = vbNo Then
  167.                 player.settings.setMode "loop", True
  168.                 Exit Do
  169.             ElseIf loopChoice = vbCancel Then
  170.                 WScript.Quit
  171.             End If
  172.         End If
  173.         Do While player.playState = 3
  174.             WScript.Sleep 100
  175.         Loop
  176.     Loop
  177.     If player.playState <> 1 Then
  178.         player.controls.stop
  179.     End If
  180. Loop
  181. On Error Goto 0
  182. player.close
  183. Set player = Nothing
  184. Set fso = Nothing
  185. Set shell = Nothing
复制代码
以上就是完整代码了,急用拜托。
 楼主| 发表于 2025-3-6 13:39:38 | 显示全部楼层
能给优化一下就更好了,比我我想的是,循环播放完一首后,自动关闭,弹出界面播放第二首,这样界面就能同步显示歌曲名称了,如果不能实现,就在循环播放时弹出显示操作界面吧,现在必须得到进程去关闭才行,有点麻烦啊
发表于 2025-3-6 23:20:09 | 显示全部楼层
本帖最后由 jyswjjgdwtdtj 于 2025-3-6 23:24 编辑

vbs是单线程的 或许可以试试htmlfile的settimeout函数 用回调来弹出一个对话框 不过最方便的还是用cscript打开 想结束进程直接把控制台窗口关了就行使控制台窗口显示或者不显示可以用wshshell的exec方法运行powershell.exe -windowstyle hidden或者normal来调整 当然以上都不美观 想要自用到是够了
同时你的代码似乎有点臃肿诶

  1. Do
  2.             musicFolderPath = InputBox("请输入包含MP3文件的音乐文件夹路径", "音乐路径输入")
  3.             If musicFolderPath = "" Then
  4.                 WScript.Quit
  5.             Else
  6.                 Exit Do
  7.             End If
  8.         Loop
复制代码
套个循环是何意?
 楼主| 发表于 2025-3-7 01:17:51 | 显示全部楼层
回复 3# jyswjjgdwtdtj


    你好,如果方便的话,您能帮我改一下吗?deepseek写的还是欠佳啊,do循环那个还有循环播放不显示,我也不知道怎么改了,您能给个完整的可以运行的吗,谢谢了
发表于 2025-3-7 23:16:59 | 显示全部楼层
回复 4# yiyue7


   用vbs实现交互实在不是一个容易的事 考虑用hta做个小应用?
 楼主| 发表于 2025-3-8 09:14:12 | 显示全部楼层
回复 5# jyswjjgdwtdtj


    算了,不整了,就这么用吧,hta界面和控制显示有的会有问题,兼容性问题,如果彻底还不如py呢
发表于 2025-3-8 11:13:33 | 显示全部楼层
本帖最后由 aloha20200628 于 2025-3-8 11:29 编辑

回复 6# yiyue7

一楼代码用vbs播放mp3,是在调用系统内置的wmplayer插件,为何楼主不直接用系统内置的wmplayer播放指定的mp3播放列表文件呢?例如以下代码(存为 test.bat 运行),代码中的d3变量(即mp3文件所在目录)以及wmplayer.exe的路径均由楼主自定义...

  1. @echo off &set "d3=d:\mp3"
  2. dir /b/s/a-d "%d3%\*.mp3">"mp3播放列表.m3u"
  3. start "" "C:\Program Files\Windows Media Player\wmplayer.exe" "%cd%\mp3播放列表.m3u"
  4. exit/b
复制代码
 楼主| 发表于 2025-3-10 08:50:56 | 显示全部楼层
回复 7# aloha20200628


    你好,只是单纯的听歌玩而已,我做的这种脚本肯定没有播放器软件功能齐全的,纯属娱乐,感谢你的回复。
发表于 2025-3-10 22:47:27 | 显示全部楼层
回复 8# yiyue7


   自制播放器可以考虑html5 像chrome可以吧网页打包为本地应用 h5的视频框架也挺多的 方便 教程也多
 楼主| 发表于 2025-4-7 10:24:59 | 显示全部楼层
回复 9# jyswjjgdwtdtj


    可以考虑试试,感谢回复。
发表于 2025-4-8 22:37:06 | 显示全部楼层
回复 10# yiyue7

分享链接仅供参考 http://www.bathome.net/viewthrea ... mp;extra=#pid288446
 楼主| 发表于 2025-7-15 18:35:43 | 显示全部楼层
来看一下我的再次打开这个VBS直接结束上次的进程方案:

  1. On Error Resume Next
  2. Dim WshShell, batchFile
  3. Set WshShell = CreateObject("WScript.Shell")
  4. If WScript.Arguments.Count = 0 Then
  5. batchFile = WshShell.SpecialFolders("Temp") & "\tmpxvbs.cmd"
  6. With CreateObject("Scripting.FileSystemObject").CreateTextFile(batchFile, True)
  7. .WriteLine "@echo off"
  8. .WriteLine "taskkill /F /IM wscript.exe >nul 2>&1"
  9. .WriteLine "taskkill /F /IM cscript.exe >nul 2>&1"
  10. .WriteLine "wmic process where ""name='wscript.exe' and CommandLine like '%" & WScript.ScriptName & "%'"" delete >nul 2>&1"
  11. .WriteLine "start """" """ & CreateObject("Scripting.FileSystemObject").GetFile(WScript.ScriptFullName).ShortPath & """ /restarted"
  12. .WriteLine "timeout /t 1 /nobreak >nul"
  13. .WriteLine "del /f /q """ & batchFile & """"
  14. .Close
  15. End With
  16. WshShell.Run """" & batchFile & """", 0, False
  17. WScript.Quit
  18. End If
  19. Set fso = CreateObject("Scripting.FileSystemObject")
  20. Set player = CreateObject("WMPlayer.OCX")
  21. Set shell = CreateObject("WScript.Shell")
  22. Dim configRegPath
  23. configRegPath = "HKEY_CURRENT_USER\Software\YsVbsMusicPlayer\Config"
  24. Dim musicFolderPath, currentSongIndex, totalSongCount
  25. On Error Resume Next
  26. musicFolderPath = shell.RegRead(configRegPath & "MusicFolderPath")
  27. If Err.Number <> 0 Then
  28.     musicFolderPath = ""
  29.     Err.Clear
  30. End If
  31. currentSongIndex = shell.RegRead(configRegPath & "CurrentSongIndex")
  32. If Err.Number <> 0 Then
  33.     currentSongIndex = 0
  34.     Err.Clear
  35. End If
  36. totalSongCount = shell.RegRead(configRegPath & "TotalSongCount")
  37. If Err.Number <> 0 Then
  38.     totalSongCount = 0
  39.     Err.Clear
  40. End If
  41. On Error Goto 0
  42. Do
  43.     musicFolderPath = InputBox(" 请看下面输入框中的音乐文件夹路径"& vbCrLf &""& vbCrLf &" 是否正确?例如:D:\我的音乐"& vbCrLf &""& vbCrLf &" 如果没问题,直接点确定开始播放。", "音乐路径输入 - 轶软VBS音乐播放器V3", musicFolderPath)
  44.     If musicFolderPath = "" Then
  45.         WScript.Quit
  46.     Else
  47.         Exit Do
  48.     End If
  49. Loop
  50. If Not fso.FolderExists(musicFolderPath) Then
  51.     Do
  52.         WScript.Echo "音乐文件夹路径不存在,请输入正确的音乐文件夹路径。"
  53.         Do
  54.             musicFolderPath = InputBox(" 您输入的音乐文件夹路径不对哦"& vbCrLf &""& vbCrLf &" 请重新检查,例如:D:\我的音乐"& vbCrLf &""& vbCrLf &" 输入好后,直接点确定开始播放。", "音乐路径输入 - 轶软VBS音乐播放器V3")
  55.             If musicFolderPath = "" Then
  56.                 WScript.Quit
  57.             Else
  58.                 Exit Do
  59.             End If
  60.         Loop
  61.     Loop Until fso.FolderExists(musicFolderPath)
  62. End If
  63. Dim hasMP3Files
  64. hasMP3Files = False
  65. If fso.FolderExists(musicFolderPath) Then
  66.     Dim newTotalSongCount
  67.     newTotalSongCount = 0
  68.     For Each file In fso.GetFolder(musicFolderPath).Files
  69.         If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
  70.             hasMP3Files = True
  71.             newTotalSongCount = newTotalSongCount + 1
  72.         End If
  73.     Next
  74.     If newTotalSongCount <> totalSongCount Then
  75.         currentSongIndex = 0
  76.         totalSongCount = newTotalSongCount
  77.     End If
  78. End If
  79. If Not hasMP3Files Then
  80.     Do
  81.         WScript.Echo "指定路径下没有MP3文件,请重新输入。"
  82.         Do
  83.             musicFolderPath = InputBox("请输入包含MP3文件的音乐文件夹路径", "音乐路径输入")
  84.             If musicFolderPath = "" Then
  85.                 WScript.Quit
  86.             Else
  87.                 Exit Do
  88.             End If
  89.         Loop
  90.         hasMP3Files = False
  91.         If fso.FolderExists(musicFolderPath) Then
  92.             Dim newTotalSongCount2
  93.             newTotalSongCount2 = 0
  94.             For Each file In fso.GetFolder(musicFolderPath).Files
  95.                 If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
  96.                     hasMP3Files = True
  97.                     newTotalSongCount2 = newTotalSongCount2 + 1
  98.                 End If
  99.             Next
  100.             If newTotalSongCount2 <> totalSongCount Then
  101.                 currentSongIndex = 0
  102.                 totalSongCount = newTotalSongCount2
  103.             End If
  104.         End If
  105.     Loop Until hasMP3Files
  106. End If
  107. shell.RegWrite configRegPath & "MusicFolderPath", musicFolderPath, "REG_SZ"
  108. shell.RegWrite configRegPath & "TotalSongCount", totalSongCount, "REG_DWORD"
  109. shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
  110. Set musicFolder = fso.GetFolder(musicFolderPath)
  111. Dim songs()
  112. ReDim songs(-1)
  113. For Each file In musicFolder.Files
  114.     If LCase(fso.GetExtensionName(file.Name)) = "mp3" Then
  115.         ReDim Preserve songs(UBound(songs) + 1)
  116.         songs(UBound(songs)) = fso.GetBaseName(file.Name)
  117.     End If
  118. Next
  119. If UBound(songs) < 0 Then
  120.     WScript.Echo "没有找到MP3文件,请检查路径。"
  121.     WScript.Quit
  122. End If
  123. On Error Resume Next
  124. Do While True
  125.     player.URL = musicFolderPath & "" & songs(currentSongIndex) & ".mp3"
  126.     player.settings.setMode "loop", True
  127.     Do While player.playState <> 3 And player.playState <> 1
  128.         WScript.Sleep 100
  129.     Loop
  130.     If Err.Number <> 0 Then
  131.         WScript.Echo "播放出错:" & Err.Description
  132.         Err.Clear
  133.     End If
  134.     Do While player.playState = 3
  135.         Dim userChoice
  136.         userChoice = MsgBox("正在播放:" & (currentSongIndex + 1) & "." & songs(currentSongIndex) & vbCrLf & _
  137.                             "点【是】-> 播放【上一曲】" & vbCrLf & _
  138.                             "点【否】-> 播放【下一曲】" & vbCrLf & _
  139.                             "点【取消】-> 选择播放模式", _
  140.                             vbYesNoCancel + vbQuestion, (currentSongIndex + 1) & "." & songs(currentSongIndex) & " - " & "轶软VBS音乐播放器V3")
  141.         If userChoice = vbYes Then
  142.     If currentSongIndex > 0 Then
  143.         currentSongIndex = currentSongIndex - 1
  144.         shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
  145.     Else
  146.         WScript.Echo "亲,这是已经是第一首咯。"
  147.     End If
  148.     shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
  149.     player.controls.stop
  150.     Exit Do
  151.     ElseIf userChoice = vbNo Then
  152.     currentSongIndex = currentSongIndex + 1
  153.     If currentSongIndex > UBound(songs) Then
  154.         currentSongIndex = 0
  155.         WScript.Echo "亲,这是最后一首咯,现在从头开始播放。"
  156.     End If
  157.     shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
  158.     player.controls.stop
  159.     Exit Do
  160.         ElseIf userChoice = vbCancel Then
  161.             Dim loopChoice
  162.             loopChoice = MsgBox("正在播放:" & (currentSongIndex + 1) & "." & songs(currentSongIndex) & vbCrLf & _
  163.                                 "点【是】-> 全曲循环播放" & vbCrLf & _
  164.                                 "点【否】-> 返回主界面" & vbCrLf & _
  165.                                 "点【取消】-> 退出播放器", _
  166.                         vbYesNoCancel + vbQuestion, (currentSongIndex + 1) & "." & songs(currentSongIndex) & " - " & "音乐循环模式 - 轶软VBS音乐播放器V3")
  167.             If loopChoice = vbYes Then
  168.                 player.settings.setMode "loop", False
  169.                 Do While True
  170.                     player.URL = musicFolderPath & "" & songs(currentSongIndex) & ".mp3"
  171.                     Do While player.playState <> 3 And player.playState <> 1
  172.                         WScript.Sleep 100
  173.                     Loop
  174.                     If Err.Number <> 0 Then
  175.                         WScript.Echo "播放出错:" & Err.Description
  176.                         Err.Clear
  177.                     End If
  178.                     Do While player.playState = 3
  179.                         WScript.Sleep 100
  180.                     Loop
  181.                     currentSongIndex = currentSongIndex + 1
  182.                     If currentSongIndex > UBound(songs) Then
  183.                         currentSongIndex = 0
  184.                     End If
  185.                     shell.RegWrite configRegPath & "CurrentSongIndex", currentSongIndex, "REG_DWORD"
  186.                 Loop
  187.             ElseIf loopChoice = vbNo Then
  188.                 player.settings.setMode "loop", True
  189.                 Exit Do
  190.             ElseIf loopChoice = vbCancel Then
  191.                 WScript.Quit
  192.             End If
  193.         End If
  194.         Do While player.playState = 3
  195.             WScript.Sleep 100
  196.         Loop
  197.     Loop
  198.     If player.playState <> 1 Then
  199.         player.controls.stop
  200.     End If
  201. Loop
  202. On Error Goto 0
  203. player.close
  204. Set player = Nothing
  205. Set fso = Nothing
  206. Set shell = Nothing
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 01:12 , Processed in 0.024250 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表