简体   繁体   English

如何部分流式传输mp3文件

[英]How to partially stream an mp3 file

I'm trying to partially stream an mp3 file, but all the bytes before the requested "byte-mark" is stil being downloaded: 我正在尝试部分流mp3文件,但是请求的“字节标记”之前的所有字节都被下载:

  • Let's assume that the mp3 file is 7000000 bytes in filesize. 假设mp3文件的文件大小为7000000字节。
  • I "jump" to 6000000 bytes and begin streaming from there to the end. 我“跳”到6000000字节,然后从那里开始流式传输到结尾。
  • But I notice that every byte from 1-5999999 is being downloaded before the mp3 file is being played from the 6000000 byte mark. 但是我注意到,从6000000字节标记开始播放mp3文件之前,将下载1-5999999中的每个字节。
  • I am using JLayer (Java Zoom - http://www.javazoom.net/javalayer/javalayer.html ) to play the mp3 file. 我正在使用JLayer(Java Zoom- http://www.javazoom.net/javalayer/javalayer.html )播放mp3文件。

     import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.advanced.AdvancedPlayer; try { URL url = new URL("http://somerandomsite.com/audiotestfile.mp3"); URLConnection connection = url.openConnection(); connection.connect(); int fileSize = connection.getContentLength(); InputStream inputStream = url.openStream(); System.out.println("Filesize in bytes: " + fileSize); // Lets assume the filesize of the mp3 file is 7000000 bytes long skippedBytes = inputStream.skip(6000000); // Skip to 6000000 bytes to only stream the file partially System.out.println("Skipped bytes: " + skippedBytes); // The skipped bytes are equal to 6000000 bytes, but all previous bytes are still being downloaded. AdvancedPlayer ap = new AdvancedPlayer(inputStream); ap.play(); } catch (FileNotFoundException | JavaLayerException e) { System.out.println(e.getMessage()); } 

How do I stream partially? 我如何部分流媒体?

I think what you want to do is to have the server only send you from byte 6000000 onwards, but your code above is actually downloading the entire stream and simply ignoring or 'skipping' the first 6000000 bytes, as you have observed. 我认为您想要做的是让服务器仅从6000000字节开始向您发送消息,但是如您所见,上面的代码实际上是下载整个流,并且只是忽略或“跳过”了前6000000字节。

This is because the InputStream.skip essentially reads in and ignores the bytes you tell it to skip. 这是因为InputStream.skip本质上会读入并忽略您告诉它跳过的字节。 From the documentation for InputStream skip: 从InputStream的文档中跳过:

The skip method of this class creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached. 此类的skip方法创建一个字节数组,然后重复读取该字节数组,直到已读取n个字节或到达流的末尾为止。

What I think you actually want to do is to request from the server that it starts streaming to you from the 6000001'th byte in the mp3 file. 我认为您真正想做的是从服务器请求它从mp3文件中的第6000001个字节开始向您流式传输。 One way to do this is to use a concept called 'Byte Serving': 一种实现方法是使用称为“字节服务”的概念:

  • Your server indicates that it can accept request for a portion of a file by using the Accepts-Ranges response header 您的服务器表明它可以通过使用Accepts-Ranges响应标头接受文件部分的请求
  • The client then sends a request to the server for a particular portion of a file using the Range request header 然后,客户端使用Range请求标头向服务器发送文件特定部分的请求
  • The server responds with the requested portion of the file 服务器以文件的请求部分响应

There is an example of the message flow back and forth to there server in this answer - in this case it is for an mp4 file but the approach is exactly the same: https://stackoverflow.com/a/8507991/334402 在此答案中,有一个来回往返于服务器的消息流的示例-在这种情况下,它是用于mp4文件的,但是方法是完全相同的: https : //stackoverflow.com/a/8507991/334402

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

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