Skip to content

Instantly share code, notes, and snippets.

@jgeek
Last active April 9, 2016 12:56
Show Gist options
  • Save jgeek/49eea6e9987e75289f073729ae064b48 to your computer and use it in GitHub Desktop.
Save jgeek/49eea6e9987e75289f073729ae064b48 to your computer and use it in GitHub Desktop.
Play framework
http://stackoverflow.com/questions/27848548/play-framework-2-3-x-bytechunks-mp3-streaming-has-no-playback-is-not-scrollabl
With(MP3Headers.class)
public static Result test() {
final int begin, end;
final boolean isRangeReq;
response().setHeader("Accept-Ranges", "bytes");
if (request().hasHeader("RANGE")) {
isRangeReq = true;
String[] range = request().getHeader("RANGE").split("=")[1].split("-");
begin = Integer.parseInt(range[0]);
if (range.length > 1) {
end = Integer.parseInt(range[1]);
} else {
end = song.length-1;
}
response().setHeader("Content-Range", String.format("bytes %d-%d/%d", begin, end, song.length));
} else {
isRangeReq = false;
begin = 0;
end = song.length - 1;
}
Chunks<byte[]> chunks = new ByteChunks() {
public void onReady(Chunks.Out<byte[]> out) {
if(isRangeReq) {
out.write(Arrays.copyOfRange(song, begin, end));
} else {
out.write(song);
}
out.close();
}
};
response().setHeader("Content-Length", (end - begin + 1) + "");
if (isRangeReq) {
return status(206, chunks);
} else {
return status(200, chunks);
}
}
public class MP3Headers extends Action.Simple {
public Promise<Result> call(Http.Context ctx) throws Throwable {
ctx.response().setContentType("audio/mpeg");
return delegate.call(ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment