15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PlayFramework 2.5.2 からRangeRequest対応した!

Posted at

0. 前置き

とあるサービスにてiPhoneで動画再生するようにしたいという要求がありました。
それで、コンテント返せばいいやってことで以下のように書いてました。


def getMovie() = Action { implicit request =>
   val sendFile = new File("/sendFilePath/example.mp4")
   val fileContent: Enumerator[Array[Byte]] = Enumerator.fromFile(sendFile)
   val source = Source.fromPublisher(Streams.enumeratorToPublisher(fileContent)).map(ByteString.apply)
   val entity = HttpEntity.Streamed(source, Some(file.length), Some(contentsType))
   Result(
      header = ResponseHeader(200),
      body = entity
   ).as("video/mp4")
}

ところがどっこい、動画再生されないじゃありませんか…(´・ω・`)

あ、iPhoneとかAndroidって動画再生RangeRequestしてるんだ。
ってことでPlayFrameworkでRangeRequestに対応することに。

1.PlayFrameworkではRangeRequestは今までめんどくさかった。

http://qiita.com/upopo21/items/ae43f0b61b0b7de22183
上記の方が記事を掲載していただいてるように、RangeRequestめんどくさかったです。

で、本題、Play2.5.2からRangeRequestが公式に対応しましたヽ(=´▽`=)ノ
https://github.com/playframework/playframework/pull/5995

1.これからは

公式対応したので、これからは以下のような感じでかけます。
非常に簡単ですね。

import play.api.mvc._

def getMovie() = Action { implicit request =>
   val sendFile = new File("/sendFilePath/example.mp4")
   RangeResult.ofFile(sendFile, request.headers.get("Range"), Some("video/mp4"))
}

Play使いでiPhone動画再生させたくて困っている方の足しになれば!
あ、Play2.4とかなら上司に2.5にしていいですか!?っていう口実にもなりますね。

15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?