Skip to content

Commit

Permalink
Merge pull request #59 from AngeloFilaseta/bugSolvingAngelo
Browse files Browse the repository at this point in the history
Solved audio and sbt run crash bugs
  • Loading branch information
Tale152 committed Sep 1, 2021
2 parents 0fb39d0 + 96cbb20 commit 0c119f9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
6 changes: 1 addition & 5 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
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(
name := "PPS-20-scala-quest",
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
Expand Down
13 changes: 13 additions & 0 deletions src/main/scala/controller/util/ResourceName.scala
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand Down Expand Up @@ -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)
}

}
3 changes: 2 additions & 1 deletion src/main/scala/view/Frame.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package view

import controller.util.ResourceName.resourceAsInputStream
import view.util.scalaQuestSwingComponents.SqSwingBorderPanel

import java.awt._
Expand Down Expand Up @@ -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
Expand Down
19 changes: 7 additions & 12 deletions src/main/scala/view/util/SoundPlayer.scala
Original file line number Diff line number Diff line change
@@ -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()))
}
}

0 comments on commit 0c119f9

Please sign in to comment.