【问题标题】:SBT & Scala AudioSystemSBT 和 Scala 音频系统
【发布时间】:2015-07-30 15:03:33
【问题描述】:

我有一个奇怪的问题,我一直在尝试解决这个问题。 我有一个使用 Scala & Spray 构建的应用程序,它使用 AudioSystem API。 我使用 SBT 构建和测试应用程序。 我有一个扩展“App”的 boot.scala。 如果我将以下代码放在 boot.scala 中并通过 Eclipse(没有 sbt)(运行方式... Scala App)运行它,它运行良好...

val stream:AudioInputStream = AudioSystem.getAudioInputStream(new File("test.wav"))
val audioFormat:AudioFormat = stream.getFormat();
val samplingRate = audioFormat.getSampleRate()
println("Sampling Rate: "+samplingRate)

文件的采样率按预期输出。 我在 Specs2 Route 测试中有相同的代码,类似于...

"API" should {
  "Respond to POST requests" in {
    val stream:AudioInputStream = AudioSystem.getAudioInputStream(new File("test.wav"))
    val audioFormat:AudioFormat = stream.getFormat();
    val samplingRate = audioFormat.getSampleRate()
    println("Sampling Rate: "+samplingRate)
    ...

但是,当我使用“sbt test”从终端执行此操作时,我收到以下错误...

UnsupportedAudioFileException: : could not get audio input stream from input file

我知道文件 (test.wav) 没问题,因为我可以播放它并且通过 Eclipse 执行代码可以正常工作。终端(及其编码)看起来也不错,因为我将一个测试文件放在一起,它只运行相同的几行代码并成功地从终端运行。

这个问题似乎只发生在 SBT 上!

有人有什么想法吗?

谢谢

【问题讨论】:

  • 有人有什么想法吗?!?!从字面上拉我的头发在这里!
  • 我还发现这个页面似乎表明其他人在访问 SBT 中的 Java 音频 API 时也遇到了问题...alvinalexander.com/scala/…

标签: scala sbt specs2


【解决方案1】:

经过几天的搜索终于找到了答案......

Why does AudioSystem.getMixerInfo() return different results under sbt vs Scala?

“这是一个类加载器问题。javax.sound 不喜欢上下文类加载器不是系统类加载器。”我的解决方法是......

val cl = classOf[javax.sound.sampled.AudioSystem].getClassLoader
val old = Thread.currentThread.getContextClassLoader

var audioFormat:AudioFormat = null
try {
  Thread.currentThread.setContextClassLoader(cl)
  val stream:AudioInputStream = 
    AudioSystem.getAudioInputStream(new ByteArrayInputStream(data))
  audioFormat = stream.getFormat()
} finally Thread.currentThread.setContextClassLoader(old)

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多