diff --git a/README.md b/README.md index 7af2cda..c28a189 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,8 @@ func main() { // Format of the source. go-mp3's format is signed 16bit integers. op.Format = oto.FormatSignedInt16LE - // Remember that you should **not** create more than one context + // Remember that you should **not** create more than one context. + // Calling oto.NewContext more than once returns an error: reuse the context for all your players. otoCtx, readyChan, err := oto.NewContext(op) if err != nil { panic("oto.NewContext failed: " + err.Error()) diff --git a/context.go b/context.go index 8d337bd..5a49b25 100644 --- a/context.go +++ b/context.go @@ -107,7 +107,7 @@ func NewContext(options *NewContextOptions) (*Context, chan struct{}, error) { defer contextCreationMutex.Unlock() if contextCreated { - return nil, nil, fmt.Errorf("oto: context is already created") + return nil, nil, fmt.Errorf("oto: context is already created (NewContext can be called only once per process; reuse the existing context instead)") } contextCreated = true diff --git a/oto_test.go b/oto_test.go index fac52f4..f1aba09 100644 --- a/oto_test.go +++ b/oto_test.go @@ -60,3 +60,14 @@ func TestSetBufferSize(t *testing.T) { } } } + +func TestNewContextAfterContextCreated(t *testing.T) { + // TestMain already created the context, so this call must fail. + op := &oto.NewContextOptions{} + op.SampleRate = 48000 + op.ChannelCount = 2 + _, _, err := oto.NewContext(op) + if err == nil { + t.Fatal("NewContext after the context was created must return an error") + } +}