All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.dev.schlaubi.lavakord.interop.JavaPlayer.kt Maven / Gradle / Ivy

There is a newer version: 7.1.0
Show newest version
package dev.schlaubi.lavakord.interop

import dev.arbjerg.lavalink.protocol.v4.Track
import dev.schlaubi.lavakord.audio.Event
import dev.schlaubi.lavakord.audio.EventSource
import dev.schlaubi.lavakord.audio.player.Player
import kotlinx.coroutines.CoroutineScope
import java.time.Duration
import java.util.concurrent.CompletableFuture
import kotlin.coroutines.CoroutineContext

/**
 * Wrapper class for [Player] which replaces suspending functions by using [CompletableFuture].
 *
 * @see Player
 * @property playingTrack the currently playing [Track]
 * @property paused whether the playback is currently paused or not
 * @property volume the current volume of this player
 * @property position the position of the current song the player is at (-1 if [playingTrack] is null)
 */
public class JavaPlayer(internal val suspendingPlayer: Player) : CoroutineScope, JavaEventSource {
    override val coroutineContext: CoroutineContext
        get() = suspendingPlayer.coroutineScope.coroutineContext
    override val suspendingEventSource: EventSource
        get() = suspendingPlayer
    public val playingTrack: Track?
        get() = suspendingPlayer.playingTrack
    public val paused: Boolean
        get() = suspendingPlayer.paused
    public val volume: Int
        get() = suspendingPlayer.volume
    public val position: Long
        get() = suspendingPlayer.position


    /**
     * Changes the currently playing track to [track].
     */
    public fun playTrack(track: Track): CompletableFuture = playTrack(track.encoded)

    /**
     * Changes the currently playing track to [track].
     *
     * @param track the lavalink encoded track
     */
    public fun playTrack(track: String): CompletableFuture =
        run { suspendingPlayer.playTrack(track) }

    /**
     * Stops playback of the currently playing track.
     */
    public fun stopTrack(): CompletableFuture = run { suspendingPlayer.stopTrack() }

    /**
     * Pauses/unpauses playback of the current track.
     *
     * @param doPause whether the playback should be paused or un-paused
     */
    public fun pause(doPause: Boolean = true): CompletableFuture = run { suspendingPlayer.pause(doPause) }

    /**
     * Unpauses the playback
     *
     * @see pause
     */
    public fun unPause(): CompletableFuture = pause(false)

    /**
     * Seeks to a specific [position] in the current playback.
     */
    public fun seekTo(position: Duration): CompletableFuture = seekTo(position.toMillis())

    /**
     * Seeks to a specific [position] in the current playback.
     *
     * @param position the position in the track in milliseconds
     */
    public fun seekTo(position: Long): CompletableFuture = run { suspendingPlayer.seekTo(position) }

    /**
     * Creates an [EqualizerBuilder] to update equalizer config.
     * **This does not update the equalizers. To update equalizers please call [EqualizerBuilder.apply]**
     */
    public fun updateEqualizer(): EqualizerBuilder = EqualizerBuilder(suspendingPlayer.equalizers.toMutableMap())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy