gpt4 book ai didi

java - 无法播放使用 XUGGLER 编码的音频

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:41 28 4
gpt4 key购买 nike

我正在使用此代码将音频发布到 red5 服务器,并通过发布者演示对其进行测试,看看我是否能够听到我正在发布的内容。数据包已成功发送,但无法听到音频。请帮助我,我无法找出错误。

package testx;


import com.xuggle.ferry.IBuffer;
import com.xuggle.xuggler.*;
import com.xuggle.xuggler.IAudioSamples.Format;

import javax.sound.sampled.*;
public class Testx{

private static IContainer outContainer;
private static IContainerFormat outContainerFormat;
private static IStreamCoder outAudioCoder;
private static IStream outAudioStream;


public static void main(String args[])
{
String urlOut ="rtmp://localhost:1935/oflaDemo/xxx";
outContainer = IContainer.make();
outContainerFormat = IContainerFormat.make();
outContainerFormat.setOutputFormat("flv", urlOut, null);
int retVal = outContainer.open(urlOut, IContainer.Type.WRITE, outContainerFormat);
if (retVal < 0) {
System.out.println("Could not open output container");
return;
}



outAudioStream = outContainer.addNewStream(1);


outAudioCoder = outAudioStream.getStreamCoder();
ICodec cdc = ICodec.findEncodingCodec(ICodec.ID.CODEC_ID_AAC);
outAudioCoder.setCodec(cdc);
outAudioCoder.setSampleRate(44100);
outAudioCoder.setChannels(1);

outAudioCoder.setFrameRate(IRational.make(44100,1));
outAudioCoder.setSampleFormat(Format.FMT_S16);
outAudioCoder.setBitRate(64000);

retVal = outAudioCoder.open(null,null);
if (retVal < 0) {
System.out.println("Could not open audio coder");
return;
}

retVal = outContainer.writeHeader();
if (retVal < 0) {
System.out.println("Could not write output FLV header: ");
return ;
}






AudioFormat audioFormat = new AudioFormat(44100,

(int)16,

1,

true, /* xuggler defaults to signed 16 bit samples */

false);



TargetDataLine line = null;

DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);

if (!AudioSystem.isLineSupported(info)) {

return;



}

// Obtain and open the line.

try {

line = (TargetDataLine) AudioSystem.getLine(info);

line.open(audioFormat);

} catch (LineUnavailableException ex) {

return;

}



// Begin audio capture.

line.start();


long lastPos_out=0;

long audtioTime =0;
long startTime = System.nanoTime();
while(1)
{
byte[] data = new byte[line.getBufferSize()/5];
int sz = line.read(data, 0, data.length);

if(sz>0)
{

long nanoTs = System.nanoTime()-startTime;
IBuffer iBuf = IBuffer.make(null,data,0,sz);
System.out.println(iBuf.toString());
IAudioSamples smp = IAudioSamples.make(iBuf, 1,IAudioSamples.Format.FMT_S16);
smp.setComplete(true,sz/2,44100,1,Format.FMT_S16, nanoTs / 1000);


int samplesConsumed = 0;
while(samplesConsumed<smp.getNumSamples()){
IPacket packet= IPacket.make();
samplesConsumed +=outAudioCoder.encodeAudio(packet, smp, samplesConsumed);

if (packet.isComplete()) {

// packet.setKeyPacket(true);
packet.setPosition(lastPos_out);
packet.setStreamIndex(1);
lastPos_out+=packet.getSize();
outContainer.writePacket(packet);
}
}
}
// try {
// Thread.sleep((long)10000);
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }



}
}

}

最佳答案

解决了,我的麦克风音量很低,将速率设置为16khz

关于java - 无法播放使用 XUGGLER 编码的音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20097078/

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