2

Audio Player Demo & Source Code – Fyne Golang Gui tutorial 67

Audio Player & Demo Source Code – Fyne Golang Gui tutorial 67

#Source Code#

package main
import (
    "fmt"
    "os"
    "time"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/canvas"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/dialog"
    "fyne.io/fyne/v2/storage"
    "fyne.io/fyne/v2/theme"
    "fyne.io/fyne/v2/widget"
    "github.com/faiface/beep"
    "github.com/faiface/beep/mp3"
    "github.com/faiface/beep/speaker"
)
var f *os.File
var format beep.Format
var streamer beep.StreamSeekCloser
var pause bool = false
func main() {
    go func(msg string) {
        fmt.Println(msg)
        if streamer == nil {
        } else {
            //slider.Value = float64(streamer.Position())
            fmt.Println(fmt.Sprint(streamer.Len()))
        }
    }("going")
    time.Sleep(time.Second)
    a := app.New()
    w := a.NewWindow("audio player...")
    w.Resize(fyne.NewSize(400, 400))
    logo := canvas.NewImageFromFile("logo.png")
    logo.FillMode = canvas.ImageFillOriginal
    toolbar := widget.NewToolbar(
        widget.NewToolbarSpacer(),
        widget.NewToolbarAction(theme.MediaPlayIcon(), func() {
            // f, _ = os.Open("hen.mp3")
            speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
            speaker.Play(streamer)
        }),
        widget.NewToolbarAction(theme.MediaPauseIcon(), func() {
            if !pause {
                pause = true
                speaker.Lock()
            } else if pause {
                pause = false
                speaker.Unlock()
            }
        }),
        widget.NewToolbarAction(theme.MediaStopIcon(), func() {
            speaker.Clear()
            // speaker.Close()
        }),
        widget.NewToolbarSpacer(),
    )
    label := widget.NewLabel("Audio MP3..")
    label.Alignment = fyne.TextAlignCenter
    label2 := widget.NewLabel("Play MP3..")
    label2.Alignment = fyne.TextAlignCenter
    browse_files := widget.NewButton("Browse...", func() {
        fd := dialog.NewFileOpen(func(uc fyne.URIReadCloser, _ error) {
            streamer, format, _ = mp3.Decode(uc)
            label2.Text = uc.URI().Name()
            label2.Refresh()
        }, w)
        fd.Show()
        fd.SetFilter(storage.NewExtensionFileFilter([]string{".mp3"}))
    })
    // slider := widget.NewSlider(0, 100)
    c := container.NewVBox(label, browse_files, label2, toolbar)
    w.SetContent(
        container.NewBorder(logo, nil, nil, nil, c),
    )
    w.ShowAndRun()
}

 

Download Code

Fyne Golang GUI Course

 

Improved fyne documentation with videos.
Fyne Audio Player DEMO & Source is another addition in our fyne examples series.
fyne widgets taught with the help of interactive videos.
Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 2 comments
Ramu - 4 weeks ago

bro i am getting this error i have tried many times and search on google but unable to fix it . if you know what i do to correct this please suggest

go run main.go
/home/ramanand/go/src/github.com/faiface/beep/mp3/decode.go:9:2: cannot find package “github.com/hajimehoshi/go-mp3” in any of:
/usr/lib/go-1.13/src/github.com/hajimehoshi/go-mp3 (from $GOROOT)
/home/ramanand/go/src/github.com/hajimehoshi/go-mp3 (from $GOPATH)
/home/ramanand/go/src/github.com/faiface/beep/speaker/speaker.go:8:2: cannot find package “github.com/hajimehoshi/oto” in any of:
/usr/lib/go-1.13/src/github.com/hajimehoshi/oto (from $GOROOT)
/home/ramanand/go/src/github.com/hajimehoshi/oto (from $GOPATH)
/home/ramanand/go/src/github.com/faiface/beep/mp3/decode.go:10:2: cannot find package “github.com/pkg/errors” in any of:
/usr/lib/go-1.13/src/github.com/pkg/errors (from $GOROOT)
/home/ramanand/go/src/github.com/pkg/errors (from $GOPATH)

Reply
    Tony BB - 4 weeks ago

    Greetings. Hope you are doing great.
    You need to import packages first.

    Run these 2 commands for importing packages.

    go get github.com/hajimehoshi/go-mp3
    go get github.com/hajimehoshi/oto

    Reply

Leave a Reply: