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

后端 未结 1 799
旧时难觅i
旧时难觅i 2021-02-06 02:15

After a lot of problem isolation, I got this snippet:

object Snippet  {
    def main(args: Array[String]): Unit = {
        println(\"Snip demo:: \"+ util.Proper         


        
1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 02:57

    This is a classloader issue. javax.sound does NOT like having the context classloader be anything other than the system classloader. This fixes it for me locally:

    object Snippet  {
        def main(args: Array[String]): Unit = {
            println("Snip demo:: "+ util.Properties.versionString)
            println(" jvm "+System.getProperty("java.version"))
            import javax.sound.sampled._
            val cl = classOf[javax.sound.sampled.AudioSystem].getClassLoader
            val old = Thread.currentThread.getContextClassLoader
            try {
              Thread.currentThread.setContextClassLoader(cl)
              val mixers : Array[Mixer.Info] = javax.sound.sampled.AudioSystem.getMixerInfo()
              println(" Numer of mixers: "+ mixers.size)
              for (mi : Mixer.Info <- mixers ) { 
                println("mixer info "+mi.getName) 
              }
           } finally Thread.currentThread.setContextClassLoader(old) 
        }
    }
    

    And the output:

    > run
    [info] Compiling 1 Scala source to /home/jsuereth/projects/sbt/diagnose/sound-issues/target/scala-2.10/classes...
    [info] Running Snippet 
    Snip demo:: version 2.10.4
     jvm 1.7.0_55
     Numer of mixers: 9
    mixer info default [default]
    mixer info PCH [plughw:0,0]
    mixer info NVidia [plughw:1,3]
    mixer info NVidia [plughw:1,7]
    mixer info NVidia [plughw:1,8]
    mixer info NVidia [plughw:1,9]
    mixer info Port PCH [hw:0]
    mixer info Port NVidia [hw:1]
    mixer info Port Unknown Name
    [success] Total time: 2 s, completed Aug 1, 2014 11:00:03 AM
    > 
    

    0 讨论(0)
提交回复
热议问题