Tin tức và phân tích của tất cả các thiết bị di động

Cách tự động tắt âm thanh khi ngắt kết nối tai nghe trong hệ thống Windows 10

Hệ thống Windows 10 có thể lưu trữ các cấu hình âm thanh riêng biệt cho các thiết bị âm thanh khác nhau. Bạn có thể đặt mức âm lượng khác nhau cho từng thiết bị âm thanh được kết nối và âm lượng sẽ tự động điều chỉnh khi bạn kết nối thiết bị. Tất nhiên, không ai tắt tiếng thiết bị âm thanh mọi lúc. Họ tăng hoặc giảm âm lượng, nhưng không ai thường tắt tiếng thiết bị âm thanh.

Nếu bạn sử dụng một cặp tai nghe với máy tính của mình và thường xuyên phải ngắt kết nối chúng, bạn có thể sử dụng một tập lệnh PowerShell nhỏ sẽ tự động tắt âm thanh khi bạn ngắt kết nối tai nghe.

Đây là điều mà điện thoại di động làm, tức là khi bạn ngắt kết nối tai nghe, nhạc sẽ tự động dừng. Lý do là bạn đã nghe nhạc xong hoặc vô tình tháo tai nghe ra và cần một cách nhanh chóng để tắt chúng đi. Về cơ bản, kịch bản được viết theo nguyên tắc tương tự bởi Prateek Singh của GEEEEEFY.

Tự động tắt tiếng

Mở Notepad và dán đoạn sau;

[cmdletbinding()]
Param()

#Adding definitions for accessing the Audio API
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
// f(), g(), ... are unused COM method slots. Define these if you care
int f(); int g(); int h(); int i();
int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
int j();
int GetMasterVolumeLevelScalar(out float pfLevel);
int k(); int l(); int m(); int n();
int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
int f(); // Unused
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio {
static IAudioEndpointVolume Vol() {
var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
IMMDevice dev = null;
Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
IAudioEndpointVolume epv = null;
var epvid = typeof(IAudioEndpointVolume).GUID;
Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
return epv;
}
public static float Volume {
get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
}
public static bool Mute {
get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
}
}
'@ -Verbose


While($true)
{
#Clean all events in the current session since its in a infinite loop, to make a fresh start when loop begins
Get-Event | Remove-Event -ErrorAction SilentlyContinue

#Registering the Event and Waiting for event to be triggered
Register-WmiEvent -Class Win32_DeviceChangeEvent
Wait-Event -OutVariable Event |Out-Null

$EventType = $Event.sourceargs.newevent | `
Sort-Object TIME_CREATED -Descending | `
Select-Object EventType -ExpandProperty EventType -First 1

#Conditional logic to handle, When to Mute/unMute the machine using Audio API
If($EventType -eq 3) 
{
[Audio]::Mute = $true
Write-Verbose "Muted [$((Get-Date).tostring())]"
}
elseif($EventType -eq 2 -and [Audio]::Mute -eq $true)
{
[Audio]::Mute = $false
Write-Verbose "UnMuted [$((Get-Date).tostring())]"
}
}

Lưu nó với phần mở rộng PS1. Đảm bảo bạn chọn “Tất cả tệp” từ danh sách thả xuống loại tệp. Đặt tên cho tệp ngay lập tức cho bạn biết nó dùng để làm gì. Lưu nó ở nơi mà bạn không có khả năng vô tình xóa nó, nhưng cũng là nơi bạn có thể dễ dàng tìm thấy nó nếu cần.

Chạy kịch bản

PowerShell không thể tự động chạy tập lệnh. Có một biện pháp bảo mật tích hợp ngăn không cho nó làm như vậy, nhưng có một cách để giải quyết vấn đề đó. Chúng tôi có một bài viết chi tiết về cách bạn có thể làm điều này. Làm theo hướng dẫn để tự động chạy tập lệnh PowerShell mà bạn vừa tạo và sử dụng tác vụ đã lên lịch để chạy tập lệnh mỗi khi bạn khởi động máy tính.

Ngoài ra, bạn có thể chạy tập lệnh theo cách thủ công khi khởi động hệ thống. Tôi sử dụng nó chưa đầy 30 phút và tôi không biết mình đã sống như thế nào nếu không có nó trước đây.