gpt4 book ai didi

java - 如何在Java中播放长声音文件?没有语法错误但没有声音

转载 作者:行者123 更新时间:2023-11-30 07:40:45 28 4
gpt4 key购买 nike

所以我对 OOP 总体来说是新手,尤其是 Swing 类。我的问题是,虽然我的代码编译没有错误,但当我播放一些音乐的长声音文件时没有声音。有没有办法解决这个问题,因为我使用的是一个波形文件,该文件存储在我能想到的构成代码的每个可能的文件夹中。此外,自从我阅读了一些有关小端字节序的帖子以来,是否需要一种特定类型的波形格式(我不知道这意味着什么)...

代码转到 try block ,但随后“跳过”接下来的四行,就好像它们根本不存在一样。

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;

public class Sound {
Clip clip = null;

public Sound(String file) {

try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(file));
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
System.out.println("Done!");
} catch (Exception err) {
err.printStackTrace();
}
}
}

如果可能的话,用外行的话来说。谢谢。 :)

注意:我想强调这样一个事实:我不希望得到实现 sun.audio 包的答案,因为它对于 Java 来说显然“过时”了。

编辑:我发现了一个使用线程循环的尴尬解决方案:只要音乐正在播放,线程就会 hibernate 15 秒。

最佳答案

这可以使用 java 渲染单声道(单 channel )wav 音频

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class PlayAudio {
private static boolean tryToInterruptSound = false;
private static long mainTimeOut = 3000;
private static long startTime = System.currentTimeMillis();

public static synchronized Thread PlayAudio(final File file) {

Thread soundThread = new Thread() {
@Override
public void run() {
try{
Clip clip = null;
AudioInputStream inputStream = null;
clip = AudioSystem.getClip();
inputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = inputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
long durationInMiliSeconds =
(long) (((float)audioFileLength / (frameSize * frameRate)) * 1000);

clip.open(inputStream);
clip.start();
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound started playing!");
Thread.sleep(durationInMiliSeconds);
while (true) {
if (!clip.isActive()) {
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound got to it's end!");
break;
}
long fPos = (long)(clip.getMicrosecondPosition() / 1000);
long left = durationInMiliSeconds - fPos;
System.out.println("" + (System.currentTimeMillis() - startTime) + ": time left: " + left);
if (left > 0) Thread.sleep(left);
}
clip.stop();
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound stoped");
clip.close();
inputStream.close();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound interrupted while playing.");
}
}
};
soundThread.setDaemon(true);
soundThread.start();
return soundThread;
}

public static void main(String[] args) {

Thread soundThread = PlayAudio(new File("/home/scott/Dropbox/Documents/data/audio/Elephant_sounds_mono_rgUFu_hVhlk.wav"));
System.out.println("" + (System.currentTimeMillis() - startTime) + ": playSound returned, keep running the code");
try {
Thread.sleep(mainTimeOut );
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tryToInterruptSound) {
try {
soundThread.interrupt();
Thread.sleep(1);
// Sleep in order to let the interruption handling end before
// exiting the program (else the interruption could be handled
// after the main thread ends!).
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("" + (System.currentTimeMillis() - startTime) + ": End of main thread; exiting program " +
(soundThread.isAlive() ? "killing the sound deamon thread" : ""));
}
}

现在输入的 wav 文件名是硬编码的......享受

关于java - 如何在Java中播放长声音文件?没有语法错误但没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735221/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com