From 8086afb6b58bfe8d11ffffaf0b3b0482457a70e1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 21 Mar 2016 01:38:15 +0900 Subject: [PATCH] examples/audio: Toggle play/pause --- examples/audio/main.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/examples/audio/main.go b/examples/audio/main.go index 8a58ed77d..ec05fb7aa 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -61,6 +61,7 @@ var ( player *Player playerCh = make(chan *Player) mouseButtonState = map[ebiten.MouseButton]int{} + keyState = map[ebiten.Key]int{} ) func playerBarRect() (x, y, w, h int) { @@ -70,6 +71,24 @@ func playerBarRect() (x, y, w, h int) { return } +func (p *Player) updatePlayPause() error { + if p.audioPlayer == nil { + return nil + } + if !ebiten.IsKeyPressed(ebiten.KeyS) { + keyState[ebiten.KeyS] = 0 + return nil + } + keyState[ebiten.KeyS]++ + if keyState[ebiten.KeyS] != 1 { + return nil + } + if p.audioPlayer.IsPlaying() { + return p.audioPlayer.Pause() + } + return p.audioPlayer.Play() +} + func (p *Player) updateBar() error { if p.audioPlayer == nil { return nil @@ -106,14 +125,17 @@ func update(screen *ebiten.Image) error { if err := player.updateBar(); err != nil { return err } + if err := player.updatePlayPause(); err != nil { + return err + } } op := &ebiten.DrawImageOptions{} x, y, w, h := playerBarRect() op.GeoM.Translate(float64(x), float64(y)) screen.DrawImage(playerBarImage, op) - currentTimeStr := "" - if player != nil && player.audioPlayer.IsPlaying() { + currentTimeStr := "00:00" + if player != nil { c := player.audioPlayer.Current() // Current Time @@ -131,6 +153,7 @@ func update(screen *ebiten.Image) error { } msg := fmt.Sprintf(`FPS: %0.2f +Press S to toggle Play/Pause %s`, ebiten.CurrentFPS(), currentTimeStr) if player == nil { msg += "\nNow Loading..."