Ejemplo n.º 1
0
func (hub *Hub) streamStep() {
	r, err := os.Open(hub.track)
	defer r.Close()

	if err != nil {
		log.Println(err)
		return
	}

	d := mp3.NewDecoder(r)
	var f mp3.Frame

	for {
		if err := d.Decode(&f); err != nil {
			log.Println(err)
			return
		}
		b := make([]byte, f.Size())

		f.Reader().Read(b)
		hub.SendMessage(b)

		time.Sleep(f.Duration())
	}
}
Ejemplo n.º 2
0
// Read reads a full mp3 frame. If p is too small to hold the frame,
// return the part that does fit (and io.ErrShortBuffer) and discard
// the rest of the frame.
func (mr *MP3Reader) Read(p []byte) (n int, err error) {
	var f mp3.Frame
	err = mr.d.Decode(&f)
	if err != nil {
		if err == io.ErrUnexpectedEOF {
			err = io.EOF
		}
		return
	}
	r := f.Reader()

	var x int
	for {
		x, err = r.Read(p)
		n += x
		if len(p) == 0 && err == nil {
			err = io.ErrShortBuffer
		}
		if err != nil {
			break
		}
		p = p[x:]
	}
	if err == io.EOF {
		err = nil
	}
	return
}