Buenas!.
Es un juego infantil que hice para resolver una duda que tengo con los sonidos.
A ver si podeís ayudarme.
- Se hace clic en el botón de reproducir el sonido y luego se hace clic en el animal que crees que hace ese sonido.
Como es lógico para un adulto no tiene problema. Ok, cuando se acierta se escucharán los aplausos.
Y ahora viene la pregunta. Si le doy a reproducir sonido de animal, escucharé dos sonidos a la vez.
Los aplausos y el del animal.
¿ Cómo se podría hacer para que los aplausos cesasen y solo se escuchase el sonido del animal ?.
En realidad hice este ejemplo con la escusa de resolver esta duda. Lo necesito para otro ejemplo.
El código fuente:
FMain:
Código:
' Gambas class file
Const MENSAJE As String = "Haz clic en la imagen del animal que hace ese soindo"
Private canimales As New Collection
Private rutaimagenes As String
Private rutasonidos As String
Private sonidoerror As Sound
Private aplausos As Sound
Private adivinaanimal As String
Public Sub Form_Open()
Label1.text = MENSAJE
Me.Text = "Adivina el animal"
rutaimagenes = Application.Path &/ "Imagenes/"
rutasonidos = Application.Path &/ "Sonidos/"
sonidoerror = Sound.Load(Application.Path &/ "Sonidos/error.ogg")
aplausos = Sound.Load(Application.Path &/ "Sonidos/aplausosfin.ogg")
CreaAnimales()
Randomize
DistribuirAnimales
Timer1.Delay = 3000
Panel1.Enabled = False
End
Public Sub CreaAnimales()
CreaObjetoAnimal(rutasonidos, rutaimagenes, "01_cat.png", "gato.wav", "gato")
CreaObjetoAnimal(rutasonidos, rutaimagenes, "02_pig.png", "cerdo.ogg", "cerdo")
CreaObjetoAnimal(rutasonidos, rutaimagenes, "04_hippopotamus.png", "hipopotamo.ogg", "hipopotamo")
CreaObjetoAnimal(rutasonidos, rutaimagenes, "10_chicken.png", "gallina.ogg", "gallina")
End
Public Sub CreaObjetoAnimal(rutasonidos As String, rutaimagenes As String, dibujo As String, sonido As String, nombre As String)
Dim animal As New Animal
With animal
.dibujo = Picture.Load(rutaimagenes & dibujo)
.sonido = rutasonidos & sonido
.nombre = nombre
End With
canimales.Add(animal, animal.nombre)
End
Public Function DevolverListaNombres(clista As Collection) As String[]
Dim lista As New String[]
Dim animal As Animal
For Each animal In clista
lista.Add(animal.nombre)
Next
Return lista
End
Public Sub DistribuirAnimales()
Dim unpicturebox As PictureBox
Dim aanimales As String[]
Dim azar As Integer
Dim fondo As Integer[] = [Color.red, Color.green, Color.Blue, Color.Yellow]
aanimales = DevolverListaNombres(canimales)
For Each unpicturebox In Panel1.Children
If unpicturebox Is PictureBox Then
azar = Rand(aanimales.Max)
With unpicturebox
.Mode = PictureBox.Fill
.Tag = aanimales[azar]
.Picture = canimales[aanimales[azar]].dibujo
.Background = fondo[azar]
End With
aanimales.Remove(azar)
fondo.Remove(azar)
Endif
Next
Panel1.Enabled = False
End
Public Sub ImagenesDeAnimales_MouseDown()
If Last.tag = adivinaanimal Then
Label1.Text = "Correcto!"
aplausos.Play()
Else
Label1.Text = "Incorrecto!"
sonidoerror.Play()
Endif
Panel1.Enabled = False
Timer1.Start()
End
Public Sub btnReproduce_Click()
Dim lista As String[]
Dim azar As Integer
Panel1.Enabled = True
lista = DevolverListaNombres(canimales)
azar = Rand(lista.Max)
adivinaanimal = lista[azar]
canimales[adivinaanimal].ReproducirSonido()
End
Public Sub Timer1_Timer()
Label1.Text = MENSAJE
DistribuirAnimales()
Timer1.Stop
End
Clase Animal:
Código:
Property nombre As String Use $nombre
Property sonido As String Use $sonido
Property dibujo As Picture Use $dibujo
Public Sub ReproducirSonido()
Dim ssonido As Sound
ssonido = Sound.Load(Me.sonido)
ssonido.Play()
End
Os subo el ejemplo del juego de los sonidos.
¿ Qué me podéis aconsejar cuando trabaje con sonidos y uso el componente gb.sdl2.audio ?.
Saludos