com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager Java Examples

The following examples show how to use com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalMusicManager.java    From kyoko with MIT License 7 votes vote down vote up
public LocalMusicManager(Vertx vertx, MusicSettings settings, EventManager eventManager) {
    this.vertx = vertx;
    this.eventManager = eventManager;
    this.sourceManagers = new ArrayList<>();
    this.players = new HashMap<>();
    this.queues = new HashMap<>();
    this.pendingLeave = new HashMap<>();
    this.audioConnections = new HashMap<>();
    this.queueLimit = settings.queueLimit;

    playerManager = new DefaultAudioPlayerManager();
    playerManager.setFrameBufferDuration(300);
    playerManager.getConfiguration().setFilterHotSwapEnabled(true);
    playerManager.getConfiguration().setOpusEncodingQuality(10);
    playerManager.getConfiguration().setFrameBufferFactory(NonAllocatingAudioFrameBuffer::new);

    eventManager.registerEventHandler(DiscordEvent.CHANNEL_DELETE, this::onChannelDelete);
    eventManager.registerEventHandler(DiscordEvent.GUILD_DELETE, this::onLeave);
    eventManager.registerEventHandler(DiscordEvent.VOICE_STATE_UPDATE, this::onVoiceStateUpdate);

    LocalPlayerWrapper.configuration = playerManager.getConfiguration();
    LocalPlayerWrapper.settings = settings;
}
 
Example #2
Source File: TwitchStreamAudioSourceManager.java    From kyoko with MIT License 6 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    String streamName = getChannelIdentifierFromUrl(reference.identifier);
    if (streamName == null) {
        return null;
    }

    JsonBrowser channelInfo = fetchChannelInfo(streamName);

    if (channelInfo == null) {
        return AudioReference.NO_TRACK;
    } else {
        final String displayName = channelInfo.get("display_name").text();
        final String status = channelInfo.get("status").text();

        return new TwitchStreamAudioTrack(new AudioTrackInfo(
                status,
                displayName,
                Long.MAX_VALUE,
                reference.identifier,
                true,
                reference.identifier
        ), this);
    }
}
 
Example #3
Source File: SoundPlayerImpl.java    From DiscordSoundboard with Apache License 2.0 6 votes vote down vote up
private void init() {
    loadProperties();
    initializeDiscordBot();
    updateFileList();
    getUsers();

    playerManager = new DefaultAudioPlayerManager();
    playerManager.registerSourceManager(new LocalAudioSourceManager());
    playerManager.registerSourceManager(new YoutubeAudioSourceManager());
    playerManager.registerSourceManager(new VimeoAudioSourceManager());
    playerManager.registerSourceManager(new SoundCloudAudioSourceManager());

    musicPlayer = playerManager.createPlayer();
    musicPlayer.setVolume(75);

    leaveAfterPlayback = Boolean.parseBoolean(appProperties.getProperty("leaveAfterPlayback"));

    ConnectorNativeLibLoader.loadConnectorLibrary();

    initialized = true;
}
 
Example #4
Source File: BotApplicationManager.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
public BotApplicationManager() {
  guildContexts = new HashMap<>();
  controllerManager = new BotControllerManager();

  controllerManager.registerController(new MusicController.Factory());

  playerManager = new DefaultAudioPlayerManager();
  //playerManager.useRemoteNodes("localhost:8080");
  playerManager.getConfiguration().setResamplingQuality(AudioConfiguration.ResamplingQuality.LOW);
  playerManager.registerSourceManager(new YoutubeAudioSourceManager());
  playerManager.registerSourceManager(SoundCloudAudioSourceManager.createDefault());
  playerManager.registerSourceManager(new BandcampAudioSourceManager());
  playerManager.registerSourceManager(new VimeoAudioSourceManager());
  playerManager.registerSourceManager(new TwitchStreamAudioSourceManager());
  playerManager.registerSourceManager(new BeamAudioSourceManager());
  playerManager.registerSourceManager(new HttpAudioSourceManager());
  playerManager.registerSourceManager(new LocalAudioSourceManager());

  executorService = Executors.newScheduledThreadPool(1, new DaemonThreadFactory("bot"));
}
 
Example #5
Source File: MusicManager.java    From Shadbot with GNU General Public License v3.0 6 votes vote down vote up
private MusicManager() {
    this.audioPlayerManager = new DefaultAudioPlayerManager();
    this.audioPlayerManager.getConfiguration().setFrameBufferFactory(NonAllocatingAudioFrameBuffer::new);
    this.audioPlayerManager.getConfiguration().setFilterHotSwapEnabled(true);
    AudioSourceManagers.registerRemoteSources(this.audioPlayerManager);
    this.guildMusics = new ConcurrentHashMap<>();
    this.guildJoining = new ConcurrentHashMap<>();

    //IPv6 rotation config
    final String ipv6Block = CredentialManager.getInstance().get(Credential.IPV6_BLOCK);
    if (!Config.IS_SNAPSHOT && ipv6Block != null && !ipv6Block.isBlank()) {
        LOGGER.info("Configuring YouTube IP rotator");
        final List<IpBlock> blocks = Collections.singletonList(new Ipv6Block(ipv6Block));
        final AbstractRoutePlanner planner = new RotatingNanoIpRoutePlanner(blocks);

        new YoutubeIpRotatorSetup(planner)
                .forSource(this.audioPlayerManager.source(YoutubeAudioSourceManager.class))
                .setup();
    }
}
 
Example #6
Source File: LocalPlayerDemo.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws LineUnavailableException, IOException {
  AudioPlayerManager manager = new DefaultAudioPlayerManager();
  AudioSourceManagers.registerRemoteSources(manager);
  manager.getConfiguration().setOutputFormat(COMMON_PCM_S16_BE);

  AudioPlayer player = manager.createPlayer();

  manager.loadItem("ytsearch: epic soundtracks", new FunctionalResultHandler(null, playlist -> {
    player.playTrack(playlist.getTracks().get(0));
  }, null, null));

  AudioDataFormat format = manager.getConfiguration().getOutputFormat();
  AudioInputStream stream = AudioPlayerInputStream.createStream(player, format, 10000L, false);
  SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat());
  SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

  line.open(stream.getFormat());
  line.start();

  byte[] buffer = new byte[COMMON_PCM_S16_BE.maximumChunkSize()];
  int chunkSize;

  while ((chunkSize = stream.read(buffer)) >= 0) {
    line.write(buffer, 0, chunkSize);
  }
}
 
Example #7
Source File: SoundCloudAudioSourceManager.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  AudioItem track = processAsSingleTrack(reference);

  if (track == null) {
    track = playlistLoader.load(reference.identifier, httpInterfaceManager, this::buildTrackFromInfo);
  }

  if (track == null) {
    track = processAsLikedTracks(reference);
  }

  if (track == null && allowSearch) {
    track = processAsSearchQuery(reference);
  }

  return track;
}
 
Example #8
Source File: RemoteNodeProcessor.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
/**
 * @param playerManager Audio player manager
 * @param nodeAddress Address of this node
 * @param scheduledExecutor Scheduler to use to schedule reconnects
 * @param httpInterfaceManager HTTP interface manager to use for communicating with node
 * @param abandonedTrackManager Abandoned track manager, where the playing tracks are sent if node goes offline
 */
public RemoteNodeProcessor(DefaultAudioPlayerManager playerManager, String nodeAddress,
                           ScheduledThreadPoolExecutor scheduledExecutor, HttpInterfaceManager httpInterfaceManager,
                           AbandonedTrackManager abandonedTrackManager) {

  this.playerManager = playerManager;
  this.nodeAddress = nodeAddress;
  this.scheduledExecutor = scheduledExecutor;
  this.httpInterfaceManager = httpInterfaceManager;
  this.abandonedTrackManager = abandonedTrackManager;
  queuedMessages = new LinkedBlockingQueue<>();
  playingTracks = new ConcurrentHashMap<>();
  mapper = new RemoteMessageMapper();
  threadRunning = new AtomicBoolean();
  connectionState = new AtomicInteger(ConnectionState.OFFLINE.id());
  tickHistory = new ArrayDeque<>(NODE_REQUEST_HISTORY);
  closed = false;
}
 
Example #9
Source File: Main.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private Main() {
  this.musicManagers = new ConcurrentHashMap<>();

  this.playerManager = new DefaultAudioPlayerManager();
  AudioSourceManagers.registerRemoteSources(playerManager);
  AudioSourceManagers.registerLocalSource(playerManager);
}
 
Example #10
Source File: HttpAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  AudioReference httpReference = getAsHttpReference(reference);
  if (httpReference == null) {
    return null;
  }

  if (httpReference.containerDescriptor != null) {
    return createTrack(AudioTrackInfoBuilder.create(reference, null).build(), httpReference.containerDescriptor);
  } else {
    return handleLoadResult(detectContainer(httpReference));
  }
}
 
Example #11
Source File: BeamAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  String streamName = getChannelNameFromUrl(reference.identifier);
  if (streamName == null) {
    return null;
  }

  JsonBrowser channelInfo = fetchStreamChannelInfo(streamName);

  if (channelInfo == null) {
    return AudioReference.NO_TRACK;
  } else {
    String displayName = channelInfo.get("name").text();
    String id = getPlayedStreamId(channelInfo);

    if (displayName == null || id == null) {
      throw new IllegalStateException("Expected id and name fields from Beam channel info.");
    }

    return new BeamAudioTrack(new AudioTrackInfo(
        displayName,
        streamName,
        Units.DURATION_MS_UNKNOWN,
        id + "|" + streamName + "|" + reference.identifier,
        true,
        "https://beam.pro/" + streamName
    ), this);
  }
}
 
Example #12
Source File: YoutubeAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  try {
    return loadItemOnce(reference);
  } catch (FriendlyException exception) {
    // In case of a connection reset exception, try once more.
    if (HttpClientTools.isRetriableNetworkException(exception.getCause())) {
      return loadItemOnce(reference);
    } else {
      throw exception;
    }
  }
}
 
Example #13
Source File: GetyarnAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  final Matcher m = GETYARN_REGEX.matcher(reference.identifier);

  if (!m.matches()) {
    return null;
  }

  return extractVideoUrlFromPage(reference);
}
 
Example #14
Source File: RemoteNodeManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
/**
 * @param playerManager Audio player manager
 */
public RemoteNodeManager(DefaultAudioPlayerManager playerManager) {
  this.playerManager = playerManager;
  this.httpInterfaceManager = RemoteNodeProcessor.createHttpInterfaceManager();
  this.processors = new ArrayList<>();
  this.abandonedTrackManager = new AbandonedTrackManager();
  this.enabled = new AtomicBoolean();
  this.lock = new Object();
  this.activeProcessors = new ArrayList<>();
}
 
Example #15
Source File: VimeoAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  if (!trackUrlPattern.matcher(reference.identifier).matches()) {
    return null;
  }

  try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) {
    return loadFromTrackPage(httpInterface, reference.identifier);
  } catch (IOException e) {
    throw new FriendlyException("Loading Vimeo track information failed.", SUSPICIOUS, e);
  }
}
 
Example #16
Source File: PlayingTrackManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Autowired
public PlayingTrackManager(StatisticsManager statisticsManager) {
  this.statisticsManager = statisticsManager;
  manager = new DefaultAudioPlayerManager();
  tracks = new ConcurrentHashMap<>();

  manager.setUseSeekGhosting(false);
  AudioSourceManagers.registerRemoteSources(manager);
}
 
Example #17
Source File: Main.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private Main() {
  this.musicManagers = new HashMap<>();

  this.playerManager = new DefaultAudioPlayerManager();
  AudioSourceManagers.registerRemoteSources(playerManager);
  AudioSourceManagers.registerLocalSource(playerManager);
}
 
Example #18
Source File: PlayerControl.java    From Yui with Apache License 2.0 5 votes vote down vote up
public PlayerControl()
{
    java.util.logging.Logger.getLogger("org.apache.http.client.protocol.ResponseProcessCookies").setLevel(Level.OFF);

    this.playerManager = new DefaultAudioPlayerManager();
    playerManager.registerSourceManager(new YoutubeAudioSourceManager());
    playerManager.registerSourceManager(new SoundCloudAudioSourceManager());
    playerManager.registerSourceManager(new BandcampAudioSourceManager());
    playerManager.registerSourceManager(new VimeoAudioSourceManager());
    playerManager.registerSourceManager(new TwitchStreamAudioSourceManager());
    playerManager.registerSourceManager(new HttpAudioSourceManager());
    playerManager.registerSourceManager(new LocalAudioSourceManager());

    musicManagers = new HashMap<String, GuildMusicManager>();
}
 
Example #19
Source File: PlaylistAudioSourceManager.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@PostConstruct
public void init() {
    try {
        sourceMethod = DefaultAudioPlayerManager.class.getDeclaredMethod("checkSourcesForItem",
                AudioReference.class, AudioLoadResultHandler.class, boolean[].class);
        sourceMethod.setAccessible(true);
    } catch (NoSuchMethodException e) {
        log.warn("Could not get LavaPlayer init method");
    }
}
 
Example #20
Source File: YoutubeAudioSourceManager.java    From kyoko with MIT License 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    try {
        return loadItemOnce(reference);
    } catch (FriendlyException exception) {
        // In case of a connection reset exception, try once more.
        if (HttpClientTools.isRetriableNetworkException(exception.getCause())) {
            return loadItemOnce(reference);
        } else {
            throw exception;
        }
    }
}
 
Example #21
Source File: SpotifyAudioSourceManager.java    From kyoko with MIT License 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    refreshToken();
    if (accessToken == null)
        return null;

    AudioItem item;
    for (Function<AudioReference, AudioItem> loader : loaders) {
        if ((item = loader.apply(reference)) != null)
            return item;
    }
    return null;
}
 
Example #22
Source File: NicoAudioSourceManager.java    From kyoko with MIT License 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    Matcher trackMatcher = trackUrlPattern.matcher(reference.identifier);

    if (trackMatcher.matches()) {
        return loadTrack(trackMatcher.group(1));
    }

    return null;
}
 
Example #23
Source File: SafeHttpAudioSourceManager.java    From kyoko with MIT License 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    AudioReference httpReference = getAsHttpReference(reference);
    if (httpReference == null) {
        return null;
    }

    if (httpReference.containerDescriptor != null) {
        return createTrack(AudioTrackInfoBuilder.create(reference, null).build(), httpReference.containerDescriptor);
    } else {
        return handleLoadResult(detectContainer(httpReference));
    }
}
 
Example #24
Source File: ExHttpAudioSourceManager.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    try {
        URI uri = new URI(reference.identifier);
        if (allowedHosts.contains(uri.getHost()) || StringUtils.endsWithAny(uri.getHost(), allowedHighDomains)) {
            return super.loadItem(manager, reference);
        }
    } catch (URISyntaxException e) {
        // fall down
    }
    return null;
}
 
Example #25
Source File: NicoAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  Matcher trackMatcher = trackUrlPattern.matcher(reference.identifier);

  if (trackMatcher.matches()) {
    return loadTrack(trackMatcher.group(1));
  }

  return null;
}
 
Example #26
Source File: PlaylistAudioSourceManager.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Transactional
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
    String uuid = extractUUID(reference.identifier);
    if (uuid == null) {
        return null;
    }
    Playlist playlist = playlistService.getPlaylist(uuid);
    if (playlist == null) {
        return null;
    }
    List<PlaylistItem> items = new ArrayList<>(playlist.getItems());
    if (CollectionUtils.isNotEmpty(items)) {
        List<AudioTrack> tracks = new ArrayList<>(items.size());
        items.forEach(e -> {
            try {
                AudioTrack track = getTracksFor(manager, e);
                if (track != null) {
                    tracks.add(track);
                }
            } catch (Exception ex) {
                // fall down and ignore it
            }
        });
        if (!tracks.isEmpty()) {
            storedPlaylistService.refreshStoredPlaylist(playlist, tracks);
            return new StoredPlaylist(playlist, tracks);
        }
    }
    onError(playlist, "discord.command.audio.playlist.notFound");
    return null;
}
 
Example #27
Source File: PlaylistAudioSourceManager.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
private boolean checkSourcesForItem(DefaultAudioPlayerManager manager, AudioReference reference, AudioLoadResultHandler resultHandler) {
    if (sourceMethod == null) {
        return false;
    }
    try {
        Object result = sourceMethod.invoke(manager, reference, resultHandler, new boolean[1]);
        return Boolean.TRUE.equals(result);
    } catch (IllegalAccessException | InvocationTargetException e) {
        log.warn("Could not invoke checkSourcesForItem of LavaPlayer");
        return false;
    }
}
 
Example #28
Source File: Discord.java    From DiscordBot with Apache License 2.0 5 votes vote down vote up
public Discord() {
	command = new Command();
	audioPlayerManager = new DefaultAudioPlayerManager();
	audioQueue = new AudioQueue();
	messageSender = new MessageSender();
	discordThread = new DiscordThread();
}
 
Example #29
Source File: LocalAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  File file = new File(reference.identifier);

  if (file.exists() && file.isFile() && file.canRead()) {
    return handleLoadResult(detectContainerForFile(reference, file));
  } else {
    return null;
  }
}
 
Example #30
Source File: BandcampAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
  if (trackUrlPattern.matcher(reference.identifier).matches()) {
    return loadTrack(reference.identifier);
  } else if (albumUrlPattern.matcher(reference.identifier).matches()) {
    return loadAlbum(reference.identifier);
  }
  return null;
}