-
-
Notifications
You must be signed in to change notification settings - Fork 556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow volume control of Music & Sound with property #1311
Open
DeathPhoenix22
wants to merge
1
commit into
AlmasB:dev
Choose a base branch
from
DeathPhoenix22:#1310
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,12 @@ | |
package com.almasb.fxgl.audio | ||
|
||
import com.almasb.fxgl.core.Disposable | ||
import javafx.beans.property.DoubleProperty | ||
import javafx.beans.property.SimpleDoubleProperty | ||
|
||
enum class AudioType { | ||
MUSIC, SOUND | ||
enum class AudioType(val volume: DoubleProperty) { | ||
MUSIC(SimpleDoubleProperty(0.5)), | ||
SOUND(SimpleDoubleProperty(0.5)) | ||
} | ||
|
||
/** | ||
|
@@ -19,6 +22,10 @@ enum class AudioType { | |
*/ | ||
abstract class Audio(val type: AudioType) { | ||
|
||
internal fun mix(volume: Double) : Double { | ||
return type.volume.value * volume | ||
} | ||
|
||
abstract fun setLooping(looping: Boolean) | ||
|
||
abstract fun setVolume(volume: Double) | ||
|
@@ -52,31 +59,56 @@ private val audio: Audio by lazy { | |
|
||
fun getDummyAudio() = audio | ||
|
||
abstract class Media(internal val audio : Audio) : Disposable { | ||
|
||
internal var isDisposed = false | ||
internal val volume = SimpleDoubleProperty(1.0) | ||
|
||
init { | ||
audio.type.volume.addListener { _, _, _ -> | ||
audio.setVolume(volume.value) | ||
} | ||
volume.addListener { _, _, _ -> | ||
audio.setVolume(volume.value) | ||
} | ||
} | ||
|
||
fun setVolume(value: Double) { | ||
volume.value = value | ||
} | ||
|
||
fun getVolume() : Double { | ||
return volume.value | ||
} | ||
|
||
fun volumeProperty() : DoubleProperty { | ||
return volume | ||
} | ||
|
||
fun setLooping(looping: Boolean) { | ||
audio.setLooping(looping) | ||
} | ||
|
||
override fun dispose() { | ||
isDisposed = true | ||
} | ||
} | ||
|
||
/** | ||
* Represents a long-term audio in mp3 file. | ||
* Use for background (looping) music or recorded dialogues. | ||
* | ||
* @author Almas Baimagambetov (AlmasB) ([email protected]) | ||
*/ | ||
class Music(val audio: Audio) : Disposable { | ||
class Music(audio : Audio) : Media(audio) { | ||
|
||
internal var isDisposed = false | ||
|
||
override fun dispose() { | ||
isDisposed = true | ||
} | ||
} | ||
|
||
/** | ||
* Represents a short sound in .wav file. | ||
* | ||
* @author Almas Baimagambetov (AlmasB) ([email protected]) | ||
*/ | ||
class Sound(val audio: Audio) : Disposable { | ||
|
||
internal var isDisposed = false | ||
class Sound(audio : Audio) : Media(audio) { | ||
|
||
override fun dispose() { | ||
isDisposed = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ import com.almasb.fxgl.core.EngineService | |
import com.almasb.fxgl.core.Inject | ||
import com.almasb.fxgl.core.collection.UnorderedArray | ||
import com.almasb.fxgl.logging.Logger | ||
import javafx.beans.property.DoubleProperty | ||
import java.net.URL | ||
|
||
/** | ||
|
@@ -27,27 +26,11 @@ class AudioPlayer : EngineService() { | |
private val activeMusic = UnorderedArray<Music>() | ||
private val activeSounds = UnorderedArray<Sound>() | ||
|
||
@Inject("globalMusicVolumeProperty") | ||
private lateinit var musicVolume: DoubleProperty | ||
|
||
@Inject("globalSoundVolumeProperty") | ||
private lateinit var soundVolume: DoubleProperty | ||
|
||
@Inject("isPauseMusicWhenMinimized") | ||
private var isPauseMusicWhenMinimized = true | ||
|
||
private val loader = DesktopAndMobileAudioLoader() | ||
|
||
override fun onMainLoopStarting() { | ||
musicVolume.addListener { _, _, newVolume -> | ||
activeMusic.forEach { it.audio.setVolume(newVolume.toDouble()) } | ||
} | ||
|
||
soundVolume.addListener { _, _, newVolume -> | ||
activeSounds.forEach { it.audio.setVolume(newVolume.toDouble()) } | ||
} | ||
} | ||
|
||
override fun onMainLoopPausing() { | ||
if (isPauseMusicWhenMinimized) { | ||
pauseAllMusic() | ||
|
@@ -74,7 +57,6 @@ class AudioPlayer : EngineService() { | |
if (!activeSounds.containsByIdentity(sound)) | ||
activeSounds.add(sound) | ||
|
||
sound.audio.setVolume(soundVolume.value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need anymore, the global volumes are automatically mixed with the Media configured sound |
||
sound.audio.play() | ||
} | ||
|
||
|
@@ -106,7 +88,6 @@ class AudioPlayer : EngineService() { | |
activeMusic.add(music) | ||
} | ||
|
||
music.audio.setVolume(musicVolume.value) | ||
music.audio.play() | ||
} | ||
|
||
|
@@ -192,4 +173,5 @@ class AudioPlayer : EngineService() { | |
fun loadAudio(audioType: AudioType, url: URL, isMobile: Boolean): Audio { | ||
return loader.loadAudio(audioType, url, isMobile) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automatically handled by the Audio with Property change listeners