Skip to content

Commit

Permalink
Allow tracks to end at a custom time (lavalink-devs#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
schnapster authored and freyacodes committed Sep 25, 2017
1 parent 02c8c40 commit 71ef03f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
4 changes: 3 additions & 1 deletion IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ Response to `isConnectedRes`.

Cause the player to play a track.
`startTime` is an optional setting that determines the number of milliseconds to offset the track by. Defaults to 0.
`endTime` is an optional setting that determines at the number of milliseconds at which point the track should stop playing. Helpful if you only want to play a snippet of a bigger track. By default the track plays until it's end as per the encoded data.
```json
{
"op": "play",
"guildId": "...",
"track": "...",
"startTime": "60000"
"startTime": "60000",
"endTime": "120000"
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,17 @@ public AudioTrack getPlayingTrack() {
public void playTrack(AudioTrack track) {
try {
position = track.getPosition();
TrackData trackData = track.getUserData(TrackData.class);

JSONObject json = new JSONObject();
json.put("op", "play");
json.put("guildId", link.getGuildId());
json.put("track", LavalinkUtil.toMessage(track));
json.put("startTime", position);
if (trackData != null) {
json.put("startTime", trackData.startPos);
json.put("endTime", trackData.endPos);
}
json.put("pause", paused);
if (link.getCurrentSocket() != null)
link.getCurrentSocket().send(json.toString());
Expand Down
38 changes: 38 additions & 0 deletions LavalinkClient/src/main/java/lavalink/client/player/TrackData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lavalink.client.player;
/*
* Copyright (c) 2017 Frederik Ar. Mikkelsen & NoobLance
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Created by napster on 25.09.17.
* <p>
* Optional object to enrich an AudioTrack via {@code AudioTrack#setUserData}
*/
public class TrackData {

public final long startPos;
public final long endPos;

public TrackData(long startPos, long endPos) {
this.startPos = startPos;
this.endPos = endPos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
package lavalink.server.io;

import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.TrackMarker;
import lavalink.server.player.Player;
import lavalink.server.player.TrackEndMarkerHandler;
import lavalink.server.util.Util;
import net.dv8tion.jda.manager.AudioManager;
import org.java_websocket.WebSocket;
Expand Down Expand Up @@ -152,6 +154,9 @@ public void onMessage(WebSocket webSocket, String s) {
if (json.has("startTime")) {
track.setPosition(json.getLong("startTime"));
}
if (json.has("endTime")) {
track.setMarker(new TrackMarker(json.getLong("endTime"), new TrackEndMarkerHandler(player)));
}

if (json.optBoolean("pause", false)) {
player.setPause(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lavalink.server.player;
/*
* Copyright (c) 2017 Frederik Ar. Mikkelsen & NoobLance
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/


import com.sedmelluq.discord.lavaplayer.track.TrackMarkerHandler;

public class TrackEndMarkerHandler implements TrackMarkerHandler {

private final Player player;

public TrackEndMarkerHandler(Player player) {
this.player = player;
}

@Override
public void handle(MarkerState state) {
if (state.equals(MarkerState.REACHED) | state.equals(MarkerState.BYPASSED))
player.stop();
}
}

0 comments on commit 71ef03f

Please sign in to comment.