Computer Hope

Software => Computer programming => Topic started by: Swollow on September 09, 2012, 01:04:50 PM

Title: VBS Pause/Resume music WMPlayer.OCX
Post by: Swollow on September 09, 2012, 01:04:50 PM
hey, I know how to play music in VBS like so:
Code: [Select]
Set wmp = CreateObject("WMPlayer.OCX")
wmp.settings.autoStart = True
wmp.settings.volume = 50
wmp.URL = "Music.mp3"
while wmp.Playstate <> 1
WSH.Sleep 100
wend
but how would I pause/resume music?

(I'm using Windows 7 FYI)
Title: Re: VBS Pause/Resume music WMPlayer.OCX
Post by: Sidewinder on September 09, 2012, 03:36:07 PM
Script will play first 20 seconds of song, pause for 5 seconds and then resume playing the song. Change the timings as needed.

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

wmp.settings.autoStart = True
wmp.settings.volume = 50
wmp.URL = "Music.mp3"
PauseAndResume()

While wmp.Playstate <> 1
  WScript.Sleep 100 
Wend

Function PauseAndResume()
  WScript.Sleep 20000
  wmp.controls.pause()
  WScript.Sleep 5000
  wmp.controls.play()
End Function

WSH was not defined. Changed to WScript.

 8)
Title: Re: VBS Pause/Resume music WMPlayer.OCX
Post by: Swollow on September 09, 2012, 07:30:48 PM
How would I pause music that was playing from a different vbs script, like 1 file to play a song and 1 to pause it and 1 to resume it
Title: Re: VBS Pause/Resume music WMPlayer.OCX
Post by: Sidewinder on September 10, 2012, 10:23:56 AM
You want two scripts to control the same object instance (WMP)? I think you would have to create an out-of-process COM object. Don't know if this would work, but it sure sounds promising.  ;)

An alternative would be to run WMP in a window. While the media is playing, give the WMP window focus and you can use CTL-P to control pause/resume operations. See this post (http://www.computerhope.com/forum/index.php/topic,132991.msg858674.html#msg858674) for ideas.

 8)
Title: Re: VBS Pause/Resume music WMPlayer.OCX
Post by: Swollow on September 10, 2012, 04:11:42 PM
You want two scripts to control the same object instance (WMP)? I think you would have to create an out-of-process COM object. Don't know if this would work, but it sure sounds promising.  ;)

An alternative would be to run WMP in a window. While the media is playing, give the WMP window focus and you can use CTL-P to control pause/resume operations. See this post (http://www.computerhope.com/forum/index.php/topic,132991.msg858674.html#msg858674) for ideas.

 8)
what I'm doing is programming MS-DOS Batch and occasionally using vbs files to do things batch can't so the second option isn't going to work, I'm not very familiar with vbs and don't know what an out of process COM object is, its rather hard to get batch to communicate with the vbs maybe an option would be to create a text document with the value of the time the music was at then kill the process and then restart the process later with the music starting at that point in time
Title: Re: VBS Pause/Resume music WMPlayer.OCX
Post by: Sidewinder on September 11, 2012, 07:42:51 AM
I don't think this is going to happen. Giving focus to Win7 WMP from an external script for some reason does not work. I could only give focus from within the script that launched WMP. That would eliminate having seperate scripts for pause and resume.

The Script Component also fizzled out. I could get the music to play but could not pause/resume it. The WMP object and the script run asynchronous, which explains the while/wend loop in your script and the sleep in my script. VBScript is single threaded, so interrupting the loop might not be possible.

My suggestion is to manually start the music, and use CTL-P to toggle between pause and resume. Make sure the WMP window has focus when using CTL-P.

 8)

To add insult to injury, I found that giving focus to WMP on WinXP works just fine.
Title: Re: VBS Pause/Resume music WMPlayer.OCX
Post by: Swollow on September 11, 2012, 04:16:47 PM
alright thanks for trying though