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

Play is downloading a file instead of streaming it

我正在尝试创建一个 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 header:

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

我应该改变什么?

使用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")
                       // ^
}