使用 OkHttp 消费音频流

Consuming the audio stream with OkHttp

我想使用 Java - https://playoutonestreaming.com/proxy/bylr?mp=/stream.

使用以下音频流

主要目标非常简单 - 我想创建一个服务,这将允许我目前拥有 300-400 个活动连接(例如活动侦听器)。当我尝试与 OkHttp 建立连接时出现问题。

这是我的代码:

  1. 消费流的任务
@Slf4j
@RequiredArgsConstructor
class ListenStream implements Runnable {
    private static final String STREAM_URL = "https://playoutonestreaming.com/proxy/bylr?mp=/stream";
    private final OkHttpClient client;
    private final int id;

    @Override
    public void run() {
        Clip clip;
        try {
            log.info("[TASK-{}] - Connecting to the stream...", id);
            Request request = new Request.Builder()
                    .url(STREAM_URL)
                    .get()
                    .build();
            Response response = client.newCall(request).execute();
            assert response.body() != null;
            InputStream is = new ByteArrayInputStream(response.body().bytes());
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
            clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (IOException e) {
            log.info("Finished listening to the stream");
        } catch (LineUnavailableException | UnsupportedAudioFileException e) {
            log.error("Failed to process audio stream");
            e.printStackTrace();
        }
    }
}
  1. 执行者
public class RadioMain {

    public static void main(String[] args) throws InterruptedException {
        OkHttpClient client = new OkHttpClient.Builder()
                .callTimeout(120, TimeUnit.SECONDS)
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .writeTimeout(120, TimeUnit.SECONDS)
                .followRedirects(true)
                .followSslRedirects(true)
                .build();

        ExecutorService service = Executors.newFixedThreadPool(300);
        List<Runnable> tasks = IntStream.range(1, 301)
                .mapToObj(number -> new ListenStream(client, number))
                .collect(Collectors.toList());
        tasks.forEach(service::submit);
        service.awaitTermination(120, TimeUnit.SECONDS);
        service.shutdown();
    }
}

使用下面的方法,我必须为OkHttpClient设置超时,然后使用它发出请求。但是,一旦出现超时,我就会收到 InterruptedIOException: socket closed,我需要创建一个新任务。 是否有使用 OkHttp 消费音频流的正确方法?

调用超时将使请求在该时间长度后失败,因此请删除该设置或将其定义为 0。

https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/call-timeout/

Sets the default timeout for complete calls. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

The call timeout spans the entire call: resolving DNS, connecting, writing the request body, server processing, and reading the response body. If the call requires redirects or retries all must complete within one timeout period.

The default value is 0 which imposes no timeout.