3

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?

Yama
  • 31
  • 4

1 Answers1

2

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)

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • Thank you for your answer. The code you provided didn't work for me. I had the same error code. – Yama Jan 11 '19 at 10:53
  • @Yama, in this case it looks like an OS problem -- which one do you use? – Sergey Grinev Jan 11 '19 at 10:56
  • My OS: Windows 10 Education (Version: 1803) Java: jdk-10.0.2 – Yama Jan 11 '19 at 12:00
  • detecting if audio system is available or not would be also enough. But these functions (canPlayProtocol and canPlayContentType) doesn't seem not what I'm looking for. http://cr.openjdk.java.net/~almatvee/8091132/webrev.00/modules/media/src/main/java/com/sun/media/jfxmedia/MediaManager.java.html – Yama Jan 12 '19 at 01:58