vbs版的音乐播放器

vbs版的音乐播放器

功能:

1.支持文件拖放,可以拖放一个音频文件到脚本上。

2.双击脚本会打开一个文件选择对话框。

PS:播放器本身没有技术含量,只是在调用wmplayer.ocx对象。纯属娱乐!

PSS: 播发器没有UI界面,会在后台播放音频文件。脚本运行后会在创建wscript.exe/cscript.exe 或mshta.exe两个进程,需要手动结束才能关闭。

代码:(请将下列代码复制到记事本里,并保存为扩展名为vbs文件)

Call Main()
Sub Main()									
Dim objArgs, objwmp, strFilePath, nArgsCount


Set objArgs = WScript.Arguments   
nArgsCount = objArgs.Count		  '保存拖放文件的个数

Set objwmp = CreateObject("wmplayer.ocx")
objwmp.settings.volume = 50
objwmp.uiMode = "Invisible"

Select Case nArgsCount

Case 0:				   <span style="white-space:pre">	</span>'没有拖放文件,直接打开文件选择对话框
strFilePath = LoadFile()
objwmp.URL = strFilePath		'设置播放文件的路径

Do Until objwmp.playState = 1   <span style="white-space:pre">	</span>'设置播放状态

WScript.Sleep 1000

Loop

Case 1:							'拖放一个音频文件
strFilePath = objArgs(0)
objwmp.URL = strFilePath

	Do Until objwmp.playState = 1
	WScript.Sleep 1000
	Loop


Case Else:
MsgBox "对不起,一次只能拖放一个文件", vbYes + vbError, "错误"
WScript.Quit

End Select

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'目的:打开文件选择对话框并返回文件路径
'参数:无
'函数名:LoadFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function LoadFile()
Dim shell : Set shell = CreateObject("WScript.Shell") 
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") 

Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2) 
Dim tempName : tempName = fso.GetTempName() 
Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta") 

tempFile.Write _ 
"<html>" & _ 
"<head>" & _ 
"<title>Browse</title>" & _ 
"</head>" & _ 
"<body>" & _ 
"<input type='file' id='f' />" & _ 
"<script type='text/javascript'>" & _ 
"var f = document.getElementById('f');" & _ 
"f.click();" & _ 
"var shell = new ActiveXObject('WScript.Shell');" & _ 
"shell.RegWrite('HKEY_CURRENT_USER\\Volatile Environment\\MsgResp', f.value);" & _ 
"window.close();" & _ 
"</script>" & _ 
"</body>" & _ 
"</html>" 
tempFile.Close 
shell.Run tempFolder & "\" & tempName & ".hta", 0, True 
LoadFile = shell.RegRead("HKEY_CURRENT_USER\Volatile Environment\MsgResp") 
shell.RegDelete "HKEY_CURRENT_USER\Volatile Environment\MsgResp" 

End Function
结束进程代码( 请将下列代码复制到记事本里,并保存为扩展名为vbs文件):
' Terminate a Process

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'wscript.exe' Or Name = 'mshta' Or Name = 'cscript'")

For Each objProcess in colProcessList
    objProcess.Terminate()
Next

纯属娱乐!!!


你可能感兴趣的:(vbs版的音乐播放器)