mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-23 17:32:02 +01:00
examples/pcm: Bug fix: audio.Player refs should be held not to be GCed
Now audio.Player can be GCed after #746 was fixed.
This commit is contained in:
parent
5d0420cea0
commit
52f0bd7e21
@ -83,6 +83,9 @@ func square(out []int16, volume float64, freq float64, sequence float64) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// players holds audio.Player objects not to be GCed.
|
||||||
|
var players = map[*audio.Player]struct{}{}
|
||||||
|
|
||||||
// toBytes returns the 2ch little endian 16bit byte sequence with the given left/right sequence.
|
// toBytes returns the 2ch little endian 16bit byte sequence with the given left/right sequence.
|
||||||
func toBytes(l, r []int16) []byte {
|
func toBytes(l, r []int16) []byte {
|
||||||
if len(l) != len(r) {
|
if len(l) != len(r) {
|
||||||
@ -127,6 +130,7 @@ func playNote(scoreIndex int) rune {
|
|||||||
|
|
||||||
p, _ := audio.NewPlayerFromBytes(audioContext, toBytes(l, r))
|
p, _ := audio.NewPlayerFromBytes(audioContext, toBytes(l, r))
|
||||||
p.Play()
|
p.Play()
|
||||||
|
players[p] = struct{}{}
|
||||||
|
|
||||||
return rune(note)
|
return rune(note)
|
||||||
}
|
}
|
||||||
@ -146,6 +150,18 @@ func update(screen *ebiten.Image) error {
|
|||||||
}
|
}
|
||||||
frames++
|
frames++
|
||||||
|
|
||||||
|
// Close players that alrady finish.
|
||||||
|
closed := []*audio.Player{}
|
||||||
|
for p := range players {
|
||||||
|
if p.IsPlaying() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
closed = append(closed, p)
|
||||||
|
}
|
||||||
|
for _, p := range closed {
|
||||||
|
delete(players, p)
|
||||||
|
}
|
||||||
|
|
||||||
if ebiten.IsDrawingSkipped() {
|
if ebiten.IsDrawingSkipped() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user