Powershellで再生デバイスを切り替える
概要
AudioDeviceCmdletsというPowershellでAudioDeviceを操作するためのModuleを使って,GUIではなくPowershellから再生デバイスを変更できるようにします.
手順
Module
AudioDeviceCmdlets
のインストール.管理者権限のpowershellで実行します.❯ Install-Module -Name AudioDeviceCmdlets -V -Force
-Force
オプションなしだと自分の環境だと失敗しました.原因はよくわかっていませんがインストールがスキップされてしまいます.❯ Install-Module -Name AudioDeviceCmdlets -V ... VERBOSE: The specified Location is 'https://www.powershellgallery.com/api/v2' and PackageManagementProvider is 'NuGet'. VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='AudioDeviceCmdlets'' for ''. VERBOSE: Total package yield:'1' for the specified package 'AudioDeviceCmdlets'. VERBOSE: Skipping installed module AudioDeviceCmdlets 3.0.0.4.
AudioDeviceCmdlets
のインポートインストールしただけではまだ使えないのでインポートします.
Get-Module
コマンドを叩いた時にAudioDeviceCmdlets
があれば,インポートに成功しています.# import module ❯ Import-Module AudioDeviceCmdlets # check module ❯ Get-Module ModuleType Version PreRelease Name ExportedCommands ---------- ------- ---------- ---- ---------------- Binary 3.0.0.4 AudioDeviceCmdlets {Get-AudioDevice, Set-AudioDevice, Write-AudioDevice} ...
AudioDeviceのリストを取得する.
インポートに成功したら各種Cmdletsが使えるようになるので,実際にAudioDeviceのリストを取得します. 使うCmdletsは
Get-AudioDevice -List
です.こんな感じに接続しているデバイスの情報を取得できます.
❯ Get-AudioDevice -List Index : 1 Default : False Type : Playback Name : Speakers (Realtek(R) Audio) ID : {0.0.0.00000000}.{52736375-a7ca-fdf8-3b5d-9218beaab35a} Device : CoreAudioApi.MMDevice ...
AudioDeviceを切り替える
先ほど取得した情報をもとに
Set-AudioDevice
を使って,デバイスを切り替えます.今回はindexをもとに切り替えます.❯ Set-AudioDevice -Index 3
コマンド実行後,再生デバイスが切り替わっているはずです.