无法播放用XUGGLER编码的音频

时间:2013-11-20 13:17:53

标签: java xuggler

我正在使用此代码将音频发布到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();
    //  }



        }
    }

}

1 个答案:

答案 0 :(得分:0)

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