Java Sound:在IntelliJ中运行时找到的设备,但在SBT中没有 [英] Java Sound: devices found when run in IntelliJ, but not in SBT
问题描述
我正在尝试在Scala SBT管理的项目中使用Java Sound API。
I'm trying to to use Java Sound API in a Scala SBT-managed project.
这是一个播放笔记的玩具应用程序。
Here is a toy app that plays a note.
import javax.sound.midi._
object MyMain extends App {
val infos = MidiSystem.getMidiDeviceInfo()
println( "[DEBUG] midi devices found: " + infos.length )
val myMsg = new ShortMessage;
// Start playing the note Middle C (60),
// moderately loud (velocity = 93).
myMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);
val timeStamp = -1;
val rcvr : Receiver = MidiSystem.getReceiver();
rcvr.send(myMsg, timeStamp);
readChar() // give time to play note
}
当我在SBT中执行运行
时,我得到 javax.sound.midi.MidiUnavailableException
,因为 infos.length
返回0.也就是说,当我在IntelliJ中运行应用程序时,找到了两个设备并且笔记播放得很好。
When I execute run
in SBT, I get the javax.sound.midi.MidiUnavailableException
because infos.length
returns 0. That said, when I run the app in IntelliJ, two devices are found and the note plays just fine.
SBT需要知道什么才能让它运行?是否有需要添加到类路径的东西?我注意到IntelliJ将一大堆罐子附加到执行命令(但是,删除来自jdk / jre / lib /的罐子没有效果,而其他罐子与scala相关或与IntelliJ相关)。
What does SBT need to know to make it run? Is there something which needs to be added to the classpath? I noticed that IntelliJ attaches a whole bunch of jars to the execution command (however, removing those of the jars that come from jdk/jre/lib/ had no effect, while the others are scala related or IntelliJ related).
推荐答案
SBT在进程中运行你的应用程序,带有一些类加载器魔法,可能会阻止 MidiSystem
从查找(使用SPI)声音组件。
SBT runs your application in-process, with some classloader magic, which probably prevents MidiSystem
from finding (using SPI) the sound components.
您可以尝试使用新的JVM来运行您的应用程序: fork in run:= true
。请参阅文档中的分叉。
You can try forking a new JVM to run your application: fork in run := true
. See Forking in the documentation.
请注意:
-
默认情况下,它不会将输入重定向到应用程序。你可以添加:
by default it does not redirect input to the application. You can add that with:
connectInput in run := true
kill
或任何类型的任务管理器)
kill
or any kind of task manager)这篇关于Java Sound:在IntelliJ中运行时找到的设备,但在SBT中没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!