Scheduling Windows 10/11 PowerShell Script For Continuous Playback

To discuss development of addons / skins / customization of MediaMonkey.

Moderators: jiri, drakinite, Addon Administrators

Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

Topic: I play MMW5 nonstop 24/7. I have playlists containing thousands of unique songs that I use Auto DJ to select music from. This allows me to hear unique music based on results from my playlist.

Problem Statement: MMW5 will crash with a white screen that will immediately halt all playback until I return to my computer. I must shut down the running task that is frozen, restart MMW5, and finally click the play button to continue playback. Unless I take these manual steps, MMW5 is frozen at a white screen forever.

Automated Solution: I created a PowerShell script that automatically does all this work. I schedule this script to execute via WIndows Task Scheduler. I have been using this script successfully for days, and it is working perfectly to address my problem statement. Note that if you do see that MMW5 is frozen you have the ability to run the scheduled task on demand to resume playback instantly. Without taking on-demand action, the issue gets resolved during the next scheduled run of the script. I have just used this script successfully with MMW5 frozen on a complete white screen. I will share this script with others, and I will also show you how to schedule the script via Windows Task Scheduler.
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

Update for 2024.0.0.3018: I discovered a previously reported issue has returned in this version. I have modified this script to work-around this issue (see https://www.mediamonkey.com/forum/viewt ... 0&start=15 for details). My work-around involves creating backup copies of your %APPDATA%\MediaMonkey5\persistent.json files and optionally your %APPDATA%\MediaMonkey5\MediaMonkey.ini. Create a folder on your C: drive: C:\Backups\MMW5. Copy either of these two files after you have configured MMW5 how you wish. If either file exists in C:\Backups\MMW5, the files will be restored. Only the files that exist will be restored. Also, configure the UI exactly how you like it. After you have your perfect configuration and UI look, close MMW5 and copy the files to C:\Backups\MMW5. Note that the MediaMonkey.ini restore will alter the last file played by Auto DJ. Skip restore of this file if you want continuation of playback from the previous execution.

Below is the complete script. Copy + paste all of this text into your favorite text editor. You may use Windows notepad if you do not have an editor:

Code: Select all

<#	
	.NOTES
	===========================================================================
	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2024 v5.8.237
	 Created on:   	1/22/2024 6:48 PM
	 Modified on:	5/3/2024 5:24 PM
	 Created by:   	Dryst
	 Organization: 	
	 Filename: EndThenRelaunchMMW5     	
	===========================================================================
	.DESCRIPTION
		Script that force stops the running Media Monkey 5 task, waits five seconds, 
		and launches it again with the play command line parameter.
		Additionally, this script restores backed up copies of MediaMonkey.ini and
		persistent.json. It looks for backups of these two files at C:\Backups\MMW5
#>

$mediaMonkeyProcessId = (Get-Process MediaMonkey).Id

Stop-Process -Id $mediaMonkeyProcessId -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2

# Ending the MediaMonkey process should automatically end all MediaMonkeyEngine processes.
# The code above waits two seconds to make sure they are closed. Any remaining open after
# two seconds must be hung. The code below will catch and force close them.

$mediaMonkeyEngineProcessIds = (Get-Process MediaMonkeyEngine).Id

foreach ($processId in $mediaMonkeyEngineProcessIds) {
	Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
}

Start-Sleep -Seconds 5

$appDataPath = (Get-ChildItem -Path Env:APPDATA).Value

# check if MediaMonkey.ini backup exists
If (Test-Path "C:\Backups\MMW5\MediaMonkey.ini") {
	# the backup was found, so restore it while the app is closed
	Write-host "MediaMonkey.ini backup found; restoring now..." -f Cyan
	Copy-Item -Path "C:\Backups\MMW5\MediaMonkey.ini" -Destination ($appDataPath + "\MediaMonkey5\") -Force -ErrorAction SilentlyContinue
}
Else {
	Write-host "MediaMonkey.ini backup not found; skipping settings restore" -f Gray
}

# check if persistent.json backup exists
If (Test-Path "C:\Backups\MMW5\persistent.json") {
	# the backup was found, so restore it while the app is closed
	Write-host "persistent.json backup found; restoring now..." -f Cyan
	Copy-Item -Path "C:\Backups\MMW5\persistent.json" -Destination ($appDataPath + "\MediaMonkey5\") -Force -ErrorAction SilentlyContinue
}
Else {
	Write-host "persistent.json backup not found; skipping UI layout restore" -f Gray
}

If (Test-Path "C:\Program Files (x86)\Ventis\MediaMonkey\MediaMonkey.exe") {
	Write-host "MediaMonkey 2024 found; starting now..." -f Cyan
	Start-Process -FilePath "C:\Program Files (x86)\Ventis\MediaMonkey\MediaMonkey.exe" -ArgumentList "/NoSplash /Play" -ErrorAction SilentlyContinue
}
Else {
	If (Test-Path "C:\Program Files (x86)\MediaMonkey 5\MediaMonkey.exe") {
		Write-host "MediaMonkey 5 found; starting now..." -f Cyan
		Start-Process -FilePath "C:\Program Files (x86)\MediaMonkey 5\MediaMonkey.exe" -ArgumentList "/NoSplash /Play" -ErrorAction SilentlyContinue
	}
}

Remove-Variable processId -ErrorAction SilentlyContinue
Remove-Variable mediaMonkeyProcessId -ErrorAction SilentlyContinue
Remove-Variable mediaMonkeyEngineProcessIds -ErrorAction SilentlyContinue
Remove-Variable appDataPath -ErrorAction SilentlyContinue
Save the file above with the file name: EndThenRelaunchMMW5.ps1

Save this script into C:\Scripts directory. If Scripts does not exist, create the folder on your C: drive.
Last edited by Dryst on Thu May 09, 2024 7:13 pm, edited 9 times in total.
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

With the script saved, I will go over how to schedule the task:

First, launch Windows Task Scheduler:

https://www.dropbox.com/scl/fi/qz9fukuo ... ddxve&dl=0

Once Task Scheduler appears, click Create Task shown on the right pane:

https://www.dropbox.com/scl/fi/a2fs722c ... o6wst&dl=0

You will see a task creation dialog appear next:

https://www.dropbox.com/scl/fi/74pvei34 ... qbve9&dl=0

Enter values as shown above for the General tab.

Click the Triggers Task tab next. Click the New... button in the dialog to create a new trigger:

https://www.dropbox.com/scl/fi/yrf5skl2 ... yg0ys&dl=0

Enter all options as shown above. Note that for Advanced settings Repeat task every option that maximum value from the pick list is 1 hour. Simply select 1 hour than change the 1 to the number of hours you wish the task to repeat. I entered 2. Please note that this value must be longer than the longest single track that may be played by Auto DJ. I have some tracks that are continuous album playback that exceed one hour in length. If I was to repeat the task every hour and this long track was playing, I would end up in a never ending cycle of this single long track being played repeatedly. Please keep this in mind.

Click the Actions tab next where you will configure the task to execute the PowerShell script:

https://www.dropbox.com/scl/fi/nqwl679m ... 5wzhi&dl=0

For the Add arguments (optional) text input, enter the following:

Code: Select all

-NonInteractive -NoProfile -ExecutionPolicy ByPass -File "C:\Scripts\EndThenRelaunchMMW5.ps1"
Click the Conditions tab next. You may enter any conditions you wish. My playback depends on a network connection, so I set mine as follows:

https://www.dropbox.com/scl/fi/6h3twk92 ... i6kon&dl=0

Finally, click the Settings tab:

https://www.dropbox.com/scl/fi/ipyvk2p3 ... yca3b&dl=0

After all this is entered, click the OK button. You will have a task that runs as often as you requested that will restart MMW5. You also have the option of running the task on demand if you find MMW5 if frozen with a white screen.
Barry4679
Posts: 2439
Joined: Fri Sep 11, 2009 8:07 am
Location: Australia
Contact:

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Barry4679 »

Hi Dryst,

thanks for the script and the introduction to PowerShell and the Windows Task Scheduler.
I had no knowledge of either of those two Windows tools.

I have no personal need for the Continuous Playback situation that you developed the script for.
I am looking for a less irksome way of restarting MM5 after it has crashed, as we have discussed in another thread.

I followed your clear instructions, and the script works as advertised if I run it in the PowerShell ISE or via the "Run with PowerShell" option in the content menu for the script file.

I tried to run it manually from the Task Scheduler .. ie. not using a time trigger. Via this route the script closes MM5, but the MM5 app does not reopen on the desktop. I see that it starts new MM5 processes, with new PIDs, but the GUI is not available.

I tried removing the scripts Play and NOSPLASH arguments, but this didn't seem to help.

Any idea what could be causing this?

FWIIW I would think a command to restart MM5 easily after a crash may be more generally applicable than the 24*7 availability issue.

You may want to consider a new post to address the second issue, as it is not immediately obvious that your script has more general applicability. In this Use Case the splash screen is useful as assurance that something is happening while MM5 churns about starting, after the 2+5 seconds delay.

And in this Use Case the complication setting up the Task Scheduler could be avoided. All is needed is a shortcut to run the script isn't it?
Want a dark skin for MM5? This is the one that works best for me .. elegant, compact & clear.
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

The script closes MMW5, but it does not start after closing. This is your issue, correct?

Did you install Media Monkey 5 to a different location than default? This would be a reason it is not starting for you. Try doing a WIndows Key + R and enter the following within the Run, Open input that appears:

"C:\Program Files (x86)\MediaMonkey 5\MediaMonkey.exe"

If you click OK, will MMW5 launch and be visible? My guess is the answer is no. This is all the PowerShell script is doing.

Also, this script was written assuming you have a 64-bit OS. If you have 32-bit this could be another issue.
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

Barry4679 wrote: Wed Feb 07, 2024 7:44 am And in this Use Case the complication setting up the Task Scheduler could be avoided. All is needed is a shortcut to run the script isn't it?
If MMW5 was reliable, I would have no need for this script.

It's not reliable. That is the issue.
Barry4679
Posts: 2439
Joined: Fri Sep 11, 2009 8:07 am
Location: Australia
Contact:

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Barry4679 »

Dryst wrote: Fri Feb 09, 2024 8:28 pm The script closes MMW5, but it does not start after closing. This is your issue, correct?
Correct.
Dryst wrote: Fri Feb 09, 2024 8:28 pm Did you install Media Monkey 5 to a different location than default? This would be a reason it is not starting for you. Try doing a WIndows Key + R and enter the following within the Run, Open input that appears:

"C:\Program Files (x86)\MediaMonkey 5\MediaMonkey.exe"

If you click OK, will MMW5 launch and be visible? My guess is the answer is no. This is all the PowerShell script is doing.

Also, this script was written assuming you have a 64-bit OS. If you have 32-bit this could be another issue.
I have 65 bit OS.
MM5 installed in the default location.
The Windows key + R test works OK, ie. MM5 starts
Also as I said before the script works as advertised if I run it in the PowerShell ISE or via the "Run with PowerShell" option in the content menu for the script file.

Must be something I don't know about a running a task manually from the Task Scheduler, or how to create a task inside the scheduler w/o a trigger, or a maybe it is a rights issue.
I don't need an answer to this, I was just trying to springboard into the an evaluation of the Task Scheduler, arising from your posting.
Dryst wrote: Fri Feb 09, 2024 8:43 pm If MMW5 was reliable, I would have no need for this script.
It's not reliable. That is the issue.
I am not arguing this point.
Personally I find MM5 to be stable, but then I work within a narrow path, and don't push out into weeds by trying to run music 24*7 etc.

I can pretty much crash MM5 at will.
The steps are:
  1. Try something outside of my narrow path :D
The point that I was trying to make was that your script appears to be more generally applicable than your thread title suggests.
And for that general Use Case it more easy to setup than your post suggests, because there is no need to go anywhere need the Windows task Scheduler.
Want a dark skin for MM5? This is the one that works best for me .. elegant, compact & clear.
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

Barry4679 wrote: Sat Feb 10, 2024 12:26 am I have 65 bit OS.
MM5 installed in the default location.
The Windows key + R test works OK, ie. MM5 starts
Also as I said before the script works as advertised if I run it in the PowerShell ISE or via the "Run with PowerShell" option in the content menu for the script file.

Must be something I don't know about a running a task manually from the Task Scheduler, or how to create a task inside the scheduler w/o a trigger, or a maybe it is a rights issue.
I don't need an answer to this, I was just trying to springboard into the an evaluation of the Task Scheduler, arising from your posting.
Okay.

You are having a problem with a single line in the script:

Code: Select all

Start-Process -FilePath "C:\Program Files (x86)\MediaMonkey 5\MediaMonkey.exe" -ArgumentList "/NoSplash /Play" -ErrorAction SilentlyContinue
Enter PowerShell from within Windows without MMW5 running and paste versions of that line to diagnose the issue, if you wish to use the script.

For example, try this and look at the output from PowerShell:

Code: Select all

Start-Process -FilePath "C:\Program Files (x86)\MediaMonkey 5\MediaMonkey.exe" -ArgumentList "/NoSplash /Play"
I just performed this and I saw MMW5 launch with no splash screen and immediately play music (as should happen for you too, since you installed in the default location):

https://www.dropbox.com/scl/fi/rzisgc6f ... skuzg&dl=0

Since it is not working for you, do you see any error messages?
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

Barry4679 wrote: Sat Feb 10, 2024 12:26 am The point that I was trying to make was that your script appears to be more generally applicable than your thread title suggests.
And for that general Use Case it more easy to setup than your post suggests, because there is no need to go anywhere need the Windows task Scheduler.
I shared this script to show how to automate this. You certainly do not have to schedule a task to run. You may simply double click the ps1 file and manually execute it, if you wish.

This script took a mere couple of minutes to create. The world is welcome to use it for anything they wish.
Barry4679
Posts: 2439
Joined: Fri Sep 11, 2009 8:07 am
Location: Australia
Contact:

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Barry4679 »

The task restarts MM5 OK and I get no error messages when I run it using the instructions in your last post today, ie. in Powershell

But when I run it from the Task Scheduler manually:
  • the task never completes
  • MM5 closes
  • MM5 does not reopen onto the desktop
  • the Task History panel looks normal ... no error messages : Action Started -> Task Triggered By User -> Action Complete -> Task Completed
  • It does start 4 new MM background processes, but there is no GUI
Maybe it is starting as a service? Except I hear no music restarting

I will send you a PM with images of the Task configuration.
I guess that I screwed up somewhere.
Want a dark skin for MM5? This is the one that works best for me .. elegant, compact & clear.
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

Please double check your Task Scheduler settings. This was the main reason I posted exact screenshots. Make sure your screens match exactly.
Barry4679
Posts: 2439
Joined: Fri Sep 11, 2009 8:07 am
Location: Australia
Contact:

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Barry4679 »

Thanks Dryst, for helping me fix my typo via PM.
It works correctly now.
Thanks for the helpful script.
Want a dark skin for MM5? This is the one that works best for me .. elegant, compact & clear.
Dryst
Posts: 261
Joined: Sun Dec 19, 2021 7:27 am

Re: Scheduling Windows 10/11 PowerShell Script For Continuous Playback

Post by Dryst »

One quick update I noticed--

If you uninstall MMW5 followed by reinstalling it later (even if you have MMW4 installed), the install location will change. Instead of

"C:\Program Files (x86)\MediaMonkey 5\MediaMonkey.exe"

The install location has been moved to

"C:\Program Files (x86)\Ventis\MediaMonkey\MediaMonkey.exe""

On my machine, version 4 still exists at

"C:\Program Files (x86)\MediaMonkey\MediaMonkey.exe"

Due to this, please confirm your actual install locations if the script does not work for you.
Post Reply