JavaFX AudioClip MEDIA_UNSUPPORTED:无法识别的文件签名,更新了不赞成使用的代码

时间:2018-12-31 22:56:03

标签: java audio javafx audioclip

基本上,我正在尝试将已弃用的Java Applet内容的一些旧代码更新为javafx,我认为主要问题是由于Javafx的audioclip不支持“ .au”文件而导致的,我想知道是否有办法修复它/使用其他方法在javafx中播放音频,而无需实际转换文件类型。

原始代码:

import java.applet.*;

public class AnnounceTimeOnSeparateThread implements Runnable{
	private AudioClip[] hourAudio = new AudioClip[12];
	private AudioClip[] minuteAudio = new AudioClip[60];
	private AudioClip amAudio = Applet.newAudioClip(this.getClass().getResource("/audio/am.au"));
	private AudioClip pmAudio = Applet.newAudioClip(this.getClass().getResource("/audio/pm.au"));
	private int hour;
	private int minute;
	public AnnounceTimeOnSeparateThread() {
		for (int i = 0; i < 12; i++)
			hourAudio[i] = Applet.newAudioClip(this.getClass().getResource("/audio/hour" + i + ".au"));
		for (int i = 0; i < 60; i++)
			minuteAudio[i] = Applet.newAudioClip(this.getClass().getResource("/audio/minute" + i + ".au"));
	}

	public void setHour(int hour) {
		this.hour=hour;
	}
	public void setMinute(int minute) {
		this.minute=minute;
	}
	public void run() {
		try{ 
			hourAudio[hour % 12].play();
			Thread.sleep(1500);
			minuteAudio[minute].play();
			Thread.sleep(1500);
			}catch (InterruptedException ex) {
			}
		if (hour < 12)
			amAudio.play();
		else
			pmAudio.play();
		}
	}

我的更新尝试出现错误: (基本上是小的更改,只是将其更改为fx语法并导入))

import javafx.scene.media.AudioClip;

public class AnnounceTimeOnSeparateThread implements Runnable {
	private AudioClip[] hourAudio = new AudioClip[12];
	private AudioClip[] minuteAudio = new AudioClip[60];
	private AudioClip amAudio = new AudioClip(this.getClass().getResource("/audio/am.au").toExternalForm());
	private AudioClip pmAudio =new AudioClip(this.getClass().getResource("/audio/pm.au").toExternalForm());
	private int hour;
	private int minute;

	public AnnounceTimeOnSeparateThread() {
		for (int i = 0; i < 12; i++)
			hourAudio[i] = new AudioClip(this.getClass().getResource("/audio/hour" + i + ".au").toExternalForm());
		for (int i = 0; i < 60; i++)
			minuteAudio[i] = new AudioClip(this.getClass().getResource("/audio/minute" + i + ".au").toExternalForm());
	}

0 个答案:

没有答案