[英]How to get progress of downloading a file with audioinputstream java
I am new to downloading a file with java and sorry if its a dumb question but I've searched for 2 days and couldn't get a solution. 我是刚开始使用Java下载文件,但是如果问题很愚蠢,请问对不起,但是我搜索了2天,却找不到解决方案。 I am using JPanel within an applet and I want to show the download progress in a progressBar.(maybe download is not the correct word it is more likely an initialization) 我在applet中使用JPanel,我想在progressBar中显示下载进度。(也许下载不是正确的单词,它更有可能是初始化)
What i want is to show a download process of a wav file from an URL.I don't mean to show the reading process(i will let the user know the file is being processed later.) i just want to get the progress of ais=AudioSystem.getAudioInputStream(url); 我要显示的是从URL下载wav文件的过程。我并不是要显示读取过程(我会让用户知道文件正在稍后处理。)我只是想了解进度ais = AudioSystem.getAudioInputStream(url);
i tried to use ProgressMonitorInputStream but this doesn't work for me, because i want to add the progress bar to my JFrame. 我试图使用ProgressMonitorInputStream,但这对我不起作用,因为我想将进度条添加到我的JFrame中。 I have looked for swingWorker but the usage of it does not give me the progress i want. 我一直在寻找swingWorker,但是它的使用并没有给我我想要的进步。
Here is my read function. 这是我的阅读功能。 I am also changing the wav format according to my needs. 我还根据需要更改了wav格式。
public void readFile() {
byte[] data = null;
AudioInputStream ais = null;
try {
URL url = new URL("http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/Samples/AFsp/M1F1-Alaw-AFsp.wav");
ais = AudioSystem.getAudioInputStream(url);
AudioFormat format = ais.getFormat();
numberOfChannels = format.getChannels();
AudioFormat TargetFormat = new AudioFormat(format.getSampleRate(), 16, numberOfChannels, true, false);
AudioInputStream desiredFormatStream = AudioSystem.getAudioInputStream(TargetFormat,ais);
data = new byte[4];
int totalSampleCount = ais.available()/(format.getSampleSizeInBits()/8);
audio = new double[totalSampleCount];
for (int i = 0; i < totalSampleCount-1; i=i+2) {
desiredFormatStream.read(data);
if(!TargetFormat.isBigEndian()){
audio[i] = ((short) (((data[1] & 0xFF) << 8) + (data[0] & 0xFF))) / ((double) MAX_16_BIT);
audio[i+1] = ((short) (((data[3] & 0xFF) << 8) + (data[2] & 0xFF))) / ((double) MAX_16_BIT);
}
else {
audio[i] = ((short) (((data[0] & 0xFF) << 8) + (data[1] & 0xFF))) / ((double) MAX_16_BIT);
audio[i+1] = ((short) (((data[2] & 0xFF) << 8) + (data[3] & 0xFF))) / ((double) MAX_16_BIT);
}
}
StdAudio.open(TargetFormat);
} catch (Exception e) {
System.out.println(e.getMessage());
throw new RuntimeException("Could not read " + filename);
}
}
Is there any way to achieve this? 有什么办法可以做到这一点?
Thanks in advance. 提前致谢。
The easiest way is to open a URLConnection
on your already present URL
-object and query the Content-Length
http-header field. 最简单的方法是在您已经存在的URL
上打开URLConnection
并查询Content-Length
http-header字段。 Use the getContentLength()
-method to do so. 使用getContentLength()
方法进行操作。
NOTE if you're downloading large files, an
int
might not be big enough to hold the content length. 注意如果要下载大文件,则int
可能不足以容纳内容长度。 Therefor,getContentLengthLong()
should be prefered. 因此,应首选getContentLengthLong()
。 This method is available since Java 7 . 从Java 7开始可以使用此方法。
This will hold the length of the file in bytes. 这将保存文件的长度(以字节为单位)。 When you're reading via AudioInputStream.read()
, this method will return the actual amount of bytes read each time. 当您通过AudioInputStream.read()
读取时,此方法将返回每次读取的实际字节数。
Pseudo-code 伪码
long totalLength = con.getContentLengthLong();
while still reading{
long totalBytesRead += in.read();
print "Process: "+(totalBytesRead * 100 / totalLength);
}
See also : 另请参阅 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.