beep
beep copied to clipboard
Error getting MP3 file duration
func Mp3Time(){
byteBody, anyErr := ioutil.ReadFile("./test.mp3")
if anyErr != nil {
fmt.Printf("ReadFile err:%v\n", anyErr)
return
}
fmt.Printf("len byteMp3:%v\n", len(string(byteBody)))
streamer, format, anyErr := mp3.Decode(ioutil.NopCloser(bytes.NewBuffer(byteBody)))
if anyErr != nil {
fmt.Printf("Decode anyErr:%v", anyErr)
return
}
defer streamer.Close()
fmt.Printf("streamerMp3.Len():%v\n", streamer.Len())
length := format.SampleRate.D(streamer.Len())
nTime := int64(length.Round(time.Millisecond).Seconds() * 1000)
fmt.Printf("time:%v\n", nTime)
} The result is: len byteMp3:2219090 streamerMp3.Len():0 time:0
I think this is actually due to a limitation in haijmehoshi/go-mp3, being the fact that the constructor mp3.Decode(rc) actually requires io.Seeker interface in order to implement Len() and seeking. Maybe it could be mentioned in documentation. reference