在 github 页上的 repo 中获取音频文件

source an audio file in repo on github pages

我在 github 页上托管一个项目,我想通过 html5 <audio> 元素播放位于我的存储库中的音频文件。当我在我的开发环境中获取它时,音频播放正常,但是当我推送到 gh-pages 时,我收到 404 控制台错误,无法找到它。

<audio ref='themeSong' src="vendor/assets/music/Tetris.mp3" autoPlay loop></audio>

错误:

GET http://myName.github.io/vendor/assets/music/Tetris.mp3 404 (Not Found)

我试过这样采购它:

"vendor/assets/music/Tetris.mp3"
"/vendor/assets/music/Tetris.mp3"
"http://github.com/myName/myRepo/vendor/assets/music/Tetris.mp3"
"http://github.com/myName/myRepo/tree/master/vendor/assets/music/Tetris.mp3"

但似乎没有任何效果。

您可以尝试参考原始 url

https://raw.githubusercontent.com/myName/myRepo/master/vendor/assets/music/Tetris.mp3

注意:像 rawgit.com 这样的服务提到:

When you request a file from raw.githubusercontent.com or gist.githubusercontent.com, GitHub usually serves it (in the case of JavaScript, HTML, CSS, and some other file types) with a Content-Type of text/plain. As a result, most modern browsers won't actually interpret it as JavaScript, HTML, or CSS.

They do this because serving raw files from a git repo is relatively inefficient, so they want to discourage people from using their GitHub repos for static file hosting.

RawGit acts as a caching proxy, forwarding requests to GitHub, caching the responses either for a short time (in the case of rawgit.com URLs) or permanently (in the case of cdn.rawgit.com URLs), and relaying them to your browser with the correct Content-Type headers.