mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
audio: Check the stream can be used or not
This commit is contained in:
parent
a05d1bfa43
commit
a2402c32c0
@ -26,6 +26,7 @@
|
|||||||
package audio
|
package audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
@ -136,6 +137,17 @@ func (p *players) playerCurrent(player *Player, sampleRate int) time.Duration {
|
|||||||
return time.Duration(sample) * time.Second / time.Duration(sampleRate)
|
return time.Duration(sample) * time.Second / time.Duration(sampleRate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *players) hasSource(src ReadSeekCloser) bool {
|
||||||
|
p.RLock()
|
||||||
|
defer p.RUnlock()
|
||||||
|
for player := range p.players {
|
||||||
|
if player.src == src {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Enable to specify the format like Mono8?
|
// TODO: Enable to specify the format like Mono8?
|
||||||
|
|
||||||
// A Context is a current state of audio.
|
// A Context is a current state of audio.
|
||||||
@ -259,8 +271,13 @@ type Player struct {
|
|||||||
// without a header (e.g. RIFF header).
|
// without a header (e.g. RIFF header).
|
||||||
// The sample rate must be same as that of the audio context.
|
// The sample rate must be same as that of the audio context.
|
||||||
//
|
//
|
||||||
|
// Note that the given src can't be shared with other Players.
|
||||||
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
|
func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
|
||||||
|
if context.players.hasSource(src) {
|
||||||
|
return nil, errors.New("audio: src cannot be shared with another Player")
|
||||||
|
}
|
||||||
p := &Player{
|
p := &Player{
|
||||||
players: context.players,
|
players: context.players,
|
||||||
src: src,
|
src: src,
|
||||||
@ -280,6 +297,8 @@ func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
|
|||||||
|
|
||||||
// Close closes the stream. Ths source stream passed by NewPlayer will also be closed.
|
// Close closes the stream. Ths source stream passed by NewPlayer will also be closed.
|
||||||
//
|
//
|
||||||
|
// After closing, the stream owned by the player will be usable to a new player again.
|
||||||
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func (p *Player) Close() error {
|
func (p *Player) Close() error {
|
||||||
p.players.removePlayer(p)
|
p.players.removePlayer(p)
|
||||||
|
Loading…
Reference in New Issue
Block a user