I have recently started working with JavaFX and I've got a kind of a problem with audioclip and exception handling.
When I tried on a computer which has some problem on the audio system, the exception was thrown.
Exception thrown:
Exception in thread "Thread-4" com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:274)
at javafx.media/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaAudioClipPlayer.play(NativeMediaAudioClipPlayer.java:319)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaAudioClipPlayer.clipScheduler(NativeMediaAudioClipPlayer.java:112)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaAudioClipPlayer.access$000(NativeMediaAudioClipPlayer.java:47)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaAudioClipPlayer$Enthreaderator.lambda$static$0(NativeMediaAudioClipPlayer.java:85)
at java.base/java.lang.Thread.run(Thread.java:844)
Here is my implementation snippet
Also, I tried Throwable
. But something is wrong in my code.
Implementation code
try{
audio = new AudioClip(new File("Ding.mp3").toURI().toString());
audio.play();
Thread.sleep(1000);
}catch(Exception e){
System.out.println("can't play audio");
}
Import section
import java.io.File;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.paint.*;
import javafx.scene.image.*;
import javafx.scene.effect.*;
import javafx.scene.text.*;
import javafx.scene.input.*;
import javafx.scene.canvas.*;
import javafx.scene.shape.*;
import javafx.scene.media.*;
import javafx.scene.media.AudioClip;
import javafx.stage.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.collections.*;
import java.util.*;
import java.net.MalformedURLException;
P.s this is my whole code https://github.com/yamakataoka/Pomodoro/blob/master/Pomodoro.java
Do you know how to catch it?