Не воспроизводится звук

153
09 января 2021, 09:10

я использую следующий код:

package main;
import javax.sound.sampled.*;
import java.io.*;
public class Player {
    public void play(String file) {
        File f = new File(file);
        AudioInputStream tr = null;
        try {
            tr = AudioSystem.getAudioInputStream(f);
            Clip clip = AudioSystem.getClip();
            clip.open();
            clip.setFramePosition(0);
            clip.start();
            Thread.sleep(clip.getMicrosecondLength()/1000);
            clip.stop(); //Останавливаем
            clip.close(); //Закрываем
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

При попытке вызова метода play, в консоль выводится следующее:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8300(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
    ... 32 more
Caused by: java.lang.IllegalArgumentException: illegal call to open() in interface Clip
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(Unknown Source)
    at java.desktop/com.sun.media.sound.AbstractDataLine.open(Unknown Source)
    at java.desktop/com.sun.media.sound.AbstractDataLine.open(Unknown Source)
    at main.Player.play(Player.java:14)
    at main.Controller.play(Controller.java:11)
    ... 42 more
Answer 1

Открываем документацию и видим, что метод open должен принимать аргумент

void open(AudioInputStream stream)
   throws LineUnavailableException,
          IOException

Так что должно быть так

clip.open(tr);
READ ALSO
Удалить одно поле из HashMap при помощи Stream API

Удалить одно поле из HashMap при помощи Stream API

У меня есть JSON с такой структурой:

115
toString (коллекции)

toString (коллекции)

Этот код выведет в консоль:

91
Spring Boot задержка выполнения метода

Spring Boot задержка выполнения метода

У меня есть клиент-серверное приложениеСерверная часть написана на Spring boot

97
Java Дженерики. Растолкуйте объявление

Java Дженерики. Растолкуйте объявление

Java ДженерикиРастолкуйте объявление для чайников, сигнатуру объявления что делает ? super T читаю, не особо могу вникнуть

108