Jim*_*est 6 streaming elixir audio-streaming html5-audio
以下代码将文件流式传输到进程.我想将音频/ mp3流式传输给许多将通过html5音频标签听到的用户.怎么可以通过File.stream!?
defmodule Test do
def start do
p = spawn(Test, :say, [])
send p, {self, "a message"}
end
def say do
receive do
{from, msg} ->
IO.puts "Process #{inspect self} says: #{msg}"
stream_bytes = 128
File.stream!("./song.mp3", [], stream_bytes)
|> Enum.each(fn chunk ->
IO.inspect chunk
end)
say
end
end
end
Run Code Online (Sandbox Code Playgroud)
$:iex test.ex
iex(1)> Test.start
输出:
<< 171,46,254,26,163,32,178,27,0,75,17,35,4,236,51,57,5,144,154,198,166,47,62,4,4, 61,85,67,135,16,34,82,49,57,176,131,96,116,152,232,24,32,140,220,67,73,128,165,178,230, 202,...... >> << 100,220,156,191,38,0,161,117,80,16,102,91,22,5,8,66,26,7,193,155,193 ,66,198,28,157,244,65,131,204,240,5,172,143,44,173,85,144,2,157,144,145,97,200,236,16,49 ,149,150,133,67,...... >> << 150,54,37,254,192,218,218,26,69,231,88,124,33,129,169,66,117, 52,214,134,130,103,85,130,48,6,144,221,153,132,8,181,26,27,83,140,54,117,149,7,60,144, 237,248,132,12,210,51,103,116,...... >> << 57,2,143,220,198,182,22,177,231,126,187,147,33,9 ,1,5,164,2,36,105,47,255,255,255,255,255,245,54,51,225,104,98,1,184,148,206,50,135,230 ,28,50,47,144,134,53,16,64,130,192,198,...... >> ..............
如何使用JavaScript读取这些二进制数据并通过音频标签听到它?
如果您使用的是基于插件的 Web 框架,那么它应该相当简单。如果您直接使用插头或在 phoenix 中使用它(基于插头),这是可能的。
也许像这样的插头就能解决问题
defmodule Audio do
@chunk_size 128
def init(opts), do: opts
def song(conn, _opts) do
conn = conn
|> send_chunked(200)
|> put_resp_header("content-type", "audio/mpeg")
File.stream!("/some/song/somewhere.mp3", [], @chunk_size)
|> Enum.into(conn)
end
end
Run Code Online (Sandbox Code Playgroud)
也许您想像这样将插头连接到 phoenix 路由器
defmodule MyApp.Router do
use MyApp.Web, :router
get "/the_song", Audio, :song
end
Run Code Online (Sandbox Code Playgroud)
然后在你的页面上
<audio src="/the_song">
Your browser does not support the <code>audio</code> element.
</audio>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1248 次 |
| 最近记录: |