Visual Basic中SoundPlayer播放资源音乐及后台播放、悬停切换实现
Hey there! Let's get your music playback working correctly in your VB app. I see two main issues here: incorrectly loading music from resources and implementing the hover/background playback switch. Let's break this down step by step.
1. Correctly Load Music from Embedded Resources
Your current code tries to assign My.Resources.backgroundlagu to a string variable, but that's wrong—when you add a WAV file as an embedded resource, My.Resources.[ResourceName] returns a Stream object, not a file path string. Here's how to fix that:
First, ensure your music files are set up properly as embedded resources:
- Right-click your project → Add → Existing Item → Select your WAV files.
- In the Properties window for each file, set Build Action to
Embedded Resource.
Then, initialize your
SoundPlayerinstances directly with the resource streams:' Declare global SoundPlayer instances (outside any sub) Private backgroundPlayer As New Media.SoundPlayer(My.Resources.backgroundlagu) Private hoverPlayer As New Media.SoundPlayer(My.Resources.hoverMusic) ' Replace with your hover music resource name
2. Implement the Full Playback Logic
We need three core behaviors:
- Start looping background music when the form loads
- Switch to hover music (and stop background) when the mouse enters the button
- Switch back to background music (and stop hover) when the mouse leaves the button
- Stop all music (or trigger game-specific audio) when the button is clicked to enter the game
Here's the complete commented code example:
Public Class Form1 ' Global SoundPlayer instances for background and hover music Private backgroundPlayer As New Media.SoundPlayer(My.Resources.backgroundlagu) Private hoverPlayer As New Media.SoundPlayer(My.Resources.hoverMusic) ' Update this to your actual hover resource name Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Start background music on loop when the form opens backgroundPlayer.PlayLooping() End Sub ' Triggered when mouse enters the "Start Game" button Private Sub btnStartGame_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStartGame.MouseEnter ' Stop background music before playing hover audio backgroundPlayer.Stop() ' Play hover music (use PlayLooping() if you want it to repeat while hovered) hoverPlayer.Play() End Sub ' Triggered when mouse leaves the "Start Game" button Private Sub btnStartGame_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStartGame.MouseLeave ' Stop hover music hoverPlayer.Stop() ' Resume background music loop backgroundPlayer.PlayLooping() End Sub ' Triggered when the "Start Game" button is clicked Private Sub btnStartGame_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStartGame.Click ' Stop all active music before entering the game backgroundPlayer.Stop() hoverPlayer.Stop() ' Add your game entry logic here (e.g., open a game form, start game-specific audio) ' Example: ' GameMainForm.Show() ' Me.Hide() End Sub ' Clean up players when the form closes to avoid lingering sounds Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing backgroundPlayer.Stop() hoverPlayer.Stop() backgroundPlayer.Dispose() hoverPlayer.Dispose() End Sub End Class
Key Notes to Keep in Mind
- SoundPlayer Limitations: This control only supports WAV files. If you need to play MP3, WMA, or other formats, you'll need to use the
WindowsMediaPlayercontrol (add it via the Toolbox → Components tab). - Resource Name Accuracy: Double-check that your resource names match exactly what you use in
My.Resources.[Name]—case sensitivity can cause issues here. - Loop Control: Use
Play()for one-time playback orPlayLooping()for continuous repetition, depending on your needs for each audio track.
内容的提问来源于stack exchange,提问作者Alvin Indra

