繁体   English   中英

如何从 Kotlin / Java 中的网站链接获取音频文件的长度?

[英]How to get the length of an audio file from a link to a website in Kotlin / Java?

我正在尝试获取音频文件的长度。 可悲的是,我在尝试检索该文件时遇到了一些问题。 这是我的代码(在 Kotlin 中):

val inputStream = AudioSystem.getAudioInputStream(URL(url))
val format = inputStream.format

val durationSeconds = inputStream.frameLength / format.frameRate
lengthTicks = (durationSeconds * 20).toDouble()

我使用的链接是https://cdn.bandithemepark.net/lindburgh/HyperionHal.mp3当我的代码运行时,我得到“UnsupportedAudioFileException: URL of unsupported format”

我不确定为什么会出现此错误,因为 MP3 对我来说看起来像是一种非常正常的文件格式。 我也尝试使用 MP4 文件,但我得到了同样的错误。 有人知道这里发生了什么吗?

根据文档

The provided reference implementation of this API supports the following features:
    Audio file formats: AIFF, AU and WAV
    Music file formats: MIDI Type 0, MIDI Type 1, and Rich Music Format (RMF)

所以看起来不支持 mp3 和 mp4。 您很可能需要一个库/插件。

决定您可能需要哪一个超出了 SO 的 scope,因为这将是基于意见的答案,不被认为是可接受的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM