From 52f0bd7e214e9c40cba3c7424243218a69934527 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 17 Dec 2018 02:06:51 +0900 Subject: [PATCH] examples/pcm: Bug fix: audio.Player refs should be held not to be GCed Now audio.Player can be GCed after #746 was fixed. --- examples/pcm/main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/pcm/main.go b/examples/pcm/main.go index 1c0f54878..505bf5f45 100644 --- a/examples/pcm/main.go +++ b/examples/pcm/main.go @@ -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. func toBytes(l, r []int16) []byte { if len(l) != len(r) { @@ -127,6 +130,7 @@ func playNote(scoreIndex int) rune { p, _ := audio.NewPlayerFromBytes(audioContext, toBytes(l, r)) p.Play() + players[p] = struct{}{} return rune(note) } @@ -146,6 +150,18 @@ func update(screen *ebiten.Image) error { } 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() { return nil }