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

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

#Source Code#

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


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 - a couple of years 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)

    Tony BB - a couple of years 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


Leave a Reply: