Skip to content

Commit

Permalink
Add a semaphore to PauseEvent
Browse files Browse the repository at this point in the history
Add an explicit Destroy method to the audio player
  • Loading branch information
remogatto committed Feb 13, 2014
1 parent d5a89a3 commit ab28fe0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
8 changes: 3 additions & 5 deletions callback_android.go
Expand Up @@ -90,7 +90,9 @@ func onPause(act *C.ANativeActivity) {
}()
Debugf("Pausing...\n")

event <- PauseEvent{unsafe.Pointer(act)}
paused := make(chan bool)
event <- PauseEvent{unsafe.Pointer(act), paused}
<-paused

// Shutdown the sound engine
shutdownOpenSL()
Expand Down Expand Up @@ -166,10 +168,6 @@ func onDestroy(act *C.ANativeActivity) {
}()
Debugf("onDestroy...\n")

// Shutdown the sound engine
Debugf("Shutdown OpenSL")
shutdownOpenSL()

// event <- DestroyEvent{unsafe.Pointer(act)}

Debugf("onDestroy done\n")
Expand Down
5 changes: 4 additions & 1 deletion events.go
Expand Up @@ -48,9 +48,12 @@ type NativeWindowRedrawNeededEvent struct {

// A PauseEvent is triggered when the application is paused. It
// happens, for example, when the back button is pressed and the
// application goes in background.
// application goes in background. Please note that the framework will
// wait for a value from Paused channel before actually pause the
// application.
type PauseEvent struct {
Activity unsafe.Pointer
Paused chan bool
}

// A ResumeEvent is triggered when the application goes
Expand Down
4 changes: 4 additions & 0 deletions sound.go
Expand Up @@ -31,3 +31,7 @@ func (ap *AudioPlayer) GetMaxVolumeLevel() (int, error) {
func (ap *AudioPlayer) SetVolumeLevel(value int) error {
return ap.ap.setVolumeLevel(value)
}

func (ap *AudioPlayer) Destroy() {
ap.ap.destroy()
}
20 changes: 4 additions & 16 deletions sound_android.go
Expand Up @@ -16,11 +16,6 @@ import (
// #cgo LDFLAGS: -landroid -lOpenSLES
import "C"

var (
rwPlayerMutex sync.RWMutex
bqPlayers []*audioPlayer
)

type bufferQueuePlayer struct {
bqPlayerObject C.SLObjectItf
bqPlayerPlay C.SLPlayItf
Expand All @@ -29,6 +24,7 @@ type bufferQueuePlayer struct {
}

type audioPlayer struct {
rwMutex sync.RWMutex
bqPlayer *bufferQueuePlayer
}

Expand All @@ -49,11 +45,6 @@ func initOpenSL() error {

func shutdownOpenSL() {
Debugf("Shutting down OpenSL ES")
rwPlayerMutex.RLock()
for _, bqPlayer := range bqPlayers {
bqPlayer.destroy()
}
rwPlayerMutex.RUnlock()
C.shutdownOpenSL()
}

Expand All @@ -64,11 +55,6 @@ func newAudioPlayer() (*audioPlayer, error) {
if result != C.SL_RESULT_SUCCESS {
return nil, fmt.Errorf("Error %d occured trying to create a buffer queue player", result)
}

rwPlayerMutex.Lock()
bqPlayers = append(bqPlayers, ap)
rwPlayerMutex.Unlock()

return ap, nil
}

Expand All @@ -77,8 +63,10 @@ func (ap *audioPlayer) play(buffer []byte, doneCh chan bool) {
}

func (ap *audioPlayer) destroy() {
Debugf("Destroying audio player %q", ap)
Debugf("Destroying audio player at 0x%x", ap)
ap.rwMutex.Lock()
C.destroyBufferQueueAudioPlayer((*C.t_buffer_queue_ap)(ap.bqPlayer))
ap.rwMutex.Unlock()
}

func (ap *audioPlayer) enqueue(buffer []byte) {
Expand Down

0 comments on commit ab28fe0

Please sign in to comment.