Skip to content

Instantly share code, notes, and snippets.

@zkessin
Created September 13, 2012 19:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zkessin/3716877 to your computer and use it in GitHub Desktop.
Save zkessin/3716877 to your computer and use it in GitHub Desktop.
Erlang Data Streaming
-module(streaming).
-export([stream_data/1, out/1, stream_from_file/3]).
out(_Arg) ->
_Pid = spawn_link(?MODULE, stream_data, [self()]),
{streamcontent, "audio/mp3", <<>>}.
stream_data(Pid) ->
File = "/home/erlang/erlang-talk/audio.mp3",
FileHDL = open_file(File),
stream_from_file(Pid, FileHDL, 1).
stream_from_file(Pid, File, I) ->
Result = file:read(File, 4096),
case Result of
{ok, Data} ->
yaws_api:stream_chunk_deliver_blocking(Pid,Data),
streaming:stream_from_file(Pid, File, I+1);
eof ->
yaws_api:stream_chunk_end(Pid);
{error,Reason}->
error_logger:error_msg("~p:~p Error ~p ~n",
[?MODULE, ?LINE, Reason])
end.
open_file(File) ->
{ok, IoDevice} = file:open(File,
[read, binary]),
IoDevice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment