[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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

下面是一个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
复制代码
以上就是完整代码了,急用拜托。

能给优化一下就更好了,比我我想的是,循环播放完一首后,自动关闭,弹出界面播放第二首,这样界面就能同步显示歌曲名称了,如果不能实现,就在循环播放时弹出显示操作界面吧,现在必须得到进程去关闭才行,有点麻烦啊

TOP

本帖最后由 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
复制代码
套个循环是何意?
你好

TOP

回复 3# jyswjjgdwtdtj


    你好,如果方便的话,您能帮我改一下吗?deepseek写的还是欠佳啊,do循环那个还有循环播放不显示,我也不知道怎么改了,您能给个完整的可以运行的吗,谢谢了

TOP

回复 4# yiyue7


   用vbs实现交互实在不是一个容易的事 考虑用hta做个小应用?
你好

TOP

回复 5# jyswjjgdwtdtj


    算了,不整了,就这么用吧,hta界面和控制显示有的会有问题,兼容性问题,如果彻底还不如py呢

TOP

本帖最后由 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
复制代码

TOP

回复 7# aloha20200628


    你好,只是单纯的听歌玩而已,我做的这种脚本肯定没有播放器软件功能齐全的,纯属娱乐,感谢你的回复。

TOP

回复 8# yiyue7


   自制播放器可以考虑html5 像chrome可以吧网页打包为本地应用 h5的视频框架也挺多的 方便 教程也多
你好

TOP

返回列表