Playing Music and Changing Scenes #2017
Unanswered
honorthrawn
asked this question in
Q&A
Replies: 2 comments 3 replies
-
Try this: import korlibs.audio.sound.*
import korlibs.audio.sound.fade.*
import korlibs.datastructure.*
import korlibs.time.*
import korlibs.korge.*
import korlibs.korge.scene.*
import korlibs.korge.tween.*
import korlibs.korge.view.*
import korlibs.image.color.*
import korlibs.image.format.*
import korlibs.io.file.std.*
import korlibs.korge.input.*
import korlibs.korge.ui.*
import korlibs.math.geom.*
import korlibs.math.interpolation.*
suspend fun main() = Korge(windowSize = Size(512, 512), backgroundColor = Colors["#2b2b2b"]) {
val sceneContainer = sceneContainer()
sceneContainer.changeTo({ MyScene() })
}
class MyScene : Scene() {
override suspend fun SContainer.sceneMain() {
val minDegrees = (-16).degrees
val maxDegrees = (+16).degrees
val image = image(resourcesVfs["korge.png"].readBitmap()) {
rotation = maxDegrees
anchor(.5, .5)
scale(0.8)
position(256, 256)
}
run {
val sound1 = AudioTone.generate(1.seconds, 5_000.0).toSound()
uiVerticalStack {
uiButton("play") {
onClick {
musicChannel.play(sound1, PlaybackParameters(infinitePlaybackTimes))
}
}
uiButton("fadeIn") {
onClick {
musicChannel.fadeIn()
}
}
uiButton("fadeOut") {
onClick {
musicChannel.fadeOut()
}
}
uiButton("stop") {
onClick {
musicChannel.stop()
}
}
}
}
while (true) {
image.tween(image::rotation[minDegrees], time = 1.seconds, easing = Easing.EASE_IN_OUT)
image.tween(image::rotation[maxDegrees], time = 1.seconds, easing = Easing.EASE_IN_OUT)
}
}
}
val Scene.musicChannel: AudioChannel by extraPropertyThis { AudioChannel("music", this) }
val Scene.voiceChannel: AudioChannel by extraPropertyThis { AudioChannel("voice", this) }
val Scene.effectsChannel1: AudioChannel by extraPropertyThis { AudioChannel("effects1", this, isLocal = true) }
val Scene.effectsChannel2: AudioChannel by extraPropertyThis { AudioChannel("effects2", this, isLocal = true) }
class AudioChannel(val name: String, val scene: Scene, val isLocal: Boolean = false) {
val views get() = scene.views
private val channelName = "sound-channel-$name"
private var channel: SoundChannel?
set(value) {
views.setExtra(channelName, value)
}
get() = views.getExtraTyped(channelName)
fun play(sound: Sound, params: PlaybackParameters = PlaybackParameters.DEFAULT) {
channel?.stop()
channel = sound.play(if (isLocal) scene.coroutineContext else views.views.coroutineContext, params)
}
fun stop() {
channel?.stop()
}
suspend fun fadeIn(time: TimeSpan = DEFAULT_FADE_TIME, easing: Easing = DEFAULT_FADE_EASING) {
channel?.fadeIn(time, easing)
}
suspend fun fadeOut(time: TimeSpan = DEFAULT_FADE_TIME, easing: Easing = DEFAULT_FADE_EASING) {
channel?.fadeOut(time, easing)
}
} This example should work with hotreloading + also preserve channels between scenes since it is using the coroutine context from the views. We can validate this in a kproject, and maybe eventually move it to the code. Feel free to check it and please, let us know if it works as expected for you. |
Beta Was this translation helpful? Give feedback.
2 replies
-
Thank you that is great. I didn't know you could have more than one active scene at a time. I managed to get it working with that approach and I think it fits in better with my understanding and the design of what I have so far. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! I am working on writing a game in Korge that uses the multiple scene feature. I have figured out I can select a random song and play it via code like this:
ps.tunes.add(resourcesVfs["music/Badlands.mp3"].readMusic())
ps.tunes.add(resourcesVfs["music/Cassette.mp3"].readMusic())
ps.tunes.add(resourcesVfs["music/Powerful.mp3"].readMusic())
ps.tunes.add(resourcesVfs["music/Soprano.mp3"].readMusic())
ps.tunes.shuffle()
ps.currentTune = ps.tunes[0].play()
ps is one of my objects -- stands for PlayerState. Just cramming it there for the time being. The problem is: the music starts just fine but as soon as I click a button that causes the scene to switch, the music stops. Ideally, I'd like to be able to start a song, play it until the end (even if scene changed) and then switch to a new song. Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions