I am making a game engine in Visual Basic 6, and I am having problems with the sound. I can play any amount of MP3s, WAVs, etc fine, but as soon as I play 3 MIDIs, VB crashes. Here is the code I am using:
[code:3d0nmxra]Option Explicit
Public CurrentSong As String
Public System As Long
Public SoundX As Long
Public SongX As Long
Public Channel1 As Long
Public Channel2 As Long
Public Sub PlayMusic(Song As String)
Dim result As FMOD_RESULT
If Val(GetVar(App.Path & "\config.ini", "CONFIG", "Sound")) = 1 Then
If CurrentSong <> Song Then
CurrentSong = Song
result = FMOD_System_CreateSound(System, App.Path & "\Music\" & Song, FMOD_HARDWARE, SongX)
If result <> FMOD_OK Then Exit Sub
result = FMOD_Sound_SetMode(SongX, FMOD_LOOP_NORMAL)
If result <> FMOD_OK Then Exit Sub
result = FMOD_System_PlaySound(System, FMOD_CHANNEL_FREE, SongX, 0, Channel2)
If result <> FMOD_OK Then Exit Sub
End If
Else
Call StopMusic
End If
End Sub
Public Sub StopMusic()
Dim result As FMOD_RESULT
CurrentSong = ""
If SoundX Then
result = FMOD_Sound_Release(SoundX)
If result <> FMOD_OK Then Exit Sub
End If
If SongX Then
result = FMOD_Sound_Release(SongX)
If result <> FMOD_OK Then Exit Sub
End If
End Sub
Public Sub PlaySound(Sound As String)
Dim result As FMOD_RESULT
If Val(GetVar(App.Path & "\config.ini", "CONFIG", "Sound")) = 1 Then
result = FMOD_System_CreateSound(System, App.Path & "\SFX\" & Sound, FMOD_HARDWARE, SoundX)
If result <> FMOD_OK Then Exit Sub
result = FMOD_Sound_SetMode(SoundX, FMOD_LOOP_OFF)
If result <> FMOD_OK Then Exit Sub
result = FMOD_System_PlaySound(System, FMOD_CHANNEL_FREE, SoundX, 0, Channel1)
If result <> FMOD_OK Then Exit Sub
End If
End Sub
Public Sub StopSound()
Dim result As FMOD_RESULT
result = FMOD_Channel_SetPaused(Channel1, 1)
If result <> FMOD_OK Then Exit Sub
End Sub[/code:3d0nmxra]
I have also tried using software mixing, and I have the same problem. Remember, I don’t actually get an error. Visual Basic just crashes.
- Monoxide asked 12 years ago
- You must login to post comments
I suggest setting SoundX and SongX to 0 after releasing them so you don’t accidentally release them multiple times. I also suggest setting them to 0 before attempting to fill them with the createsound call, in case the call fails – VB can sometimes fail to call a native function for various reasons, and if it does that you might have an invalid handle sitting in your sound variable without an error code in your result variable for you to detect.
Try setting result to an error code before you make each call, so that you can be sure it will be FMOD_OK if it was successful.
You also don’t seem to be zeroing out the channel variables when the associated sound is stopped. IIRC, channel handles are not valid once their associated sound has stopped. I don’t think it’d crash if you used them (SetPaused, etc) but it’s probably worth making sure you don’t anyway.
- Janus answered 12 years ago
- You must login to post comments
Please login first to submit.