mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
examples/audio: Reuse SE audio.Player object
This commit is contained in:
parent
07968abfd6
commit
50b7ec59d4
@ -18,7 +18,6 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
@ -63,7 +62,7 @@ var (
|
||||
audioContext *audio.Context
|
||||
musicPlayer *Player
|
||||
seStream *wav.Stream
|
||||
seBuffer []byte
|
||||
sePlayer *audio.Player
|
||||
musicCh = make(chan *Player)
|
||||
seCh = make(chan *wav.Stream)
|
||||
mouseButtonState = map[ebiten.MouseButton]int{}
|
||||
@ -98,17 +97,17 @@ func (p *Player) updateSE() error {
|
||||
if keyState[ebiten.KeyP] != 1 {
|
||||
return nil
|
||||
}
|
||||
// Clone the buffer so that we can play the same SE mutiple times.
|
||||
// TODO(hajimehoshi): This consumes memory. Can we avoid this?
|
||||
if seBuffer == nil {
|
||||
b, err := ioutil.ReadAll(seStream)
|
||||
if sePlayer == nil {
|
||||
var err error
|
||||
sePlayer, err = audioContext.NewPlayer(seStream)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
seBuffer = b
|
||||
}
|
||||
sePlayer, err := audioContext.NewPlayer(&SEStream{bytes.NewReader(seBuffer)})
|
||||
if err != nil {
|
||||
if sePlayer.IsPlaying() {
|
||||
return nil
|
||||
}
|
||||
if err := sePlayer.Rewind(); err != nil {
|
||||
return err
|
||||
}
|
||||
return sePlayer.Play()
|
||||
|
Loading…
Reference in New Issue
Block a user