我正在开发一个从安卓应用程序控制的不和谐机器人。我正在使用JDA java discord 包装器和lavaplayer音频播放器。
我遇到的问题是,当我尝试打电话时,new DefaultAudioPlayerManager()
我得到
了java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/management/NotificationListener;
据我了解,这是由编译时存在的类引起的,但在运行时不存在,问题是我不知道如何解决这个问题。在网上大量搜索后,我能找到的唯一答案是“它不适用于 android”
任何事情都非常感谢。我很困。
这是代码,错误有注释“这是错误发生的地方”并且在构造函数中:
package com.example.projectprototype.music;
import android.content.Context;
import android.widget.Toast;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.managers.AudioManager;
import java.util.HashMap;
import java.util.Map;
public class musicMain extends ListenerAdapter {
private final AudioPlayerManager playerManager;
private final Map<Long, GuildMusicManager> musicManagers;
private Guild guild;
private static String playing;
private VoiceChannel chan = null;
public musicMain(Guild guild) {
this.guild = guild;
this.musicManagers = new HashMap<>();
//THIS IS WHERE THE ERROR OCCURS V
this.playerManager = new DefaultAudioPlayerManager();
AudioSourceManagers.registerRemoteSources(playerManager);
AudioSourceManagers.registerLocalSource(playerManager);
}
private synchronized GuildMusicManager getGuildAudioPlayer(Guild guild) {
long guildId = Long.parseLong(guild.getId());
GuildMusicManager musicManager = musicManagers.get(guildId);
if (musicManager == null) {
musicManager = new GuildMusicManager(playerManager);
musicManagers.put(guildId, musicManager);
}
guild.getAudioManager().setSendingHandler(musicManager.getSendHandler());
return musicManager;
}
public void setChan(VoiceChannel chan)
{
this.chan = chan;
}
public void playMusic(String song)
{
loadAndPlay(song);
}
public void skipMusic()
{
skipTrack();
}
public static void setPlaying(String newSong)
{
playing = newSong;
}
public String getPlaying()
{
return playing;
}
private void loadAndPlay(final String trackUrl) {
GuildMusicManager musicManager = getGuildAudioPlayer(guild);
playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
play(musicManager, track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
AudioTrack firstTrack = playlist.getSelectedTrack();
if (firstTrack == null) {
firstTrack = playlist.getTracks().get(0);
}
play(musicManager, firstTrack);
}
@Override
public void noMatches() {
System.out.println("Nothing found by " + trackUrl);
}
@Override
public void loadFailed(FriendlyException exception) {
System.out.println("Could not play: " + exception.getMessage());
}
});
}
private void play(GuildMusicManager musicManager, AudioTrack track) {
connectToFirstVoiceChannel(guild.getAudioManager());
musicManager.scheduler.queue(track);
}
private void skipTrack() {
GuildMusicManager musicManager = getGuildAudioPlayer(guild);
musicManager.scheduler.nextTrack();
System.out.println("Skipped song");
}
private void connectToFirstVoiceChannel(AudioManager audioManager) {
if(audioManager.getConnectedChannel() == chan)
{
return;
}
else if(audioManager.isConnected()) {
audioManager.closeAudioConnection();
if (!audioManager.isConnected() && !audioManager.isAttemptingToConnect()) {
for (VoiceChannel voiceChannel : audioManager.getGuild().getVoiceChannels()) {
if (voiceChannel == chan && chan != null) {
audioManager.openAudioConnection(voiceChannel);
break;
} else if (chan == null) {
audioManager.openAudioConnection(voiceChannel);
break;
}
}
}
}
else
{
if (!audioManager.isConnected() && !audioManager.isAttemptingToConnect()) {
for (VoiceChannel voiceChannel : audioManager.getGuild().getVoiceChannels()) {
if (voiceChannel == chan && chan != null) {
audioManager.openAudioConnection(voiceChannel);
break;
} else if (chan == null) {
audioManager.openAudioConnection(voiceChannel);
break;
}
}
}
}
}
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.projectprototype, PID: 22924
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/management/NotificationListener;
at com.example.projectprototype.music.musicMain.<init>(musicMain.java:37)
at com.example.projectprototype.EventChannel.setMusicChannel(EventChannel.java:179)
at com.example.projectprototype.ui.home.HomeFragment$1.onItemSelected(HomeFragment.java:99)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:957)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:946)
at android.widget.AdapterView.access$300(AdapterView.java:55)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:910)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.management.NotificationListener" on path: DexPathList[[zip file "/data/app/com.example.projectprototype-bDIMBcMLHygmj0yt6QQ3Vw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.projectprototype-bDIMBcMLHygmj0yt6QQ3Vw==/lib/x86, /system/lib, /system/product/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.example.projectprototype.music.musicMain.<init>(musicMain.java:37)
at com.example.projectprototype.EventChannel.setMusicChannel(EventChannel.java:179)
at com.example.projectprototype.ui.home.HomeFragment$1.onItemSelected(HomeFragment.java:99)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:957)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:946)
at android.widget.AdapterView.access$300(AdapterView.java:55)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:910)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
```