1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-12 18:50:42 +00:00
OpenDiablo2/Sound/SoundEffect.go
2019-10-26 20:09:33 -04:00

39 lines
727 B
Go

package Sound
import (
"log"
"github.com/hajimehoshi/ebiten/audio/wav"
"github.com/essial/OpenDiablo2/Common"
"github.com/hajimehoshi/ebiten/audio"
)
type SoundEffect struct {
player *audio.Player
}
func CreateSoundEffect(sfx string, fileProvider Common.FileProvider, context *audio.Context, volume float64) *SoundEffect {
result := &SoundEffect{}
audioData := fileProvider.LoadFile(sfx)
d, err := wav.Decode(context, audio.BytesReadSeekCloser(audioData))
if err != nil {
log.Fatal(err)
}
player, err := audio.NewPlayer(context, d)
if err != nil {
log.Fatal(err)
}
player.SetVolume(volume)
result.player = player
return result
}
func (v *SoundEffect) Play() {
v.player.Rewind()
v.player.Play()
}