一、方法1
使用前提:当前环境下配有声卡,因为Clip类需要系统的能力,系统依赖声卡硬件驱动实现的
public class VoiceTimeUtils {
/**
* time/1000000
*/
private final int R_TIME = 1000000;
/**
* HTTP
*/
private final String HTTP = "http";
/**
* HTTPS
*/
private final String HTTPS = "https";
/**
* 获取音频播放时长,支持wav格式(环境有声卡)
*
* @param wavFilePath wav文件路径,支持本地和网络HTTP路径
* @author knight-jzc
* @return 秒数
*/
public long getMicrosecondLengthForWav(String wavFilePath) throws Exception {
if (wavFilePath == null || wavFilePath.length() == 0) {
return 0;
}
String bath = wavFilePath.split(":")[0];
Clip clip = AudioSystem.getClip();
AudioInputStream ais;

文章提供了两种在Java中获取音频播放时长的方法。方法1依赖于系统声卡,适用于Windows和Mac环境,但在Linux环境中可能因缺少硬件驱动支持而失败。方法2则不需声卡,适合无声卡的Linux环境。建议在开发中优先使用方法2,因为它更具有普适性。
最低0.47元/天 解锁文章
6305





