mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
examples/audio: Copy SE buffer only once
This commit is contained in:
parent
27911b8156
commit
457581c8ad
@ -18,7 +18,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
@ -63,6 +63,7 @@ var (
|
||||
audioContext *audio.Context
|
||||
musicPlayer *Player
|
||||
seStream *wav.Stream
|
||||
seBuffer []byte
|
||||
musicCh = make(chan *Player)
|
||||
seCh = make(chan *wav.Stream)
|
||||
mouseButtonState = map[ebiten.MouseButton]int{}
|
||||
@ -88,15 +89,14 @@ func (p *Player) updateSE() error {
|
||||
if keyState[ebiten.KeyP] != 1 {
|
||||
return nil
|
||||
}
|
||||
// Clone the stream
|
||||
b := &bytes.Buffer{}
|
||||
if _, err := seStream.Seek(0, 0); err != nil {
|
||||
if seBuffer == nil {
|
||||
b, err := ioutil.ReadAll(seStream)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(b, seStream); err != nil {
|
||||
return err
|
||||
seBuffer = b
|
||||
}
|
||||
sePlayer, err := audioContext.NewPlayer(bytes.NewReader(b.Bytes()))
|
||||
sePlayer, err := audioContext.NewPlayer(bytes.NewReader(seBuffer))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user