playerPool[key] if (savedPlayer != null) { return savedPlayer } val idlePlayer = playerPool.firstNotNullOfOrNull { (key, value) -> if (!value.isAttached()) { key to value } else { null } } if (idlePlayer != null) { playerPool.remove(idlePlayer.first) playerPool[key] = idlePlayer.second idlePlayer.second.stop() return idlePlayer.second } else { val player = createPlayer() playerPool += key to player return player }