fix(ModMusic): 千年播放老问题

This commit is contained in:
tangge233
2025-03-15 01:24:57 +08:00
parent bf86b0732a
commit 482d6f71fc
+4 -83
View File
@@ -258,7 +258,7 @@ Public Module ModMusic
#End Region
''' <summary>
''' 当前正在播放的 NAudio.Wave.WaveOut。
''' 当前正在播放的 NAudio.Wave.WaveOutEvent。
''' </summary>
Public MusicNAudio = Nothing
''' <summary>
@@ -266,18 +266,17 @@ Public Module ModMusic
''' </summary>
Private MusicCurrent As String = ""
Dim deviceHandler As New AudioDeviceChangeHandler
''' <summary>
''' 在 MusicUuid 不变的前提下,持续播放某地址的音乐,且在播放结束后随机播放下一曲。
''' </summary>
Private Sub MusicLoop(Optional IsFirstLoad As Boolean = False)
Dim CurrentWave As NAudio.Wave.WaveOut = Nothing
Dim CurrentWave As NAudio.Wave.WaveOutEvent = Nothing
Dim Reader As NAudio.Wave.WaveStream = Nothing
Try
'开始播放
CurrentWave = New NAudio.Wave.WaveOut()
CurrentWave = New NAudio.Wave.WaveOutEvent()
MusicNAudio = CurrentWave
CurrentWave.DeviceNumber = -1
Reader = New NAudio.Wave.AudioFileReader(MusicCurrent)
CurrentWave.Init(Reader)
CurrentWave.Play()
@@ -331,82 +330,4 @@ Public Module ModMusic
End Try
End Sub
Public Class AudioDeviceChangeHandler
Implements Interfaces.IMMNotificationClient
Private deviceWatcher As MMDeviceEnumerator
Private callback As IMMNotificationClient
Public Sub New()
' 初始化音频设备枚举器
deviceWatcher = New MMDeviceEnumerator()
' 创建设备通知回调对象
callback = New DeviceNotificationClient(Me)
' 注册回调以接收音频设备更改通知
deviceWatcher.RegisterEndpointNotificationCallback(callback)
End Sub
' 实现IMMNotificationClient接口的方法以处理设备更改事件
Public Sub OnDefaultDeviceChanged(dataFlow As DataFlow, role As Role, defaultDeviceId As String) Implements IMMNotificationClient.OnDefaultDeviceChanged
' 处理默认音频设备更改事件
If MusicNAudio Is Nothing Then Exit Sub
' 切换到系统默认输出设备
Log($"音频默认输出设备切换到 {defaultDeviceId}")
Dim target As NAudio.Wave.WaveOut = MusicNAudio
target.Stop()
target.DeviceNumber = -1
target.Resume()
End Sub
Public Sub OnDeviceAdded(deviceId As String) Implements IMMNotificationClient.OnDeviceAdded
' 处理音频设备添加事件
If MusicNAudio Is Nothing Then Exit Sub
CType(MusicNAudio, NAudio.Wave.WaveOut).DeviceNumber = -1
End Sub
Public Sub OnDeviceRemoved(deviceId As String) Implements IMMNotificationClient.OnDeviceRemoved
' 处理音频设备移除事件
If MusicNAudio Is Nothing Then Exit Sub
CType(MusicNAudio, NAudio.Wave.WaveOut).DeviceNumber = -1
End Sub
Public Sub OnDeviceStateChanged(deviceId As String, newState As DeviceState) Implements IMMNotificationClient.OnDeviceStateChanged
' 处理音频设备状态更改事件
End Sub
Public Sub OnPropertyValueChanged(deviceId As String, key As PropertyKey) Implements IMMNotificationClient.OnPropertyValueChanged
' 处理音频设备属性值更改事件
End Sub
End Class
' 实现IMMNotificationClient接口的类
Friend Class DeviceNotificationClient
Implements IMMNotificationClient
Private container As AudioDeviceChangeHandler
Public Sub New(container As AudioDeviceChangeHandler)
Me.container = container
End Sub
Public Sub OnDefaultDeviceChanged(dataFlow As DataFlow, role As Role, defaultDeviceId As String) Implements IMMNotificationClient.OnDefaultDeviceChanged
container.OnDefaultDeviceChanged(dataFlow, role, defaultDeviceId)
End Sub
Public Sub OnDeviceAdded(deviceId As String) Implements IMMNotificationClient.OnDeviceAdded
container.OnDeviceAdded(deviceId)
End Sub
Public Sub OnDeviceRemoved(deviceId As String) Implements IMMNotificationClient.OnDeviceRemoved
container.OnDeviceRemoved(deviceId)
End Sub
Public Sub OnDeviceStateChanged(deviceId As String, newState As DeviceState) Implements IMMNotificationClient.OnDeviceStateChanged
container.OnDeviceStateChanged(deviceId, newState)
End Sub
Public Sub OnPropertyValueChanged(deviceId As String, key As PropertyKey) Implements IMMNotificationClient.OnPropertyValueChanged
container.OnPropertyValueChanged(deviceId, key)
End Sub
End Class
End Module