2013-05-29 47 views
0

我正在嘗試使用xuggler來實時播放麥克風流(SPEEX編解碼器),但我收到警告說編解碼器爲空。什麼解決這個問題?如何使用xuggler通過RTMP捕捉實時音頻?

public void audioCapture(String streamName, String sessionId) { 
     IContainer readContainer = IContainer.make(); 
     readContainer.setInputBufferLength(4096); 
     String url = "rtmp://127.0.0.1:1935/live/Session_" 
       + sessionId + "/" + streamName + " live=1"; 
     if (readContainer.open(url, IContainer.Type.READ, null, true, false) < 0) { 
      // if(readContainer.open(url, IContainer.Type.READ, null) < 0){ 
      throw new RuntimeException("unable to open read container"); 
     } 
     int numStreamAudio = readContainer.getNumStreams(); 
     System.out.println("Numer of audio stream: " + numStreamAudio); 
     IStream stream = readContainer.getStream(0); 
     audioCoder = readContainer.getStream(0).getStreamCoder(); 
     int st = audioCoder.open(null, null); 
     // if(st<0) throw new RuntimeException("cant open audio coder"); 
     // if(audioCoder.open() <0) throw new 
     // RuntimeException("cant open audio coder"); 
     System.out.println(" audio coder prop channels" 
       + audioCoder.getChannels() + " sample rate " 
       + audioCoder.getSampleRate() + " bit rate " 
       + audioCoder.getBitRate() + " codec type " 
       + audioCoder.getCodecType().toString() + " codec tag " 
       + audioCoder.getCodecTag() + " audio frame size " 
       + audioCoder.getAudioFrameSize() + " codec id " 
       + audioCoder.getCodecID().toString() + " num properties " 
       + audioCoder.getNumProperties()); 

     writer.addAudioStream(1, 1, 1, 16000); 
     IPacket packet = IPacket.make(); 
     while (readContainer.readNextPacket(packet) >= 0) { 
      IAudioSamples samples = IAudioSamples.make(1024, 1); 
      int offset = 0; 
      while (offset < packet.getSize()) { 
       int bytesDecoded = audioCoder.decodeAudio(samples, packet, 
         offset); 

       // if(bytesDecoded < 0) throw new 
       // RuntimeException("got error decoding audio in: "); 
       offset += bytesDecoded; 
      } 
     } 

     // IAudioSamples samples = IAudioSamples.make(numSamples, numChannels) 
    } 

Errror:

20:24:25,569 INFO [stdout] (http-0.0.0.0-0.0.0.0-8081-2) 20:24:25.568 [http-0.0.0.0-0.0.0.0-8081-2] WARN com.xuggle.xuggler - Attempting to decode when not ready; codec not opened (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:873) 

20:25:41,517 INFO [stdout] (http-0.0.0.0-0.0.0.0-8081-2) 20:25:41.517 [http-0.0.0.0-0.0.0.0-8081-2] WARN com.xuggle.xuggler - Attempting to decode when not ready; codec not opened (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:873) 

20:25:42,621 INFO [stdout] (http-0.0.0.0-0.0.0.0-8081-2) 20:25:42.621 [http-0.0.0.0-0.0.0.0-8081-2] WARN com.xuggle.xuggler - Attempting to decode when not ready; codec not opened (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:873) 

20:25:43,485 INFO [stdout] (http-0.0.0.0-0.0.0.0-8081-2) 20:25:43.485 [http-0.0.0.0-0.0.0.0-8081-2] WARN com.xuggle.xuggler - Attempting to decode when not ready; codec not opened (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:873) 

回答

0

有u盤audioCoder爲IStreamCoder? 和一個IStreamCoder open()不需要參數,jus給audioCoder.open(),並且只有當audioCoder是一個IStreamCoder對象時。