70 lines
2.3 KiB
Java
70 lines
2.3 KiB
Java
package eu.m724.tweaks.player;
|
|
|
|
import de.maxhenkel.voicechat.api.BukkitVoicechatService;
|
|
import de.maxhenkel.voicechat.api.VoicechatServerApi;
|
|
import de.maxhenkel.voicechat.api.VolumeCategory;
|
|
import de.maxhenkel.voicechat.api.audiochannel.AudioPlayer;
|
|
import de.maxhenkel.voicechat.api.audiochannel.EntityAudioChannel;
|
|
import de.maxhenkel.voicechat.api.opus.OpusEncoderMode;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import javax.sound.sampled.AudioInputStream;
|
|
import javax.sound.sampled.AudioSystem;
|
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
import java.io.IOException;
|
|
import java.util.UUID;
|
|
|
|
public class MusicPlayer {
|
|
private static final String PLAYER_CATEGORY = "music_player";
|
|
|
|
private VoicechatServerApi voicechat = null;
|
|
|
|
private final Plugin plugin;
|
|
|
|
public MusicPlayer(Plugin plugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
public void init() {
|
|
BukkitVoicechatService service = plugin.getServer().getServicesManager().load(BukkitVoicechatService.class);
|
|
service.registerPlugin(new MyVoicechatPlugin(this));
|
|
}
|
|
|
|
void unlock(VoicechatServerApi voicechat) {
|
|
VolumeCategory category = voicechat.volumeCategoryBuilder()
|
|
.setId(PLAYER_CATEGORY)
|
|
.setName("Music players")
|
|
.build();
|
|
|
|
voicechat.registerVolumeCategory(category);
|
|
|
|
this.voicechat = voicechat;
|
|
}
|
|
|
|
public void create(Player player) {
|
|
UUID channelID = UUID.randomUUID();
|
|
EntityAudioChannel channel = voicechat.createEntityAudioChannel(channelID, voicechat.fromEntity(player));
|
|
|
|
channel.setCategory(PLAYER_CATEGORY);
|
|
channel.setDistance(10);
|
|
|
|
short[] arr;
|
|
try {
|
|
AudioInputStream audio = AudioSystem.getAudioInputStream(plugin.getResource("music.flac"));
|
|
int samples = (int) (audio.available() / audio.getFrameLength());
|
|
arr = new short[samples];
|
|
for (int i=0; i<audio.available(); i++) {
|
|
|
|
}
|
|
} catch (UnsupportedAudioFileException e) {
|
|
throw new RuntimeException(e);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
AudioPlayer audioPlayer = voicechat.createAudioPlayer(channel, voicechat.createEncoder(OpusEncoderMode.AUDIO), arr);
|
|
audioPlayer.startPlaying();
|
|
|
|
}
|
|
}
|