通过使用URL获得云端音频长度

业务需求:前端提供URL,后端需要在不下载音频的情况下计算出音频的长度,并向上取整
解决方法:通过URL和AudioInputStream来获得音频流,分析头并计算

	URL audioURL = new URL(getSpeechRequest.getAudioUrl());
	audioInputStream = AudioSystem.getAudioInputStream(audioURL);
	
	AudioFormat format = audioInputStream.getFormat();
	long frames = audioInputStream.getFrameLength();
	long durationInSeconds = (long) Math.ceil((frames+0.0) / format.getFrameRate()); //向上取整,比如0.1算1

参考资料:https://stackoverflow.com/questions/51836347/getting-an-audio-duration-from-an-url-in-java

你可能感兴趣的:(音视频,java,jvm)