faiface / beep

A little package that brings sound to any Go application. Suitable for playback and audio-processing.
MIT License
2.08k stars 151 forks source link

bug: stuck in loop #142

Closed islishude closed 2 years ago

islishude commented 2 years ago

MacOS 12.2.1 Go 1.18

package main

import (
    "log"
    "os"
    "time"

    "github.com/faiface/beep"
    "github.com/faiface/beep/mp3"
    "github.com/faiface/beep/speaker"
)

func main() {
    for i := 0; i < 10; i++ {
        play()
    }
}

func play() {
    f, err := os.Open("music.mp3")
    if err != nil {
        log.Fatal(err)
    }

    streamer, format, err := mp3.Decode(f)
    if err != nil {
        log.Fatal(err)
    }
    defer streamer.Close()

    _ = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
    done := make(chan bool)
    speaker.Play(beep.Seq(streamer, beep.Callback(func() {
        done <- true
    })))
    <-done
}