Advertisement
Guest User

Untitled

a guest
Apr 29th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module AudioDeviceCmdlets
  2.  
  3. $preferredOutputDevice = "" # ID for default speakers
  4. $preferredInputDevice  = "" # ID for default microphone
  5. $steamLinkOutputDevice = "" # ID for VR speakers
  6. $steamLinkInputDevice  = "" # ID for VR microphone
  7.  
  8. $steamVRProcessName = "vrmonitor"
  9.  
  10. function IsSteamVRActive {
  11.     try {
  12.         $process = Get-Process -Name $steamVRProcessName -ErrorAction Stop
  13.         return $true
  14.     } catch {
  15.         return $false
  16.     }
  17. }
  18.  
  19. $audioSetForVR = $false
  20. $audioSetForNormal = $false
  21.  
  22. while ($true) {
  23.     try {
  24.         $steamVRActive = IsSteamVRActive
  25.  
  26.         if ($steamVRActive) {
  27.             if (-not $audioSetForVR) {
  28.                 Start-Sleep -Seconds 5
  29.                 Write-Host "Attempting to switch to VR devices..." -ForegroundColor Cyan
  30.                 try {
  31.                     Set-AudioDevice -ID $steamLinkOutputDevice
  32.                     Set-AudioDevice -ID $steamLinkInputDevice
  33.                     Write-Host "Successfully switched to VR devices." -ForegroundColor Green
  34.                     $audioSetForVR = $true
  35.                     $audioSetForNormal = $false
  36.                 } catch {
  37.                     Write-Host "Failed to switch to VR devices, will retry..." -ForegroundColor Yellow
  38.                 }
  39.             }
  40.         } else {
  41.             if (-not $audioSetForNormal) {
  42.                 Write-Host "Attempting to switch back to preferred devices..." -ForegroundColor Cyan
  43.                 try {
  44.                     Set-AudioDevice -ID $preferredOutputDevice
  45.                     Set-AudioDevice -ID $preferredInputDevice
  46.                     Write-Host "Successfully switched to preferred devices." -ForegroundColor Green
  47.                     $audioSetForNormal = $true
  48.                     $audioSetForVR = $false
  49.                 } catch {
  50.                     Write-Host "Failed to switch to preferred devices, will retry..." -ForegroundColor Yellow
  51.                 }
  52.             }
  53.         }
  54.  
  55.     } catch {
  56.         Write-Host "Unexpected error during monitoring: $_" -ForegroundColor Magenta
  57.     }
  58.  
  59.     Start-Sleep -Seconds 2
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement