JAAD无法读取m4a

时间:2015-07-21 05:12:22

标签: scala javasound m4a

我希望为每个m4a文件生成预览。我正在尝试使用Java Sound和JAAD。

这是我在Scala中的尝试

import java.io.{File, FileOutputStream}
import javax.sound.sampled.AudioSystem

/**
 * Created by khanguyen on 7/21/15.
 */
object Main extends App {
  val filePath = "audio.m4a"

  val file = new File(filePath)

  val audio = AudioSystem.getAudioInputStream(file)

  println(audio.getFrameLength) // return -1

  println(audio.getFormat) // return PCM_SIGNED 0.0 Hz, 0 bit, 0 channels, 0 bytes/frame, 

  val output = new FileOutputStream("outputaudio.m4a")

  var buffer = Array.fill[Byte](1024)(0)

  for (i <- 0 to 1024) {
    audio.read(buffer, i * 1024, 1024)
    buffer.take(10).map(println)
    output.write(buffer)
  }

  audio.close()
  output.flush()
  output.close()
}

我无法从音频输入流中读取任何内容。 frameLength被称为-1。读取传递后,Array [Byte]中的所有字节仍为0。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

如果您在-1中收到audio.getFrameLength,那是因为不支持文件格式。

val audio = AudioSystem.getAudioInputStream(file)
                                                  //> javax.sound.sampled.UnsupportedAudioFileException: could not get audio input
                                                  //|  stream from input file
                                                  //|   at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:
                                                  //| 1187)
                                                  //|   at forcomp.wc$$anonfun$main$1.apply$mcV$sp(forcomp.wc.scala:15)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe
                                                  //| cute$1.apply$mcV$sp(WorksheetSupport.scala:76)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W
                                                  //| orksheetSupport.scala:65)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor
                                                  //| ksheetSupport.scala:75)
                                                  //|   at forcomp.wc$.main(forcomp.wc.scala:5)
                                                  //|   at forcomp.wc.main(forcomp.wc.scala)

      println(audio.getFrameLength) // return -1 


无论如何,我已经使用pet m4a示例文件对其进行了测试,但在javax.sound.AudioSystem api(http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html)中,您将找到有关支持的文件类型AAC(m4a)的更多信息。应该支持扩展是AAC编码的一部分。