如何在jsp中计算上传的mp3文件的持续时间

发布于 2024-12-29 20:11:26 字数 796 浏览 2 评论 0原文

我已经使用inf JSP 上传了一个mp3 文件。有没有办法以秒为单位查找上传的 mp3 文件的持续时间?

下面给出的是代码的一部分:

        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
            System.out.println("Content Type:"+contentType);
            System.out.println("PATAH:"+saveFile);
            System.out.println("Start :"+startPos);
            System.out.println("endPos :"+endPos);

        System.out.println("contentLength"+formDataLength);//Size of the file in Bytes

I have uploaded an mp3 file usinf JSP. Is there any way to find the duration of the uploaded mp3 file in seconds?

Given below is the part of the code:

        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
            System.out.println("Content Type:"+contentType);
            System.out.println("PATAH:"+saveFile);
            System.out.println("Start :"+startPos);
            System.out.println("endPos :"+endPos);

        System.out.println("contentLength"+formDataLength);//Size of the file in Bytes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2025-01-05 20:11:26

仅通过文件大小不可能知道音频长度,最好的方法是使用库来做到这一点。

使用 MP3 SPI: http://www.javazoom.net/mp3spi/documents.html

示例代码,返回音频文件有多少秒:

private static int getDurationWithMp3Spi(File file) {
    AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
    Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
    String key = "duration";
    Long microseconds = (Long) properties.get(key);
    int mili = (int) (microseconds / 1000);
    return (mili / 1000) % 60;
} 

It is not possible to know the audio length only by the file size, the best way is to using a library to do it.

Using MP3 SPI: http://www.javazoom.net/mp3spi/documents.html

example code, that return how many seconds the audio file has:

private static int getDurationWithMp3Spi(File file) {
    AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
    Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
    String key = "duration";
    Long microseconds = (Long) properties.get(key);
    int mili = (int) (microseconds / 1000);
    return (mili / 1000) % 60;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文