Skip to content

Commit

Permalink
[ #32 ] Also added.
Browse files Browse the repository at this point in the history
Closes #32.
  • Loading branch information
ice1000 committed Dec 11, 2017
1 parent 2c40655 commit aa8171c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/org/frice/utils/media/AudioManager.kt
Expand Up @@ -26,7 +26,7 @@ fun play(path: String, infinite: Boolean = false) = getPlayer(path, infinite).st
*/
@JvmOverloads
fun getPlayer(file: File, infinite: Boolean = false) =
AudioPlayer(if (infinite) AudioPlayerImpl.LoopAudioPlayer(file) else AudioPlayerImpl.OnceAudioPlayer(file))
AudioPlayer(if (infinite) AudioPlayerRunnable.LoopAudioPlayer(file) else AudioPlayerRunnable.OnceAudioPlayer(file))

/**
* @author ice1000
Expand Down
11 changes: 10 additions & 1 deletion src/org/frice/utils/media/AudioPlayer.kt
Expand Up @@ -8,4 +8,13 @@ package org.frice.utils.media
* @since v0.3.1
* @see org.frice.utils.media.getPlayer
*/
class AudioPlayer internal constructor(val runnable: AudioPlayerImpl) : Thread(runnable)
class AudioPlayer internal constructor(val runnable: AudioPlayerRunnable) : Thread(runnable) {

/**
* It's the safest way to stop playing.
* @since v1.7.7
*/
fun stopPlaying() {
runnable.stopped = true
}
}
Expand Up @@ -10,17 +10,13 @@ import javax.sound.sampled.*
* @since v1.7.6
* @see org.frice.utils.media.AudioPlayer
*/
sealed class AudioPlayerImpl(val file: File) : Runnable, Closeable {
protected lateinit var audioInputStream: AudioInputStream
private lateinit var format: AudioFormat
protected lateinit var line: SourceDataLine
sealed class AudioPlayerRunnable(file: File) : Runnable, Closeable {
protected var audioInputStream: AudioInputStream
private var format: AudioFormat
protected var line: SourceDataLine
var stopped = false

init {
init()
}

fun init() {
audioInputStream = AudioSystem.getAudioInputStream(file)
format = audioInputStream.format
if (format.encoding != AudioFormat.Encoding.PCM_SIGNED) {
Expand All @@ -43,7 +39,7 @@ sealed class AudioPlayerImpl(val file: File) : Runnable, Closeable {
line.close()
}

class OnceAudioPlayer(file: File) : AudioPlayerImpl(file) {
class OnceAudioPlayer(file: File) : AudioPlayerRunnable(file) {
override fun run() {
forceRun(line::open)
line.start()
Expand All @@ -58,7 +54,7 @@ sealed class AudioPlayerImpl(val file: File) : Runnable, Closeable {
}
}

class LoopAudioPlayer(file: File) : AudioPlayerImpl(file) {
class LoopAudioPlayer(file: File) : AudioPlayerRunnable(file) {
val cache = arrayListOf<ByteArray>()
override fun run() {
forceRun(line::open)
Expand Down
8 changes: 8 additions & 0 deletions test/org/frice/utils/media/AudioPlayerTest.kt
@@ -1,13 +1,21 @@
package org.frice.utils.media

import org.frice.GameFX
import org.frice.event.MOUSE_CLICKED
import org.frice.event.OnMouseEvent
import org.frice.utils.message.FLog

class AudioPlayerTest : GameFX() {
val player = getPlayer("../TouhouFrice/res/bgm.mp3", true)
override fun onLastInit() {
player.start()
}

override fun onMouse(e: OnMouseEvent) {
if (e.type == MOUSE_CLICKED) player.stopPlaying()
FLog.d(player.state)
}

companion object {
@JvmStatic
fun main(args: Array<String>) {
Expand Down

0 comments on commit aa8171c

Please sign in to comment.