mirror of
https://github.com/informaticker/discord-jellyfin-bot.git
synced 2024-11-23 18:21:55 +01:00
add seek functionality
This commit is contained in:
parent
4a65cceca3
commit
242ae2ffff
@ -113,7 +113,7 @@ async function playThis (message) {
|
|||||||
|
|
||||||
discordClient.user.client.voice.connections.forEach((element) => {
|
discordClient.user.client.voice.connections.forEach((element) => {
|
||||||
songPlayMessage(message, argument);
|
songPlayMessage(message, argument);
|
||||||
playbackmanager.startPlaying(element, itemID, isSummendByPlay);
|
playbackmanager.startPlaying(element, itemID, 0, isSummendByPlay);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
|
const discordclientmanager = require("./discordclientmanager");
|
||||||
const {
|
const {
|
||||||
getAudioDispatcher,
|
getAudioDispatcher,
|
||||||
setAudioDispatcher
|
setAudioDispatcher
|
||||||
} = require("./dispachermanager");
|
} = require("./dispachermanager");
|
||||||
|
const {
|
||||||
|
ticksToSeconds
|
||||||
|
} = require("./util");
|
||||||
|
|
||||||
var currentPlayingItemId;
|
var currentPlayingItemId;
|
||||||
var progressInterval;
|
var progressInterval;
|
||||||
var isPaused;
|
var isPaused;
|
||||||
|
var _disconnectOnFinish;
|
||||||
|
var _seek;
|
||||||
|
|
||||||
const jellyfinClientManager = require("./jellyfinclientmanager");
|
const jellyfinClientManager = require("./jellyfinclientmanager");
|
||||||
function streamURLbuilder (itemID, bitrate) {
|
function streamURLbuilder (itemID, bitrate) {
|
||||||
@ -15,13 +21,20 @@ function streamURLbuilder (itemID, bitrate) {
|
|||||||
return `${jellyfinClientManager.getJellyfinClient().serverAddress()}/Audio/${itemID}/universal?UserId=${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}&DeviceId=${jellyfinClientManager.getJellyfinClient().deviceId()}&MaxStreamingBitrate=${bitrate}&Container=${supportedContainers}&AudioCodec=${supportedCodecs}&api_key=${jellyfinClientManager.getJellyfinClient().accessToken()}&TranscodingContainer=ts&TranscodingProtocol=hls`;
|
return `${jellyfinClientManager.getJellyfinClient().serverAddress()}/Audio/${itemID}/universal?UserId=${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}&DeviceId=${jellyfinClientManager.getJellyfinClient().deviceId()}&MaxStreamingBitrate=${bitrate}&Container=${supportedContainers}&AudioCodec=${supportedCodecs}&api_key=${jellyfinClientManager.getJellyfinClient().accessToken()}&TranscodingContainer=ts&TranscodingProtocol=hls`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function startPlaying (voiceconnection, itemID, disconnectOnFinish) {
|
function startPlaying (voiceconnection = discordclientmanager.getDiscordClient().user.client.voice.connections.first(), itemID = currentPlayingItemId, seekTo, disconnectOnFinish = _disconnectOnFinish) {
|
||||||
isPaused = false;
|
isPaused = false;
|
||||||
|
currentPlayingItemId = itemID;
|
||||||
|
_disconnectOnFinish = disconnectOnFinish;
|
||||||
|
_seek=seekTo*1000;
|
||||||
async function playasync () {
|
async function playasync () {
|
||||||
const url = streamURLbuilder(itemID, voiceconnection.channel.bitrate);
|
const url = streamURLbuilder(itemID, voiceconnection.channel.bitrate);
|
||||||
jellyfinClientManager.getJellyfinClient().reportPlaybackStart({ userID: `${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}`, itemID: `${itemID}` });
|
setAudioDispatcher(voiceconnection.play(url ,{seek: seekTo}));
|
||||||
currentPlayingItemId = itemID;
|
console.log(seekTo, ticksToSeconds(getPostitionTicks()));
|
||||||
setAudioDispatcher(voiceconnection.play(url));
|
if(seekTo){
|
||||||
|
jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload());
|
||||||
|
}else{
|
||||||
|
jellyfinClientManager.getJellyfinClient().reportPlaybackStart({ userID: `${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}`, itemID: `${itemID}`, canSeek: true ,playSessionId: getPlaySessionId(), playMethod:getPlayMethod()});
|
||||||
|
}
|
||||||
|
|
||||||
getAudioDispatcher().on("finish", () => {
|
getAudioDispatcher().on("finish", () => {
|
||||||
if (disconnectOnFinish) {
|
if (disconnectOnFinish) {
|
||||||
@ -33,6 +46,16 @@ function startPlaying (voiceconnection, itemID, disconnectOnFinish) {
|
|||||||
}
|
}
|
||||||
playasync().catch((rsn) => { console.log(rsn); });
|
playasync().catch((rsn) => { console.log(rsn); });
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @param {Number} toSeek - where to seek in ticks
|
||||||
|
*/
|
||||||
|
function seek(toSeek = 0){
|
||||||
|
if(getAudioDispatcher()){
|
||||||
|
startPlaying(undefined,undefined,ticksToSeconds(toSeek),_disconnectOnFinish);
|
||||||
|
jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object=} disconnectVoiceConnection - Optional The voice Connection do disconnect from
|
* @param {Object=} disconnectVoiceConnection - Optional The voice Connection do disconnect from
|
||||||
*/
|
*/
|
||||||
@ -41,7 +64,7 @@ function stop (disconnectVoiceConnection) {
|
|||||||
if (disconnectVoiceConnection) {
|
if (disconnectVoiceConnection) {
|
||||||
disconnectVoiceConnection.disconnect();
|
disconnectVoiceConnection.disconnect();
|
||||||
}
|
}
|
||||||
jellyfinClientManager.getJellyfinClient().reportPlaybackStopped({ userId: jellyfinClientManager.getJellyfinClient().getCurrentUserId(), itemId: currentPlayingItemId });
|
jellyfinClientManager.getJellyfinClient().reportPlaybackStopped({ userId: jellyfinClientManager.getJellyfinClient().getCurrentUserId(), itemId: currentPlayingItemId ,playSessionId: getPlaySessionId()});
|
||||||
if (getAudioDispatcher()) { getAudioDispatcher().destroy(); }
|
if (getAudioDispatcher()) { getAudioDispatcher().destroy(); }
|
||||||
setAudioDispatcher(undefined);
|
setAudioDispatcher(undefined);
|
||||||
clearInterval(progressInterval);
|
clearInterval(progressInterval);
|
||||||
@ -63,12 +86,12 @@ function playPause () {
|
|||||||
|
|
||||||
function getPostitionTicks () {
|
function getPostitionTicks () {
|
||||||
// this is very sketchy but i dont know how else to do it
|
// this is very sketchy but i dont know how else to do it
|
||||||
return (getAudioDispatcher().streamTime - getAudioDispatcher().pausedTime) * 10000;
|
return (_seek+getAudioDispatcher().streamTime - getAudioDispatcher().pausedTime) * 10000;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPlayMethod () {
|
function getPlayMethod () {
|
||||||
// TODO figure out how to figure this out
|
// TODO figure out how to figure this out
|
||||||
return "Transcode";
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRepeatMode () {
|
function getRepeatMode () {
|
||||||
@ -94,7 +117,7 @@ function getNowPLayingQueue () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCanSeek () {
|
function getCanSeek () {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIsMuted () {
|
function getIsMuted () {
|
||||||
@ -143,5 +166,6 @@ module.exports = {
|
|||||||
stop,
|
stop,
|
||||||
playPause,
|
playPause,
|
||||||
resume,
|
resume,
|
||||||
pause
|
pause,
|
||||||
|
seek
|
||||||
};
|
};
|
||||||
|
@ -3,6 +3,11 @@ function checkJellyfinItemIDRegex (strgintomatch) {
|
|||||||
return regexresult;
|
return regexresult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ticksToSeconds (ticks){
|
||||||
|
return ticks/10000000;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
checkJellyfinItemIDRegex
|
checkJellyfinItemIDRegex,
|
||||||
|
ticksToSeconds
|
||||||
};
|
};
|
||||||
|
@ -11,22 +11,29 @@ function openSocket () {
|
|||||||
SupportedCommands: "Play,Playstate"
|
SupportedCommands: "Play,Playstate"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
jellyfinClientManager.getJellyfinClient().ajax({
|
||||||
|
|
||||||
|
type: 'POST',
|
||||||
|
|
||||||
|
url: jellyfinClientManager.getJellyfinClient().getUrl('system/info/public'),
|
||||||
|
|
||||||
|
data: JSON.stringify({}),
|
||||||
|
|
||||||
|
contentType: 'application/json'
|
||||||
|
|
||||||
|
}).then((resp)=>{console.log(resp)})
|
||||||
jellyfinClientManager.getJellyfinEvents().on(jellyfinClientManager.getJellyfinClient(), "message", (type, data) => {
|
jellyfinClientManager.getJellyfinEvents().on(jellyfinClientManager.getJellyfinClient(), "message", (type, data) => {
|
||||||
// console.log(data);
|
|
||||||
if (data.MessageType === "Play") {
|
if (data.MessageType === "Play") {
|
||||||
if (data.Data.PlayCommand === "PlayNow") {
|
if (data.Data.PlayCommand === "PlayNow") {
|
||||||
discordclientmanager.getDiscordClient().user.client.voice.connections.forEach((element) => {
|
playbackmanager.startPlaying(undefined, data.Data.ItemIds[data.Data.StartIndex || 0],0, false);
|
||||||
playbackmanager.startPlaying(element, data.Data.ItemIds[data.Data.StartIndex || 0], false);
|
|
||||||
element.on("error", (error) => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else if (data.MessageType === "Playstate") {
|
} else if (data.MessageType === "Playstate") {
|
||||||
if (data.Data.Command === "PlayPause") {
|
if (data.Data.Command === "PlayPause") {
|
||||||
playbackmanager.playPause();
|
playbackmanager.playPause();
|
||||||
} else if (data.Data.Command === "Stop") {
|
} else if (data.Data.Command === "Stop") {
|
||||||
playbackmanager.stop();
|
playbackmanager.stop();
|
||||||
|
} else if (data.Data.Command === "Seek") {
|
||||||
|
playbackmanager.seek(data.Data.SeekPositionTicks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user