资源——jar包
- 遇到了很多挫折,找错历时两天终于解决…要被自己蠢哭,本来在网上各种找jar,但每次到
MediaPlayer mp = new MediaPlayer(media);
报错
java.lang.IllegalStateException: Toolkit not initialized
//逐渐疯狂 网上关于该错误的描述很少
以为是jar包太老旧,在网上各种找jar,害,终究是错了,后来发现JDK1.8自带该jar
- JDK1.8中已有资源Javafx,但为什么建立项目后不可以使用??
解决方法:
java.lang.IllegalStateException: Toolkit not initialized的解决
错误源代码
package com.cj.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import javax.sound.sampled.AudioInputStream;
import javafx.scene.media.Media;//jfxrt.jar
import javafx.scene.media.MediaPlayer;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
public class MusicUtil {
private MusicUtil music;
public String filepath;
//private int currentIndex;//当前播放歌曲的索引
//private int prevSecond;//当前播放歌曲的前一秒--设置滚动条
//private PlayBean playBean;
public MusicUtil(){}
public MusicUtil(String filepath){
this.filepath=filepath;
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
/**
* 播放歌曲
*/
public void playMusic(){
music = new MusicUtil("E://Player//sing+picture//music_zhuque.mp3");
File file = new File(music.filepath);//file音频文件
System.out.println(file);
MP3File map3File = null;
try {
//解析map3文件
map3File = new MP3File(file);
System.out.println(map3File);
//获取map3文件的头信息
URI uri = file.toURI();//获取本地的URI对象
System.out.println(uri);
//加载资源,只加载不干其他事
Media media = new Media("http://sharefs.ali.kugou.com/202110192159/e1bd6d26ee0e43c347a1ef17f34693ea/G247/M04/06/19/l4cBAF-iicaAN5xoAC_SlWgdr5Y819.mp3");//构造播放器
System.out.println(media);
MediaPlayer mp = new MediaPlayer(media);
//播放
//Exception in thread "Thread-1" java.lang.IllegalStateException: Toolkit not initialized
//2.开始播放
new Thread(){
@Override
public void run() {
mp.play();
}
}.start();
} catch (IOException | TagException | ReadOnlyFileException | CannotReadException
| InvalidAudioFrameException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MusicUtil m = new MusicUtil();
m.playMusic();
}
}
修改后:
修改内容:继承Application后重写start方法,在start方法中实现播放,而不是自己写的play函数
import java.io.File;
import java.net.URL;
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class Play extends Application{
private Play music;
public String filepath;
//private int currentIndex;//当前播放歌曲的索引
//private int prevSecond;//当前播放歌曲的前一秒--设置滚动条
//private PlayBean playBean;
public Play(){}
public Play(String filepath){
this.filepath=filepath;
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
public static void main(String[] args) throws Exception {
Play m = new Play();
m.start(null);
}
@Override
public void start(Stage primaryStage) throws Exception {
URL url = this.getClass().getClassLoader().getResource("music/music_zhuque.mp3");
System.out.println(url);
System.out.println(url.toExternalForm());
Media media = new Media(url.toExternalForm());
MediaPlayer mp = new MediaPlayer(media);
mp.play();
System.out.println("放了");
mp.stop();
System.out.println("stop");
}
}
- java.lang.IllegalStateException: Toolkit not initialized的解决2
MeidiaPlay有自己的线程 不能在线程之外使用他 初始化一个JFXPanel会启动JavaFxRuntime
final JFXPanel fx = new JFXPanel(); - 注意一些加空的判断。。否则报错空指针异常
- 声音进度条的声音值域[0,1]
BorderPane 随窗体变化而改变大小