Can't catch Exception about audioclip

Viewed 232

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?

1 Answers

You can't catch this exception because it's being thrown on a different thread, which handles the media workflow.

Unfortunately, Java doesn't provide any easy means of catching such exceptions.

You get the exception, most probably, because JavaFX can't find the file you've provided. Try first with an URL to see if it's true:

AudioClip clickSound = new AudioClip("https://github.com/sgrinev/mastering-javafx-9-10-book/raw/master/resources/mouse-click.wav");

If it works, check next question's answers about the proper resources declaration in JavaFX: How to target a file (a path to it) in Java/JavaFX

P.S.: if you are really dedicated into the catching of this exception, you can dig into the next API: https://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Related