examples/audio: Toggle play/pause

This commit is contained in:
Hajime Hoshi 2016-03-21 01:38:15 +09:00
parent 82788c45b0
commit 8086afb6b5

View File

@ -61,6 +61,7 @@ var (
player *Player player *Player
playerCh = make(chan *Player) playerCh = make(chan *Player)
mouseButtonState = map[ebiten.MouseButton]int{} mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{}
) )
func playerBarRect() (x, y, w, h int) { func playerBarRect() (x, y, w, h int) {
@ -70,6 +71,24 @@ func playerBarRect() (x, y, w, h int) {
return 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 { func (p *Player) updateBar() error {
if p.audioPlayer == nil { if p.audioPlayer == nil {
return nil return nil
@ -106,14 +125,17 @@ func update(screen *ebiten.Image) error {
if err := player.updateBar(); err != nil { if err := player.updateBar(); err != nil {
return err return err
} }
if err := player.updatePlayPause(); err != nil {
return err
}
} }
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
x, y, w, h := playerBarRect() x, y, w, h := playerBarRect()
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(playerBarImage, op) screen.DrawImage(playerBarImage, op)
currentTimeStr := "" currentTimeStr := "00:00"
if player != nil && player.audioPlayer.IsPlaying() { if player != nil {
c := player.audioPlayer.Current() c := player.audioPlayer.Current()
// Current Time // Current Time
@ -131,6 +153,7 @@ func update(screen *ebiten.Image) error {
} }
msg := fmt.Sprintf(`FPS: %0.2f msg := fmt.Sprintf(`FPS: %0.2f
Press S to toggle Play/Pause
%s`, ebiten.CurrentFPS(), currentTimeStr) %s`, ebiten.CurrentFPS(), currentTimeStr)
if player == nil { if player == nil {
msg += "\nNow Loading..." msg += "\nNow Loading..."