gpt4 book ai didi

java - 在 http 协议(protocol)上使用 Java Media Framework 传输音频

转载 作者:行者123 更新时间:2023-12-01 11:32:07 25 4
gpt4 key购买 nike

我正在尝试通过 http 传输可以由 VLC winamp 或 WMP 打开的音频。我目前有通过 rtp 协议(protocol)而不是 http 执行此操作的代码。仅仅将下面代码中的 url 更改为 http 是行不通的。如何修改下面的代码以通过 http 进行流传输?

public class MediaTransmitter {

private MediaLocator mediaLocator = null;
private DataSink dataSink = null;
private Processor mediaProcessor = null;
private static final Format[] FORMATS = new Format[]{new AudioFormat(AudioFormat.MPEG_RTP)};
private static final ContentDescriptor CONTENT_DESCRIPTOR = new ContentDescriptor(ContentDescriptor.RAW_RTP);

public MediaTransmitter(MediaLocator locator) {
mediaLocator = locator;
}

public void startTransmitting() throws IOException {
mediaProcessor.start();
dataSink.open();
dataSink.start();
}

public void stopTransmitting() throws IOException {
dataSink.stop();
dataSink.close();
mediaProcessor.stop();
mediaProcessor.close();
}

public void setDataSource(DataSource ds) throws IOException, NoProcessorException, CannotRealizeException, NoDataSinkException {
mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(ds, FORMATS, CONTENT_DESCRIPTOR));
dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(), mediaLocator);
}



public static void main(String[] args) {

try {
MediaLocator locator = new MediaLocator("rtp://MY_PUBLIC_IP_HERE:10000/audio");
MediaTransmitter transmitter = new MediaTransmitter(locator);
System.out.println("-> Created media locator: '" + locator + "'");

File mediaFile = new File("D:\\data\\audio\\posters.mp3");
DataSource source = Manager.createDataSource(new MediaLocator(mediaFile.toURL()));
System.out.println("-> Created data source: '" + mediaFile.getAbsolutePath() + "'");

// set the data source.
transmitter.setDataSource(source);
System.out.println("-> Set the data source on the transmitter");

// start transmitting the file over the network.
transmitter.startTransmitting();
System.out.println("-> Transmitting...");
System.out.println(" Press the Enter key to exit");

// wait for the user to press Enter to proceed and exit.
System.in.read();
System.out.println("-> Exiting");
transmitter.stopTransmitting();
} catch (Throwable t) {
t.printStackTrace();
}

System.exit(0);
}
}

最佳答案

事实证明 RTP 是一种推送协议(protocol),因此使用相同的 url 在 VLC 中打开流失败。但是,如果我在 VLC 中将 rtp 更改为 http 并指定 url 直到端口(减去 url 的其余部分),它就可以工作。

关于java - 在 http 协议(protocol)上使用 Java Media Framework 传输音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30310954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com