How to play all songs in windows player in vbscript?

后端 未结 2 1385
误落风尘
误落风尘 2020-12-11 08:43

Could you please help to write a script to play all songs in particular folder via vbscript.

Set wmp = CreateObject(\"WMPlayer.OCX\")
wmp.openPlayer(\"xxx.mp         


        
2条回答
  •  眼角桃花
    2020-12-11 09:10

    This Vbscript PlayListSongs.vbs scan into a folder and its subfolders for songs and create a playlist in a text file in order to play it in the background. So just give a try ;)

    '***********************************Description*************************************
    'This Vbscript scan into a folder and its subfolders for songs like .mp3 .wav .....
    'And create a playlist in a text file in order to play it in the background.
    '© Hackoo © 2015
    '***********************************************************************************
    Option Explicit
    If AppPrevInstance() Then 
        MsgBox "There is an existing proceeding !" & VbCrLF &_
        CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
        WScript.Quit   
    Else 
        Dim Folder,File,fso,MyPlayList,Temp,oExec,ws,Title,WaitingMsg
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ws = CreateObject("WScript.Shell")
        Temp = ws.ExpandEnvironmentStrings("%Temp%")
        Folder = Browse4Folder()
        MyPlayList = Folder & "\MyPlayList.txt"
        If fso.FileExists(MyPlayList) Then
            fso.DeleteFile(MyPlayList)
        End If
        Title = "Looking for songs in "& DblQuote(Folder) & " using Vbscript © Hackoo 2015"
        WaitingMsg = "Please wait... Searching for songs into : "& DblQuote(Folder) & " is in progress..."
        Call CreateProgressBar(Title,WaitingMsg)'Creation of Waiting Bar
        Call LancerProgressBar() 'Launch of the Waiting Bar
        Call Pause(10)
        Call Scan4Songs(Folder)
        Call FermerProgressBar()
        Call Play(MyPlayList)
    End If
    '*********************************************************
    Sub Play(File)
        On Error Resume Next
        Dim Sound,Xwmp
        Dim fso,F,ReadME,PlayList,i,Ws,Copyright,Name,Duration
        Copyright = " © Hackoo © 2015"
        Set Ws = CreateObject("wscript.Shell")
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set F = fso.OpenTextFile(File,1)
        If Err <> 0 Then
            Ws.popup Err.Description & VbCrlF &_
            "No media file found !","3",Err.Description & Copyright,VbCritical
            wscript.quit()
        End If
        ReadMe = F.ReadAll
        PlayList = split(ReadMe,vbcrlf)
        Set Sound = CreateObject("WMPlayer.OCX")
        Sound.settings.volume = 100
        Sound.currentPlaylist.Clear
        For i = Lbound(PlayList) to Ubound(PlayList)
            Set Xwmp = Sound.newMedia(PlayList(i))
            Sound.currentPlaylist.insertItem(i),Xwmp
            Sound.Controls.Play()   
            Do while Sound.currentmedia.duration = 0
                wscript.sleep 100
            Loop
            wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
        Next    
    End Sub
    '*********************************************************************************************
    Function AppPrevInstance()   
        With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
            With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
                " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
                AppPrevInstance = (.Count > 1)   
            End With   
        End With   
    End Function    
    '*********************************************************************************************
    Function CommandLineLike(ProcessPath)   
        ProcessPath = Replace(ProcessPath, "\", "\\")   
        CommandLineLike = "'%" & ProcessPath & "%'"   
    End Function
    '*********************************************************************************************
    Function Browse4Folder()
        Dim objShell,objFolder,Message
        Message = "Please select a folder in order to scan into it and its subfolders for songs"
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.BrowseForFolder(0,Message,1,"c:\Programs")
        If objFolder Is Nothing Then
            Wscript.Quit
        End If
        Browse4Folder = objFolder.self.path
    end Function
    '*********************************************************************************************
    Function Scan4Songs(Folder)
        Dim File,Ext,i,SubFolder
        Set Folder = fso.GetFolder(Folder)
        For each File in Folder.Files
            Ext = Array("mp3","wav","ogg","asf","aa3","m3v","midi")
            For i = LBound(Ext) To UBound(Ext)
                If LCase(fso.GetExtensionName(File.name)) = LCase(Ext(i)) Then 
                    Call MakePlayListFile(MyPlayList,File.Path)
                end if
            Next
        Next
        For each SubFolder in Folder.SubFolders
            Call Scan4Songs(SubFolder.Path)
        Next
    End Function
    '*********************************************************************************************
    Sub MakePlayListFile(MyPlayList,strContents)
        Dim fso,ts
        Const ForAppending= 8
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.OpenTextFile(MyPlayList,ForAppending,True)
        ts.WriteLine strContents
        ts.Close
    End Sub
    '**********************************************************************************************
    Sub CreateProgressBar(Title,WaitingMsg)
        Dim ws,fso,f,f2,ts,ts2,Ligne,i,fread,LireTout,NbLigneTotal,Temp,PathOutPutHTML,fhta,oExec
        Set ws = CreateObject("wscript.Shell")
        Set fso = CreateObject("Scripting.FileSystemObject")
        Temp = WS.ExpandEnvironmentStrings("%Temp%")
        PathOutPutHTML = Temp & "\Barre.hta"
        Set fhta = fso.OpenTextFile(PathOutPutHTML,2,True)
        fhta.WriteLine ""
        fhta.WriteLine ""
        fhta.WriteLine "  " & Title & ""
        fhta.WriteLine ""
        fhta.WriteLine ""
        fhta.WriteLine "
    " fhta.WriteLine "" & WaitingMsg &"" fhta.WriteLine "" fhta.WriteLine "
    " fhta.WriteLine "" fhta.close End Sub '********************************************************************************************** Sub LancerProgressBar() Set oExec = Ws.Exec("mshta.exe " & Temp & "\Barre.hta") End Sub '********************************************************************************************** Sub FermerProgressBar() oExec.Terminate End Sub '********************************************************************************************** Function DblQuote(Str) DblQuote = Chr(34) & Str & Chr(34) End Function '********************************************************************************************** Sub Pause(Secs) Wscript.Sleep(Secs * 1000) End Sub '**********************************************************************************************

    EDIT : 19/08/2015 Another version

    In this example, you can scan into a folder and its subfolders for songs like .mp3. And create a playlist (MyPlayList.m3u) file in order to play it with Windows Media Player.

    [VBS] PlayListSongsWMP.vbs

    '***********************************Description*************************************
    'This Vbscript scan into a folder and its subfolders for songs like .mp3
    'And create a playlist file in order to play it with Windows Media Player.
    'Created on 05/04/2015 © Hackoo © 2015
    '***********************************************************************************
    Option Explicit
    If AppPrevInstance() Then 
        MsgBox "There is an existing proceeding !" & VbCrLF &_
        CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
        WScript.Quit   
    Else 
        Dim Folder,File,fso,MyPlayList,Temp,oExec,ws,Title,WaitingMsg
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ws = CreateObject("WScript.Shell")
        Temp = ws.ExpandEnvironmentStrings("%Temp%")
        Folder = Browse4Folder()
        MyPlayList = Folder & "\MyPlayList.m3u"
        If fso.FileExists(MyPlayList) Then
            fso.DeleteFile(MyPlayList)
        End If
        Title = "Looking for songs in "& DblQuote(Folder) & " using Vbscript © Hackoo 2015"
        WaitingMsg = "Please wait... Searching for songs into : "& DblQuote(Folder) & " is in progress..."
        Call CreateProgressBar(Title,WaitingMsg)'Creation of Waiting Bar
        Call LancerProgressBar() 'Launch of the Waiting Bar
        Call Pause(10)
        Call Scan4Songs(Folder)
        Call FermerProgressBar()
        Call Play(MyPlayList)
    End If
    '*********************************************************
    Sub Play(File)
        On Error Resume Next
        Dim Sound,Ws,Copyright
        Copyright = " © Hackoo © 2015"
        Set Ws = CreateObject("wscript.Shell")
        If Err <> 0 Then
            Ws.popup Err.Description & VbCrlF &_
            "No media file found !","3",Err.Description & Copyright,VbCritical
            wscript.quit()
        Else
        Set Sound = CreateObject("WMPlayer.OCX")
        Sound.settings.volume = 100
        Sound.OpenPlayer(File)
        End If
    End Sub
    '*********************************************************************************************
    Function AppPrevInstance()   
        With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
            With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
                " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
                AppPrevInstance = (.Count > 1)   
            End With   
        End With   
    End Function    
    '*********************************************************************************************
    Function CommandLineLike(ProcessPath)   
        ProcessPath = Replace(ProcessPath, "\", "\\")   
        CommandLineLike = "'%" & ProcessPath & "%'"   
    End Function
    '*********************************************************************************************
    Function Browse4Folder()
        Dim objShell,objFolder,Message
        Message = "Please select a folder in order to scan into it and its subfolders for songs"
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.BrowseForFolder(0,Message,1,"c:\Programs")
        If objFolder Is Nothing Then
            Wscript.Quit
        End If
        Browse4Folder = objFolder.self.path
    end Function
    '*********************************************************************************************
    Function Scan4Songs(Folder)
        On Error Resume Next
        Dim File,Ext,item,SubFolder
        Set Folder = fso.GetFolder(Folder)
        For each File in Folder.Files
            Ext = Array("mp3")
            For each item in Ext
                If LCase(fso.GetExtensionName(File.name)) = LCase(item) Then 
                    Call MakePlayListFile(MyPlayList,File.Path)
                end if
            Next
        Next
        For each SubFolder in Folder.SubFolders
            Call Scan4Songs(SubFolder.Path)
        Next
    End Function
    '*********************************************************************************************
    Sub MakePlayListFile(MyPlayList,strContents)
        Dim fso,ts
        Const ForAppending = 8
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.OpenTextFile(MyPlayList,ForAppending,True)
        ts.WriteLine "#UTF8: "& strContents
        ts.WriteLine strContents
        ts.Close
    End Sub
    '**********************************************************************************************
    Sub CreateProgressBar(Title,WaitingMsg)
        Dim ws,fso,f,f2,ts,ts2,Ligne,i,fread,LireTout,NbLigneTotal,Temp,PathOutPutHTML,fhta,oExec
        Set ws = CreateObject("wscript.Shell")
        Set fso = CreateObject("Scripting.FileSystemObject")
        Temp = WS.ExpandEnvironmentStrings("%Temp%")
        PathOutPutHTML = Temp & "\Barre.hta"
        Set fhta = fso.OpenTextFile(PathOutPutHTML,2,True)
        fhta.WriteLine ""
        fhta.WriteLine ""
        fhta.WriteLine "  " & Title & ""
        fhta.WriteLine ""
        fhta.WriteLine ""
        fhta.WriteLine "
    " fhta.WriteLine "" & WaitingMsg &"" fhta.WriteLine "" fhta.WriteLine "
    " fhta.WriteLine "" fhta.close End Sub '********************************************************************************************** Sub LancerProgressBar() Set oExec = Ws.Exec("mshta.exe " & Temp & "\Barre.hta") End Sub '********************************************************************************************** Sub FermerProgressBar() oExec.Terminate End Sub '********************************************************************************************** Function DblQuote(Str) DblQuote = Chr(34) & Str & Chr(34) End Function '********************************************************************************************** Sub Pause(Secs) Wscript.Sleep(Secs * 1000) End Sub '**********************************************************************************************

提交回复
热议问题