Last active
December 13, 2018 19:34
-
-
Save kemokemo/ab3776224e7557b389ed07595546f78c to your computer and use it in GitHub Desktop.
A modified version of the sample that uses go-mp3 and oto library to play mp3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2017 Hajime Hoshi | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// ** This file was copied from "https://github.com/hajimehoshi/go-mp3/blob/master/example/main.go" | |
// and added some changes ** | |
package main | |
import ( | |
"errors" | |
"io" | |
"log" | |
"os" | |
"time" | |
"github.com/hajimehoshi/oto" | |
"github.com/hajimehoshi/go-mp3" | |
) | |
func run() error { | |
f, err := os.Open("../classic.mp3") | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
d, err := mp3.NewDecoder(f) | |
if err != nil { | |
return err | |
} | |
defer d.Close() | |
p, err := oto.NewPlayer(d.SampleRate(), 2, 2, 8192) | |
if err != nil { | |
return err | |
} | |
defer p.Close() | |
abortCh := make(chan struct{}) | |
go func() { | |
defer func() { | |
close(abortCh) | |
log.Println("Done!") | |
}() | |
log.Println("Start to play!") | |
_, err := AbortableCopy(p, d, abortCh) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
}() | |
// Sample to stop playback after 10 seconds | |
time.Sleep(10 * time.Second) | |
log.Println("Aborting...") | |
abortCh <- struct{}{} | |
log.Println("Waiting...") | |
log.Println("Finished") | |
return nil | |
} | |
func main() { | |
if err := run(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
// AbortableCopy can be aborted from other goroutine via aobrtCh. | |
func AbortableCopy(w io.Writer, r io.Reader, abortCh <-chan struct{}) (int, error) { | |
buf := make([]byte, 32*1024) | |
var nr, nw, written int | |
var err, errw error | |
for { | |
nr, err = r.Read(buf) | |
if err != nil && err != io.EOF { | |
break | |
} | |
if nr > 0 { | |
nw, errw = w.Write(buf[0:nr]) | |
if nw > 0 { | |
written += int(nw) | |
} | |
if errw != nil { | |
err = errw | |
break | |
} | |
if nr != nw { | |
err = errors.New("The written buffer size was less than the read buffer size") | |
break | |
} | |
} | |
select { | |
default: | |
case <-abortCh: | |
return written, nil | |
} | |
} | |
return written, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These ideas are from hajime-hoshi-san.
Thank you so much.
ebitengine/oto#25