作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试播放来自 Golang 的声音。这是一个 .wav 文件。我想使用 packr 将 .wav 文件打包到可执行文件中
我在这里创建了一个非常小的项目:packr-test repository用代码。
当我在默认文件夹中运行可执行文件 (./packr-test) 时,会播放声音。 但我遇到的问题是,当我将可执行文件移动到另一个目录时,我在尝试播放声音文件时遇到错误。我认为这可能意味着声音文件没有与可执行文件捆绑在一起。
这是在 Ubuntu 上。我正在使用通常默认安装的“播放”命令,但如果它不存在,可以通过以下方式完成:
sudo apt-get install sox
sudo apt-get install sox libsox-fmt-all
使用播放命令:
play file_name.extension
为了省去查找,这是我的 Go 代码:
package main
import (
"fmt"
"os/exec"
"github.com/gobuffalo/packr"
)
func main() {
soundsBox := packr.NewBox("./sounds")
if soundsBox.Has("IEEE_float_mono_32kHz.wav") {
fmt.Println("It's there.")
} else {
fmt.Println("It's not there.")
}
args := []string{"-v20", "./sounds/IEEE_float_mono_32kHz.wav"}
output, err := exec.Command("play", args...).Output()
if err != nil {
// Play command was not successful
fmt.Println("Got an error.")
fmt.Println(err.Error())
} else {
fmt.Println(string(output))
}
}
这是我的输出:
sudo ./packr-test
It's there.
Got an error.
exit status 2
最佳答案
即使您已将其打包到二进制文件中,您仍在引用文件系统上的文件:
args := []string{"-v20", "./sounds/IEEE_float_mono_32kHz.wav"}
output, err := exec.Command("play", args...).Output()
您可以像这样从 packr box 中获取文件数据:
bytes, err := soundsBox.FindBytes("IEEE_float_mono_32kHz.wav")
要使用 exec.Command()
执行文件,我认为您必须将这些字节写回文件系统:
err := ioutil.WriteFile("/tmp/IEEE_float_mono_32kHz.wav", bytes, 0755)
exec.Command("play", []string{"-v20", "/tmp/IEEE_float_mono_32kHz.wav"}
您可以通过标准输入将字节传递给 play
,但这取决于 play
二进制文件的工作方式。
cmd.Stdin = bytes
cmd.Run()
关于go - 如何在 Golang 中使用 gobuffalo/packr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54192824/
我是一名优秀的程序员,十分优秀!