4

How can i stream a video from a play 2 application to a HTML5 video player?

When i use the solutions from play stream doc i can receive the video but the video length is infinity in the video player.

Alex
  • 8,518
  • 4
  • 28
  • 40

1 Answers1

5

I adapted this solution.

 def stream(id: Long) = Action {
    implicit r =>
      val v = videos.where(_.id === id).single
      val file = new java.io.File(v.filePath)
      import ExecutionContext.Implicits.global
      val fileContent: Enumerator[Array[Byte]] = Enumerator.fromFile(file)

      SimpleResult(
        header = ResponseHeader(200, Map(
          CONTENT_LENGTH -> file.length.toString,
          CONTENT_RANGE -> s"bytes */${file.length.toString}",
          ACCEPT_RANGES -> "bytes",
          CONTENT_TYPE -> v.format,
          PRAGMA -> "public",
          CONTENT_TRANSFER_ENCODING -> "binary",
          CONTENT_DISPOSITION -> "attachment"
        )),
        body = fileContent
      )
  }
Community
  • 1
  • 1
Alex
  • 8,518
  • 4
  • 28
  • 40