Skip to content

Addon for plugin PlasmoVoice (https://github.com/plasmoapp/plasmo-voice). With this plugin you can play mp3, wav files from url or local folder

Notifications You must be signed in to change notification settings

bivashy/PlasmoVoiceAddon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PlasmoVoiceAddon

Java Jitpack Lines of code

Spigot Downloads Github Releases Downloads

Tested Spigot version Spigot Rating

Plugin logo

PlasmoVoiceAddon is a music player that supports both MP3 and WAV formats. It allows you to play music from URLs or local files and provides an API for developers to incorporate music playback functionality.

Important: This plugin depends on the PlasmoVoice plugin. You can only play music for players who have the PlasmoVoice mod downloaded.

Installation:

  1. Download the JAR file from Spigot or GitHub releases.
  2. Place the JAR file in your Spigot plugins folder.
  3. Launch server

Plugin Commands:

  1. /musicfile <file name> - Play music from a file. The music file should be located in the PlasmoVoiceAddon folder
  2. /musicurl <URL> - Play music from a URL

Command Examples:

  1. /musicfile piano2.wav - This command will play the piano2.wav file from the PlasmoVoiceAddon folder
  2. /musicfile piano2.mp3 - This command will play the piano2.mp3 file from the PlasmoVoiceAddon folder
  3. /musicurl https://audio.jukehost.co.uk/L3HgZZAWd0GO38Ghw4xxreITjDk1krXL - This command will play music from the provided URL

Developer API:

How to play sound?

  1. Get InputStream from our source:
import java.io.File;
import java.nio.file.Files;
import java.net.URL;
import java.io.InputStream;

//  From file:
File file = //Your music file
InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()));


// From URL:
URL url = //Your music url
InputStream inputStream = new BufferedInputStream(url.openStream());
  1. Find right ISoundFormat for InputStream
import com.bivashy.plasmovoice.PlasmoVoiceAddon;
import com.bivashy.plasmovoice.sound.ISoundFormat;

PlasmoVoiceAddon addon = PlasmoVoiceAddon.getPlugin(PlasmoVoiceAddon.class);
Optional<ISoundFormat> soundFormat = addon.getSoundFormatHolder().findFirstByPredicate(sound->sound.isSupported(bufferedInputStream));
  1. Play ISound with AudioSource, SoundController (currently only PlayerAudioSource available)
import com.bivashy.plasmovoice.PlasmoVoiceAddon;
import org.bukkit.entity.Player;
import com.bivashy.plasmovoice.sound.ISound;
import com.bivashy.plasmovoice.audio.player.PlasmoVoiceSoundPlayer;

if(!soundFormat.isPresent())
        // handle unknown sound format
PlasmoVoiceAddon addon=PlasmoVoiceAddon.getPlugin(PlasmoVoiceAddon.class);
Player player= //Your player
int soundDistance=100; //Maximum distance that will be music played

PlasmoVoiceSoundPlayer soundPlayer=addon.getPlasmoVoiceSoundPlayer(player.getUniqueId());
ISound sound = soundFormat.get().newSoundFactory().createSound(inputStream); // This line will automatically close InputStream, after you cannot use stream!
IPlasmoVoiceSoundController soundController = IPlasmoVoiceSoundController.of(soundFormat.get(),soundDistance));
soundPlayer.playSound(sound,soundController);

How to add custom sound format (For example Mp3)

  1. Create implementation of ISoundFormat
import com.bivashy.plasmovoice.sound.ISoundFormat;
import com.bivashy.plasmovoice.settings.config.MusicPlayerSettings;

public class Mp3SoundFormat implements ISoundFormat {

    @Override
    public boolean isSupported(InputStream audioStream) {
        //Check if audio is supported
    }

    @Override
    public MusicPlayerSettings getSettings() {
        return new MusicPlayerSettings(sleepDelay, volume, encoderBitrate, soundDistance, shouldUseCaching);
    }

    @Override
    public ISoundFactory newSoundFactory() {
        return new Mp3SoundFactory(this);
    }

}
  1. Create implementation of ISoundFactory, that will convert your format into Opus audio format
import java.util.List;
import java.util.ArrayList;
import java.io.InputStream;

import com.bivashy.plasmovoice.sound.ISoundFactory;
import com.bivashy.plasmovoice.sound.ISoundFormat;
import com.bivashy.plasmovoice.sound.ISound;
import com.bivashy.plasmovoice.audio.codecs.OpusCodecHolder;

public class Mp3SoundFactory implements ISoundFactory {

    protected final OpusCodecHolder opusCodecHolder = OpusCodecHolder.newCodecHolder();
    protected final ISoundFormat soundFormat;

    public Mp3SoundFactory(ISoundFormat soundFormat) {
        this.soundFormar = soundFormat;
    }

    @Override
    public ISound createSound(InputStream audioStream) {
        byte[] yourSoundData = new byte[0];// Create sound data from audioStream, for example with library
        List<byte[]> splittedSoundData = // Split your byte[] opusCodecHolder.getFrameSize()
        List<byte[]> encodedSoundData = new ArrayList<>();
        for (byte[] chunkData : splittedSoundData) {
            encodedSoundData.add(opusCodecHolder.encode(chunkData));
        }
        audioStream.close(); // Close stream
        opusCodecHolder.closeEncoder(); // Close encoder, because it uses native library
        return ISound.of(encodedSoundData, soundFormat);
    }

}   
  1. Register your ISoundFormat
PlasmoVoiceAddon addon = PlasmoVoiceAddon.getPlugin(PlasmoVoiceAddon.class);
addon.getSoundFormatHolder().add(new Mp3SoundFormat());

About

Addon for plugin PlasmoVoice (https://github.com/plasmoapp/plasmo-voice). With this plugin you can play mp3, wav files from url or local folder

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages