Skip to content

Commit

Permalink
[audio] Allows playback of both MP3 or WAV files, as long as the need…
Browse files Browse the repository at this point in the history
…ed player (mpg123 or aplay) is installed

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed May 15, 2016
1 parent 80adfcd commit eecb3d7
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions platforms/audio/audio_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package audio

import (
"errors"
"github.com/hybridgroup/gobot"
"log"
"os"
"os/exec"
"path"

"github.com/hybridgroup/gobot"
)

var _ gobot.Adaptor = (*AudioAdaptor)(nil)
Expand All @@ -30,25 +32,35 @@ func (a *AudioAdaptor) Finalize() []error { return nil }
func (a *AudioAdaptor) Sound(fileName string) []error {

var errorsList []error
var err error

if fileName == "" {
log.Println("Require filename for MP3 file.")
errorsList = append(errorsList, errors.New("Requires filename for MP3 file."))
log.Println("Require filename for audio file.")
errorsList = append(errorsList, errors.New("Requires filename for audio file."))
return errorsList
}

_, err = os.Open(fileName)
_, err := os.Stat(fileName)
if err != nil {
log.Println(err)
errorsList = append(errorsList, err)
return errorsList
}

// command to play a MP3 file
cmd := exec.Command("mpg123", fileName)
err = cmd.Start()
// command to play audio file based on file type
fileType := path.Ext(fileName)
var commandName string
if fileType == ".mp3" {
commandName = "mpg123"
} else if fileType == ".wav" {
commandName = "aplay"
} else {
log.Println("Unknown filetype for audio file.")
errorsList = append(errorsList, errors.New("Unknown filetype for audio file."))
return errorsList
}

cmd := exec.Command(commandName, fileName)
err = cmd.Start()
if err != nil {
log.Println(err)
errorsList = append(errorsList, err)
Expand Down

0 comments on commit eecb3d7

Please sign in to comment.