Java getAudioInputStream 正在尝试读取音频文件,正在获取 javax.sound.sampled.UnsupportedAudioFileException,
Java getAudioInputStream trying to read audio file, getting javax.sound.sampled.UnsupportedAudioFileException,
我正在尝试在我的 java 程序中加载一个 wav 文件,使用我在此处找到的代码:
How do I get a sound file's total time in Java?
但是,在第 14 行:
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
我收到错误
"Unhandled exceptions: javax.sound.sampled.UnsupportedAudioFileException, java.io.IOException"
这是我的代码:
import java.nio.file.FileSystems;
import java.io.File;
import java.nio.file.Path;
import javax.sound.sampled.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public class Main {
public static void main(String[] args) {
Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
File file = new File(path+"/sample/loop1.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
}
}
我想制作一种方法 select 音频文件中的开始播放点(例如:在 30 秒开始播放文件)。这就是为什么我不想检索文件的持续时间。
当我执行 System.out.println(file);
时,它会打印出我的文件的正确路径。
但是,由于我收到一条错误消息,显然我没有做正确的事情。
我已经尝试在网上找到解决方案,但我没有找到任何东西,所以我在这里发布。
感谢您的关注。
AudioSystem.getAudioInputStream
抛出这些异常。
这些是 checked exceptions,因此您必须使用 try ... catch
块来捕获异常或在方法声明中添加 throws
子句。
一个基本的 try ... catch
是:
try
{
Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
File file = new File(path + "/sample/loop1.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
}
catch (UnsupportedAudioFileException | IOException ex)
{
ex.printStackTrace();
}
我正在尝试在我的 java 程序中加载一个 wav 文件,使用我在此处找到的代码:
How do I get a sound file's total time in Java?
但是,在第 14 行:
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
我收到错误
"Unhandled exceptions: javax.sound.sampled.UnsupportedAudioFileException, java.io.IOException"
这是我的代码:
import java.nio.file.FileSystems;
import java.io.File;
import java.nio.file.Path;
import javax.sound.sampled.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public class Main {
public static void main(String[] args) {
Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
File file = new File(path+"/sample/loop1.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
}
}
我想制作一种方法 select 音频文件中的开始播放点(例如:在 30 秒开始播放文件)。这就是为什么我不想检索文件的持续时间。
当我执行 System.out.println(file);
时,它会打印出我的文件的正确路径。
但是,由于我收到一条错误消息,显然我没有做正确的事情。
我已经尝试在网上找到解决方案,但我没有找到任何东西,所以我在这里发布。
感谢您的关注。
AudioSystem.getAudioInputStream
抛出这些异常。
这些是 checked exceptions,因此您必须使用 try ... catch
块来捕获异常或在方法声明中添加 throws
子句。
一个基本的 try ... catch
是:
try
{
Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
File file = new File(path + "/sample/loop1.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
}
catch (UnsupportedAudioFileException | IOException ex)
{
ex.printStackTrace();
}