Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sound on/off

Status
Not open for further replies.

21091958

Programmer
Aug 21, 2005
20
GB
Hi
I have a game with several wav.files, which play whenever a player lands in a specific area of a board.
I would like to give the possibility to the player to turn the sound off and on using a menu. i have tried this code but all i get An unhandled exception
Help welcome
Thank you

Private Sub mnuSound_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSound.Click
If mnuSound.Checked = True Then
mnuSound.Checked = False

ElseIf mnuSound.Checked = False Then
mnuSound.Checked = True
Sounds.PlayWavResource("m16")
Sounds.PlayWavResource("Heli")
etc.
End If
End Sub
 
It returns an error when i toggle more than once on/off, off/on menu
The error is in the class sound itself
An unhandled exception of type 'System.NullReferenceException' occurred in MyGame.exe
Additional information: Object reference not set to an instance of an object.

Anyway the problem is deeeper than this, the code does not work at all, I cannot turn yje sound off at all. Ideas welcome

imports System.IO
Public Class Sounds

Public Const SND_ASYNC As Int32 = 1
Public Const SND_MEMORY As Int32 = 4
Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySound" (ByVal data() As Byte, _
ByVal hMod As IntPtr, ByVal dwFlags As Int32) As Boolean
Public Shared Function PlayWavResource(ByVal wav As String)
Dim strNameSpace As String = System.Reflection.Assembly. _
GetExecutingAssembly().GetName().Name.ToString()
Dim Str As Stream = System.Reflection.Assembly.GetExecutingAssembly(). _
GetManifestResourceStream(strNameSpace & "." & wav)

If Str.CanSeek Then

Dim bStr(Str.Length) As Byte
Str.Read(bStr, 0, Int(Str.Length))

PlaySound(bStr, IntPtr.Zero, SND_ASYNC Or SND_MEMORY)
End If
End Function

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top