我正在嘗試播放來自 Golang 的聲音。這是一個 .wav 文件。我想使用packr將 .wav 文件打包成可執(zhí)行文件我在這里創(chuàng)建了一個非常小的項目:帶有代碼的packr-test 存儲庫。當我在默認文件夾中運行可執(zhí)行文件 (./packr-test) 時,會播放聲音。但我遇到的問題是,當我將可執(zhí)行文件移動到另一個目錄時,我在嘗試播放聲音文件時遇到錯誤。我認為這可能意味著聲音文件沒有與可執(zhí)行文件捆綁在一起。這是在 Ubuntu 上。我正在使用通常默認安裝的“播放”命令,但如果它不存在,可以通過以下方式完成:sudo apt-get install soxsudo apt-get install sox libsox-fmt-all使用播放命令:play file_name.extension為了節(jié)省您的查找時間,這是我的 Go 代碼:package mainimport (? ? "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
1 回答

holdtom
TA貢獻1805條經(jīng)驗 獲得超10個贊
您仍在引用文件系統(tǒng)上的文件,即使您已將其打包到二進制文件中:
args := []string{"-v20", "./sounds/IEEE_float_mono_32kHz.wav"}
output, err := exec.Command("play", args...).Output()
您可以像這樣從 packr 框中獲取文件數(shù)據(jù):
bytes, err := soundsBox.FindBytes("IEEE_float_mono_32kHz.wav")
要執(zhí)行文件,exec.Command()我認為你必須將這些字節(jié)寫回文件系統(tǒng):
err := ioutil.WriteFile("/tmp/IEEE_float_mono_32kHz.wav", bytes, 0755)
exec.Command("play", []string{"-v20", "/tmp/IEEE_float_mono_32kHz.wav"}
您也許可以play通過標準輸入將字節(jié)傳遞給它,但這取決于play二進制文件的工作方式。
cmd.Stdin = bytes
cmd.Run()
- 1 回答
- 0 關(guān)注
- 180 瀏覽
添加回答
舉報
0/150
提交
取消