Elixir流音频给用户

时间:2016-05-31 11:36:16

标签: streaming elixir html5-audio audio-streaming

以下代码将文件流式传输到进程。 我想流式传输音频/ 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

$: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,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,...>> <<<&>,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读取这些二进制数据并通过音频标签听到它?

1 个答案:

答案 0 :(得分:3)

如果您使用基于插件的Web框架,那么它应该是相当直接的。如果您直接使用插头或者在凤凰城内使用插头(基于插头),这是可能的。

也许像这样的插件可以解决这个问题

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

也许你想把你的插头连接到像这样的凤凰路由器

defmodule MyApp.Router do
  use MyApp.Web, :router

  get "/the_song", Audio, :song
end

然后在你的页面

<audio src="/the_song">
    Your browser does not support the <code>audio</code> element.
</audio>