diff --git a/build.sbt b/build.sbt index fc518f13..1bd956db 100644 --- a/build.sbt +++ b/build.sbt @@ -1,7 +1,7 @@ import Dependencies._ ThisBuild / scalaVersion := "2.12.8" -ThisBuild / version := "0.2.0-SNAPSHOT" +ThisBuild / version := "0.3.0-SNAPSHOT" lazy val root = (project in file(".")) .settings( @@ -9,10 +9,6 @@ lazy val root = (project in file(".")) libraryDependencies += scalaTest % Test ) -// System.exit calls are trapped to prevent the JVM from terminating. -// This is useful for executing user code that may call System.exit. -trapExit := false - // Tests Configurations val numberOfTestProcessors = java.lang.Runtime.getRuntime.availableProcessors + 1 // Run tests in parallel diff --git a/src/main/scala/controller/util/ResourceName.scala b/src/main/scala/controller/util/ResourceName.scala index da72e753..3d108f27 100644 --- a/src/main/scala/controller/util/ResourceName.scala +++ b/src/main/scala/controller/util/ResourceName.scala @@ -1,5 +1,8 @@ package controller.util +import java.io.{BufferedInputStream, InputStream} +import javax.sound.sampled.{AudioInputStream, AudioSystem} + /** * Component that stores the files and directories' name. */ @@ -57,4 +60,14 @@ object ResourceName { def interactionSoundEffectPath(): String = "/" + SoundsEffectsDirectoryName + "/" + InteractionSoundEffectFileName def navigationSoundEffectPath(): String = "/" + SoundsEffectsDirectoryName + "/" + NavigationSoundEffectFileName + + def resourceAsInputStream(resourceName: String): InputStream = getClass.getResourceAsStream(resourceName) + + def resourceAsAudioInputStream(resourceName: String): AudioInputStream = { + val audioSrc : InputStream = resourceAsInputStream(resourceName) + //add buffer for mark/reset support + val bufferedIn : InputStream = new BufferedInputStream(audioSrc) + AudioSystem.getAudioInputStream(bufferedIn) + } + } diff --git a/src/main/scala/view/Frame.scala b/src/main/scala/view/Frame.scala index 1af815c8..e1d83b07 100644 --- a/src/main/scala/view/Frame.scala +++ b/src/main/scala/view/Frame.scala @@ -1,5 +1,6 @@ package view +import controller.util.ResourceName.resourceAsInputStream import view.util.scalaQuestSwingComponents.SqSwingBorderPanel import java.awt._ @@ -89,7 +90,7 @@ object Frame { this.setBackground(Color.BLACK) override def paintComponent(g: Graphics): Unit = { - val bg = ImageIO.read(getClass.getResourceAsStream("/general_background.png")) + val bg = ImageIO.read(resourceAsInputStream("/general_background.png")) val g2d: Graphics2D = g.create().asInstanceOf[Graphics2D] var _y = 0 var _x = 0 diff --git a/src/main/scala/view/util/SoundPlayer.scala b/src/main/scala/view/util/SoundPlayer.scala index cb26295b..3f6a9992 100644 --- a/src/main/scala/view/util/SoundPlayer.scala +++ b/src/main/scala/view/util/SoundPlayer.scala @@ -1,27 +1,22 @@ package view.util -import controller.util.ResourceName +import controller.util.ResourceName.{interactionSoundEffectPath, navigationSoundEffectPath, resourceAsAudioInputStream} -import java.io.File -import javax.sound.sampled.AudioSystem +import javax.sound.sampled.{AudioInputStream, AudioSystem, Clip} object SoundPlayer { - private def playAudio(path: String): Unit = { - val clip = AudioSystem.getClip - clip.open( - AudioSystem.getAudioInputStream( - new File(getClass.getResource(path).getPath) - ) - ) + private def playAudio(audioInputStream: AudioInputStream): Unit = { + val clip : Clip = AudioSystem.getClip() + clip.open(audioInputStream) clip.start() } def playNavigationSound(): Unit = { - playAudio(ResourceName.navigationSoundEffectPath()) + playAudio(resourceAsAudioInputStream(navigationSoundEffectPath())) } def playInteractionSound(): Unit = { - playAudio(ResourceName.interactionSoundEffectPath()) + playAudio(resourceAsAudioInputStream(interactionSoundEffectPath())) } }