【问题标题】:how to calculate the duration of an uploaded mp3 file in jspjsp中如何计算上传的mp3文件的时长
【发布时间】:2012-01-27 11:12:23
【问题描述】:

我已经上传了一个使用 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

【问题讨论】:

    标签: java jsp audio mp3


    【解决方案1】:

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

    使用 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;
    } 
    

    【讨论】:

    • 它给出了这样的异常“javax.sound.sampled.UnsupportedAudioFileException: file is not a supported file type”
    • 是的,就是这样。您必须检查您的应用程序是否正在加载/使用 JMF。
    • 已安装 JMF,仍然无法正常工作。这就是我所做的。 File n=new File(文件路径);然后我已将此文件“n”传递给函数。我正在使用 net beans 来运行 JSP。
    • 可能是,我不知道如何,你必须包含 JMF jars 和文件。
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2010-11-08
    • 2014-05-18
    • 2018-01-27
    • 1970-01-01
    相关资源
    最近更新 更多