2014-11-02 1 views
0

AAC 스트리밍 라디오 방송국에서 재생되는 오디오 샘플에 액세스하고이를 렌더러로 보내기 전에 신호 처리 기능을 수행하고 싶습니다.AS3 appendBytes를 사용하여 AAC 스트리밍 오디오 용 NetStream 버퍼링을 조절하는 방법

URLStreamProgressEvent.PROGRESS 콜백을 사용하여 바이트 스트림에 액세스 할 수있었습니다. 해당 콜백 내에서 나는 원래 urlStream.readBytes(Bytes) 다음에 netStream.appendBytes(Bytes)을 호출했습니다. 몇 초 동안 오디오가 재생되고 netStatus가 NetStream.Buffer.Empty으로 중지됩니다.

ProgressEvent 처리기에 추적 코드를 넣고 netStream.bufferLength을 표시하고 처음에는 버퍼가 비게 될 때까지이 값이 더 커지는 것을 보았습니다. 버퍼를 조절하는 데 필요한 정보를 검색했지만 찾은 정보가 없습니다.

흥미롭게도, 내가 오히려 ProgressEvent 핸들러를 가능하게하기 위해 필요한 netStream.play(null),보다 netStream.play(url)을 실행할 때, 오디오는 잘 재생하지만 기본 오디오 샘플에 대한 가시성이 없습니다.

그럼 내 질문은 다음과 같습니다. 기본 오디오 샘플에 액세스하고 버퍼가 비어 있지 않게하려면 어떻게해야합니까?

package 
{ 
    import flash.display.Sprite; 

    import flash.net.NetConnection; 
    import flash.net.NetStream; 
    import flash.net.NetStreamAppendBytesAction; 
    import flash.net.NetStreamPlayOptions; 
    import flash.net.NetStreamInfo; 

    import flash.events.NetStatusEvent; 
    import flash.events.AsyncErrorEvent; 
    import flash.events.ProgressEvent; 
    import flash.events.Event; 

    import flash.net.URLRequest; 
    import flash.net.URLStream; 

    import flash.utils.*; 

    public class Main extends Sprite 
    { 
     private var connection:NetConnection = new NetConnection(); 
     private var netStream:NetStream = null; 
     private var urlStream:URLStream = null; 
     private var urlRequest:URLRequest = null; 

     private var inputSamples:ByteArray = new ByteArray(); 
     private var outputSamples:ByteArray = new ByteArray(); 

     private var hTimer:uint = 0; 
     private var nState:int = 1; 

     private var url:String="*** replace this text with your favorite streaming AAC radio feed ***"; 

     // Main Constructor 
     public function Main() : void 
     { 
      connectToStream(); 
     } 

     private function connectToStream() : void 
     { 
      connection.close(); 
      connection = new NetConnection(); 

      connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
      connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 
      connection.connect(null);   
     } 

     private function netStatusHandler(event:NetStatusEvent) : void 
     { 
      trace("NetStatusEvent = " + event.info.code); 
      switch (event.info.code) 
      { 
       case "NetConnection.Connect.Success": 
        requestAudio(); 
        break; 

       case "NetStream.Buffer.Empty": 

        //hTimer = setInterval(onTimerEvent, 1000); 

        break; 

      } // End switch 
     } 

     private function asyncErrorHandler(event:AsyncErrorEvent) : void 
     { 
      trace("Stream Error Handler - error = " + event); 
     } 

     public function onMetaData(info:Object) : void 
     { 
      trace("metadata: duration=" + info.duration + " framerate=" + info.framerate); 
     } 

     private function onTimerEvent() : void 
     { 

     } 

     private function requestAudio() : void 
     { 
      if (netStream !== null) 
      { 
       netStream.close(); 
      } 

      netStream = new NetStream(this.connection); 
      netStream.client = new Object(); 

      netStream.client.onMetaData = onMetaData; 

      netStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
      netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 
      netStream.checkPolicyFile = false; 

      //netStream.play(url); 

      if (urlStream !== null) 
      { 
       urlStream.close(); 
      } 

      urlStream = new URLStream(); 
      urlStream.addEventListener(ProgressEvent.PROGRESS, progressHandler); 

      urlRequest = new URLRequest(url); 

      urlStream.load(urlRequest); 

      netStream.play(null); 

      netStream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); 

     } 

     // Process audio samples from file 
     private function progressHandler(event:ProgressEvent) : void 
     { 
      var streamByte:int; 

      // Position to beginning of buffer 
      inputSamples.position = 0; 

      urlStream.readBytes(inputSamples); 

      // *** want to do some signal processing here *** 
      // For now just copy the input to the output 

      // Position to beginning of buffer 
      outputSamples.position = 0; 

      // This output indicates that the buffer is being drained 
      // until I get NetStream.Buffer.Empty and the audio stops playing 
      trace("Buffer Length = " + netStream.bufferLength); 

      while (inputSamples.bytesAvailable) 
      { 
       streamByte = inputSamples.readByte(); 

       outputSamples.writeByte(streamByte); 
      } 

      // Position to beginning of buffer 
      outputSamples.position = 0;    

      netStream.appendBytes(outputSamples);      
     } 
    } 
} 
+0

내가 코드를 테스트하지만 난'NetStream.Buffer.Empty' 이벤트를받지 못했습니다. 당연히 테스트 할 수 있다면 사용중인 피드의 URL을 나에게 줄 수 있습니까? – akmozo

+0

실제로 고객이 신호 처리를 수행해야하는 스트리밍 URL을 사용하지 말 것을 요청했습니다. 그러나, 나는 당신이 친절하게 그것을 게시 할 수 있다면 당신이 성공했다 URL을 시도에 관심이있을 것입니다. 또한 ProgressEvent 핸들러에서 수신되는 바이트가 Sound Extract 메소드를 사용할 때처럼 32 비트 부동 소수점 오디오 샘플을 나타내는지 여부를 알고 있습니까? Number 데이터 유형을 사용하여 샘플을 읽고 쓰려고 할 때 더 이상 오디오 출력을 듣지 못했습니다! – BooRowan

+0

테스트 할 수있는 링크가 있습니다. – BooRowan

답변

0

이 시도 :

import flash.media.Video 

var stream = 'http://2583.live.streamtheworld.com/KFMXFMAAC' 

var http_stream:URLStream = null 

var video:Video = new Video() // the video is used only to ensure that stream is playing 
addChild(video) 

var nc:NetConnection = new NetConnection() 
    nc.connect(null) 
var ns:NetStream = new NetStream(nc) 
    ns.client = new Object() 
    ns.client.onMetaData = function(e){} 
    ns.play(null) 

video.attachNetStream(ns) 

http_stream = new URLStream(); 
http_stream.addEventListener(ProgressEvent.PROGRESS, on_Progress) 
http_stream.load(new URLRequest(stream)) 

function on_Progress(e:ProgressEvent):void { 

    var b:ByteArray = new ByteArray() 
    http_stream.readBytes(b, 0, http_stream.bytesAvailable) 
    ns.appendBytes(b) 

    trace(ns.bufferLength) 

} 
+0

Video 클래스를 사용하지 않고 스트림을 사용할 수 있습니까? –

+0

@TarmoSaluste 물론 '예'테스트를위한 '비디오'개체를 제거 할 수 있습니다. – akmozo

관련 문제