Transmission duration when streaming mp3 from a server to html5 audio

I have a node.js server converting and streaming mp3 on the fly. I am using HTML5 audio text to use this stream, and the problem I am facing is an audio element that does not know the duration of the mp3 until it finishes playing everything (obviously). Is there a way, since my server knows the duration of the mp3 before sending, that I can include the duration in the response header from the server or something else, so does the client consuming it know the duration?

thank

+5
source share
3 answers

, , , ( - ), , , URI). , , , , .

-, , - : json , , , .mp3 ( -mp3- ..).

, , html5 / .

+2

, , ...

206 http, . .

html5 :

Range: bytes=0-  or..
Range: bytes=0-12345

spec:

HTTP/1.1 206 PARTIAL CONTENT
Accept-Ranges: bytes
Content-Range: bytes 0-12345

206 , .

Perl, , $request , . , SERVER_PROTOCOL "HTTP/1.1"

my $crlf = "\012";
if ( $request->{RANGE} && $request->{RANGE} =~ /bytes=(\d*)-(.*)$/ ) {
    $offset = $1;
    $end = $2 || $size; # end is optional in the spec.  Default to size.
    $header = $request->{SERVER_PROTOCOL} . ' 206 PARTIAL CONTENT' . $crlf .
    'Content-Range: bytes ' . $offset . '-' . $end . '/' . $size . $crlf;
} else {
    $header = $request->{SERVER_PROTOCOL} . ' 200 OK' . $crlf;
}
my $left = $end - $offset;
$header .= 'Server: ' . $SERVER_NAME . $crlf .
    'Accept-Ranges: bytes' . $crlf . 
    'Date: ' . $http_date . $crlf .
    'Content-Type: ' . ($self->simplemime($raw_path) || magic($fh)) . $crlf .
    'Last-Modified: ' . $http_date . $crlf .
    'Content-Length: ' . $size . $crlf .
    'Connection: Keep-Alive' . $crlf .
    'Cache-Control: max-age=' . $EXPIRE . $crlf . $crlf;

, , , .
"" , , , Mojolicious, AnyEvent Node.js , 1 , PHP, . (, , Ratchet Xsendfile)

, :

Range: bytes=0- 

, , ( ). HTTP/1.1 200 HTTP/1.1 206, . , , , , . , , . , ... .

+3

:

, , .

0

All Articles