Пример #1
0
// Destroy frees the underlying resources used by the player.
// It should be called as soon as the player is not in-use anymore.
func (p *Player) Destroy() {
	if p == nil {
		return
	}
	if p.source != 0 {
		al.DeleteSources(p.source)
	}
	p.muPrep.Lock()
	if len(p.bufs) > 0 {
		al.DeleteBuffers(p.bufs)
	}
	p.muPrep.Unlock()
}
Пример #2
0
func (p *Player) prepare(offset int64, force bool) error {
	p.muPrep.Lock()
	defer p.muPrep.Unlock()
	if !force && p.prep {
		return nil
	}
	if len(p.bufs) > 0 {
		p.source.UnqueueBuffers(p.bufs)
		al.DeleteBuffers(p.bufs)
	}
	if _, err := p.t.src.Seek(offset, 0); err != nil {
		return err
	}
	p.bufs = []al.Buffer{}
	// TODO(jbd): Limit the number of buffers in use, unqueue and reuse
	// the existing buffers as buffers are processed.
	buf := make([]byte, 128*1024)
	size := offset
	for {
		n, err := p.t.src.Read(buf)
		if n > 0 {
			size += int64(n)
			b := al.GenBuffers(1)
			b[0].BufferData(uint32(formatToCode[p.t.format]), buf[:n], int32(p.t.rate))
			p.bufs = append(p.bufs, b[0])
		}
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
	}
	p.size = size
	if len(p.bufs) > 0 {
		p.source.QueueBuffers(p.bufs)
	}
	p.prep = true
	return nil
}