Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: vbs to play mp3  (Read 21157 times)

0 Members and 1 Guest are viewing this topic.

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
vbs to play mp3
« on: August 24, 2012, 07:32:10 PM »
So I was writing a vbscript to run from command line and take the input and play the corresponding mp3 file. I used some code I found online and tested it, and it worked perfect for running a set file, but when I tried to replace that part with a string, it doesn't do anything. Can anyone point out what's wrong? Thanks in advance!

Code: [Select]
dim mp3
dim mp3name
mp3name = WScript.Arguments.Item(0)
mp3 = Chr(34) & WScript.Arguments.Item(0) & ".mp3" & Chr(34)
CreateObject("WScript.Shell").Run "wmplayer " & Chr(34) & mp3 & Chr(34), 0
WScript.echo("Playing " + mp3name + ".mp3   -  " + mp3)
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: vbs to play mp3
« Reply #1 on: August 25, 2012, 07:37:51 AM »
It might be easier to pass the quotes on the command line instead of trying to resolve them in the script:

Code: [Select]
Set wmp = CreateObject("WMPlayer.ocx")

wmp.URL = WScript.Arguments.Item(0)
wmp.openPlayer(wmp.URL)

You will probably have to fix-up the paths to the songs.

Run example: cscript scriptname.vbs "n:\multimedia\audio\sting-all this time.mp3"

An alternative would be to use a batch file:

Code: [Select]

@echo off
setlocal

"C:\Program Files\Windows Media Player\wmplayer.exe" "%*"

You will probably have to fix-up the paths to the wmplayer and the songs.

Run example: scriptname.bat n:\multimedia\audio\sting-all this time.mp3
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: vbs to play mp3
« Reply #2 on: August 25, 2012, 03:45:03 PM »
Thanks a lot! Although I didn't use all of your code, it got me on the right track. (semi-)finshed code below.  :)

Code: [Select]
mp3name = WScript.Arguments.Item(0)

set wmp = CreateObject("WMPlayer.ocx")

wmp.URL = WScript.Arguments.Item(1)

CreateObject("WScript.Shell").Run "wmplayer " & mp3 ,0

WScript.echo("Now playing " + mp3name + ".")

sample Input: mp3 "Question of Fate" "C:\...pathway...\Question of Fate.mp3"

Quick question, is there a way to run the sound without opening a window?
i've tried it, but it doesn't seem to do anything without WScript.echo in it.
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: vbs to play mp3
« Reply #3 on: August 25, 2012, 06:20:45 PM »
Ok, not so much solved as worked around. I put a WScritp.sleep after I open the file and set it to WScript.Arguments.Item(1) so I have to enter the length as well, but now I have no annoying window.

Code: [Select]
'Syntax: mp3v2 "<Name of File>" <time in sec>00
'Example Input: mp3v2 "Question of Fate.mp3" 184000

set wmp = CreateObject("WMPlayer.ocx")
wmp.URL = WScript.Arguments.Item(0)
dim time
time= WScript.Arguments.Item(1)


CreateObject("WScript.Shell").Run "wmplayer " & mp3 ,0
Wscript.sleep(time)
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: vbs to play mp3
« Reply #4 on: August 26, 2012, 08:18:34 AM »
I'm not sure what is going on here. Part of your code is scripting the WMP object and then the switch to the run method. Also the mp3 variable is undefined and time is a reserved word.

This is for your consideration. Before you panic at the sight of the code, WMP runs asynchronous to the script so most of the script is spent calculating how much time to wait for the song to play.

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
Set wmp = CreateObject("WMPlayer.ocx")
Set objShell = CreateObject("Shell.Application")
Set WshShell = CreateObject("WScript.Shell")

' Prompt for song
'
Do
WScript.StdOut.Write("Enter Song Name: ")
strSong = WScript.StdIn.ReadLine
Loop Until fso.FileExists(strSong)

' Calculate song length (milliseconds)
'
Set f = fso.GetFile(strSong)
folder = f.ParentFolder
file = f.Name

Set shellFolder = objShell.NameSpace(folder)
Set shellFile = shellFolder.ParseName(file)

duration = Split(shellfolder.GetDetailsOf(shellfile, 27), ":")
sleeptime = ((duration(1)*60) + duration(2)) * 1000

' Play the song
'
wmp.openPlayer(strSong)
WScript.Sleep sleeptime

' Shutdown the player
'
WshShell.AppActivate("Windows Media Player")
WshShell.SendKeys "%{F4}"

The user is prompted for the song name. A fully qualified path is required; do not use quotes. Win7 does not seem to have a shortcut key to minimize a window.  For XP you can add the following code after the wmp.openPlayer line:

Code: [Select]
WScript.Sleep 2000
WshShell.AppActivate("Windows Media Player")
WshShell.SendKeys "% n"

If nothing else, this may give you some ideas.  8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: vbs to play mp3
« Reply #5 on: August 26, 2012, 11:05:13 AM »
Also the mp3 variable is undefined and time is a reserved word.

Time isn't a reserved word in VBScript. It's a reserved word in Visual Basic.

That said, their script still doesn't make sense. First it starts using OLE Automation and then it uses shell. it uses a variable that has no value (mp3), too (as you noted)

Either way, absolutely none of the provided ProgIDs works for me :/ They all throw a "ActiveX Component cannot create Object" Error in both VBScript as well as in VB6.
I was trying to dereference Null Pointers before it was cool.