Play正在下载文件,而不是流式传输

时间:2019-08-26 17:21:04

标签: scala playframework akka-stream

我正在尝试创建一个API,该API从AWS S3中获取文件并将其发送到可以播放该文件的前端。但是,当我在浏览器中对其进行测试时,它只是下载文件。我需要播放文件。

我的动作

def index() = Action { implicit request: Request[AnyContent] =>
  val songname: String = "song.mp3"
  val objStream = s3.client.getObject("music", songname).getObjectContent

  val dataContent: Source[ByteString, _] = StreamConverters.fromInputStream(() => objStream)

  Ok.chunked(dataContent).withHeaders(ACCEPT_RANGES -> "bytes", CONTENT_TYPE -> "Content-Type: audio/mp3")
}

此外,它会忽略我的content_type标头:

[warn] a.a.ActorSystemImpl - Explicitly set HTTP header 'Content-Type: Content-Type: audio/mp3' is ignored, illegal RawHeader

我应该改变什么?

1 个答案:

答案 0 :(得分:2)

使用as代替withHeaders

def index() = Action { implicit request =>
  val songname = "song.mp3"
  val objStream = s3.client.getObject("music", songname).getObjectContent

  val dataContent: Source[ByteString, _] =
    StreamConverters.fromInputStream(() => objStream)

  Ok.chunked(dataContent).as("audio/mp3")
                       // ^
}