audio: Context.Update now returns error (#197)

This commit is contained in:
Hajime Hoshi 2016-04-05 00:09:00 +09:00
parent b6a02ddc94
commit f222885d97
5 changed files with 23 additions and 7 deletions

View File

@ -180,7 +180,9 @@ func (p *Player) close() error {
}
func update(screen *ebiten.Image) error {
audioContext.Update()
if err := audioContext.Update(); err != nil {
return err
}
if musicPlayer == nil {
select {
case musicPlayer = <-musicCh:

View File

@ -145,7 +145,9 @@ func addNote() error {
}
func update(screen *ebiten.Image) error {
audioContext.Update()
if err := audioContext.Update(); err != nil {
return err
}
defer func() {
frames++
}()

View File

@ -206,7 +206,9 @@ func init() {
}
func update(screen *ebiten.Image) error {
audioContext.Update()
if err := audioContext.Update(); err != nil {
return err
}
updateInput()
for i, key := range keys {
if keyStates[key] != 1 {

View File

@ -87,7 +87,9 @@ func (s *stream) Close() error {
var player *audio.Player
func update(screen *ebiten.Image) error {
audioContext.Update()
if err := audioContext.Update(); err != nil {
return err
}
if player == nil {
var err error
player, err = audioContext.NewPlayer(&stream{})

View File

@ -108,6 +108,7 @@ type Context struct {
stream *mixedPlayersStream
players map[*Player]struct{}
frames int
errorCh chan error
sync.Mutex
}
@ -116,6 +117,7 @@ func NewContext(sampleRate int) (*Context, error) {
c := &Context{
sampleRate: sampleRate,
players: map[*Player]struct{}{},
errorCh: make(chan error),
}
c.stream = &mixedPlayersStream{
context: c,
@ -133,8 +135,8 @@ func NewContext(sampleRate int) (*Context, error) {
break
}
if err != nil {
// TODO: Record the last error
panic(err)
c.errorCh <- err
return
}
time.Sleep(1 * time.Millisecond)
}
@ -148,10 +150,16 @@ func NewContext(sampleRate int) (*Context, error) {
// In sync mode, the game logical time syncs the audio logical time and
// you will find audio stops when the game stops e.g. when the window is deactivated.
// In unsync mode, the audio never stops even when the game stops.
func (c *Context) Update() {
func (c *Context) Update() error {
c.Lock()
defer c.Unlock()
select {
case err := <-c.errorCh:
return err
default:
}
c.frames++
return nil
}
// SampleRate returns the sample rate.