Playing a sound to a specific player

So I’ve been looking at some youtube tutorials and javadocs because I’m interested in making the switch to Sponge. There is, however, one thing that I miss, which is playing a sound to one specific player.
All methods I have seen for this, will make the sound audible to all players within range.

Is this possible in Sponge?

Just make the position the players position :stuck_out_tongue:

Does that ensure the sound is played to only the player at this position? According to the javadocs, the sound is audible to all players within range, that’s why I’m asking :slight_smile:

I actually don’t think theres a method for just to the player, i might make a PR for one then if there already isnt one.

Unless you have an issue with player standing inside of each other, using a distance of 1 should play only to the player at that position

You can use the method Player.playSound (from the Viewer interface). It will only send the sound to that player.

In fact others will hear it.

If this means that provided the others are at least 1 block away from the target player, that’s good enough for me. Thanks!

But the sound will be heard by all players in range, that method doesn’t solve the issue :confused:

This is also not good, if a player is running quickly or flying on elytra the sound may fade out quickly or the player’s ping may delay the sound and make it inaudible to the target player.

Is there any new solution to solve this issue?

It shouldn’t do if you call playSound on a specific Player instance.

Note that both World and Player extend Viewer, so it is possible to play a sound in the world (all nearby players) or to an individual player.

3 Likes

Thank you. The “All players within range will hear the sound with the given volume” phrase causes confusion :stuck_out_tongue:

I also had swapped “playTo” and “playAt” in my implementation and it has contributed to my confusion. It should work now :slight_smile:

override fun playSoundAt(session: SpongeSession, sound: PlatformSound): Boolean {
    SpongeSound[sound].playTo(session.player) // <-- This was swapped 
    return true
}

override fun playSoundTo(session: SpongeSession, sound: PlatformSound): Boolean {
    if(!session.player.isOnline)
        return false

    SpongeSound[sound].playAt(session.player)  // <-- This was swapped 
    return true
}