Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查java中的音频文件持续时间_Java_Jfilechooser_Filefilter - Fatal编程技术网

检查java中的音频文件持续时间

检查java中的音频文件持续时间,java,jfilechooser,filefilter,Java,Jfilechooser,Filefilter,我制作了一个FileChooser程序来获取.wav文件的文件路径,但现在我想添加另一个条件。我只想获得最长为5分钟的wav文件路径。我该怎么做 您可以通过知道文件大小(以MB为单位),然后知道文件的比特率来实现。请按照以下MP3示例进行操作: MP3 Filesize: 3.89MBytes Bitrate: 128Kbits 将MB转换为KB: 3.89Mbytes * 1024 = 3983Kbytes 3989Kbytes * 8 = 31866Kbits 将Kbits转换为长度:

我制作了一个FileChooser程序来获取.wav文件的文件路径,但现在我想添加另一个条件。我只想获得最长为5分钟的wav文件路径。我该怎么做

您可以通过知道文件大小(以MB为单位),然后知道文件的比特率来实现。请按照以下MP3示例进行操作:

MP3 Filesize: 3.89MBytes
Bitrate: 128Kbits
将MB转换为KB:

3.89Mbytes * 1024 = 3983Kbytes
3989Kbytes * 8 = 31866Kbits
将Kbits转换为长度:

31866Kbits / 128Kbits per second = 249 seconds
249 seconds / 60 seconds = 4.19 Minutes

请注意,它仍然不能准确地表示持续时间,您必须考虑计算中的一些差异。

您可以通过了解文件大小(以MB为单位),然后了解文件的比特率来完成计算。请按照以下MP3示例进行操作:

MP3 Filesize: 3.89MBytes
Bitrate: 128Kbits
将MB转换为KB:

3.89Mbytes * 1024 = 3983Kbytes
3989Kbytes * 8 = 31866Kbits
将Kbits转换为长度:

31866Kbits / 128Kbits per second = 249 seconds
249 seconds / 60 seconds = 4.19 Minutes
请注意,它仍然不能准确地表示持续时间,您必须在计算中考虑一些差异。

如果您使用,您可以简单地以秒为单位获得准确的持续时间,如下所示

File target = new File("E:\\sample.wav");
AudioFile af = AudioFileIO.read(target);
AudioHeader ah = af.getAudioHeader();
System.out.println( ah.getTrackLength());
下面是一个完整的工作示例代码

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;

import java.io.File;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * Created by RAGINROSE on 9/20/2019.
 */

public class AudioTest {
    public static void main(String[] args) {

            try {

                SimpleDateFormat timeInFormat = new SimpleDateFormat("ss", Locale.UK);
                SimpleDateFormat timeOutFormat = new SimpleDateFormat("mm:ss", Locale.UK);
                SimpleDateFormat timeOutOverAnHourFormat = new SimpleDateFormat("kk:mm:ss", Locale.UK);
                String duratin;

                File target = new File("E:\\sample.wav");
                AudioFile f = AudioFileIO.read(target);
                AudioHeader ah = f.getAudioHeader();

                long trackLength = (long)ah.getTrackLength();

                Date timeIn;
                synchronized(timeInFormat) {
                    timeIn = timeInFormat.parse(String.valueOf(trackLength));
                }
                if(trackLength < 3600L) {
                    synchronized(timeOutFormat) {
                        duratin =  timeOutFormat.format(timeIn);
                    }
                } else {
                    synchronized(timeOutOverAnHourFormat) {
                        duratin = timeOutOverAnHourFormat.format(timeIn);
                    }
                }

                System.out.println("Duration in Integer : " + trackLength);
                System.out.println("Duration in String : " +duratin);

            } catch (Exception e) {
                e.printStackTrace();
            }


    }
}
导入org.jaudiotagger.audio.audio文件;
导入org.jaudiotagger.audio.AudioFileIO;
导入org.jaudiotagger.audio.AudioHeader;
导入java.io.File;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Locale;
/**
*由RAGINROSE于2019年9月20日创建。
*/
公开课听力测试{
公共静态void main(字符串[]args){
试一试{
SimpleDataFormat timeInFormat=新的SimpleDataFormat(“ss”,Locale.UK);
SimpleDataFormat timeOutFormat=新的SimpleDataFormat(“mm:ss”,Locale.UK);
SimpleDataFormat timeOutOverAnHourFormat=新的SimpleDataFormat(“kk:mm:ss”,Locale.UK);
线状杜拉汀;
文件目标=新文件(“E:\\sample.wav”);
AudioFile f=AudioFileIO.read(目标);
AudioHeader ah=f.getAudioHeader();
long trackLength=(long)ah.getTrackLength();
日期时间;
已同步(时间信息){
timeIn=timeInFormat.parse(String.valueOf(trackLength));
}
如果(轨道长度<3600L){
已同步(timeOutFormat){
duratin=timeOutFormat.format(timeIn);
}
}否则{
已同步(TimeOutOveranHour格式){
duratin=timeOutOverAnHourFormat.format(timeIn);
}
}
System.out.println(“整数持续时间:“+trackLength”);
System.out.println(“字符串中的持续时间:+duratin”);
}捕获(例外e){
e、 printStackTrace();
}
}
}
别忘了添加依赖项

<dependency>
    <groupId>net.jthink</groupId>
    <artifactId>jaudiotagger</artifactId>
    <version>2.2.3</version>
</dependency>

net.jthink
Jaudiotager
2.2.3
支持的格式包括

ogg、mp3、flac、mp4、m4a、m4p、wma、wav、ra、rm、m4b和aif

希望这将对某人有所帮助。

如果您正在使用,您可以简单地以秒为单位获得准确的持续时间,如下所示

File target = new File("E:\\sample.wav");
AudioFile af = AudioFileIO.read(target);
AudioHeader ah = af.getAudioHeader();
System.out.println( ah.getTrackLength());
下面是一个完整的工作示例代码

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;

import java.io.File;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * Created by RAGINROSE on 9/20/2019.
 */

public class AudioTest {
    public static void main(String[] args) {

            try {

                SimpleDateFormat timeInFormat = new SimpleDateFormat("ss", Locale.UK);
                SimpleDateFormat timeOutFormat = new SimpleDateFormat("mm:ss", Locale.UK);
                SimpleDateFormat timeOutOverAnHourFormat = new SimpleDateFormat("kk:mm:ss", Locale.UK);
                String duratin;

                File target = new File("E:\\sample.wav");
                AudioFile f = AudioFileIO.read(target);
                AudioHeader ah = f.getAudioHeader();

                long trackLength = (long)ah.getTrackLength();

                Date timeIn;
                synchronized(timeInFormat) {
                    timeIn = timeInFormat.parse(String.valueOf(trackLength));
                }
                if(trackLength < 3600L) {
                    synchronized(timeOutFormat) {
                        duratin =  timeOutFormat.format(timeIn);
                    }
                } else {
                    synchronized(timeOutOverAnHourFormat) {
                        duratin = timeOutOverAnHourFormat.format(timeIn);
                    }
                }

                System.out.println("Duration in Integer : " + trackLength);
                System.out.println("Duration in String : " +duratin);

            } catch (Exception e) {
                e.printStackTrace();
            }


    }
}
导入org.jaudiotagger.audio.audio文件;
导入org.jaudiotagger.audio.AudioFileIO;
导入org.jaudiotagger.audio.AudioHeader;
导入java.io.File;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Locale;
/**
*由RAGINROSE于2019年9月20日创建。
*/
公开课听力测试{
公共静态void main(字符串[]args){
试一试{
SimpleDataFormat timeInFormat=新的SimpleDataFormat(“ss”,Locale.UK);
SimpleDataFormat timeOutFormat=新的SimpleDataFormat(“mm:ss”,Locale.UK);
SimpleDataFormat timeOutOverAnHourFormat=新的SimpleDataFormat(“kk:mm:ss”,Locale.UK);
线状杜拉汀;
文件目标=新文件(“E:\\sample.wav”);
AudioFile f=AudioFileIO.read(目标);
AudioHeader ah=f.getAudioHeader();
long trackLength=(long)ah.getTrackLength();
日期时间;
已同步(时间信息){
timeIn=timeInFormat.parse(String.valueOf(trackLength));
}
如果(轨道长度<3600L){
已同步(timeOutFormat){
duratin=timeOutFormat.format(timeIn);
}
}否则{
已同步(TimeOutOveranHour格式){
duratin=timeOutOverAnHourFormat.format(timeIn);
}
}
System.out.println(“整数持续时间:“+trackLength”);
System.out.println(“字符串中的持续时间:+duratin”);
}捕获(例外e){
e、 printStackTrace();
}
}
}
别忘了添加依赖项

<dependency>
    <groupId>net.jthink</groupId>
    <artifactId>jaudiotagger</artifactId>
    <version>2.2.3</version>
</dependency>

net.jthink
Jaudiotager
2.2.3
支持的格式包括

ogg、mp3、flac、mp4、m4a、m4p、wma、wav、ra、rm、m4b和aif


希望这对某人有所帮助。

有没有捷径?像getduration()之类的?不,但是你可以用我提供的数学创建你自己的方法。
getduration()。但是怎么做呢?难道没有更短的路吗?像getduration()之类的?不,但是你可以用我提供的数学创建你自己的方法。
getduration()。但是怎么做呢?