Netstream仅限音频(SoundMixer.computeSpectrum)

时间:2015-10-26 09:47:13

标签: actionscript-3 audio netstream

Netstream仅限声音?

我试图将来自网络流的声音放入声音变量,以便我可以像this tutorial一样将其可视化。 Adobe ActionScript 3.0 *访问原始声音数据

问题是,搜索结果只能找到如何将视频附加到视频对象,而不是声音对象。

private function handleAccept(e:MouseEvent):void 
    {
    myNS.attachAudio(Microphone.getEnhancedMicrophone(0));
    myNS.publish("audio");

    var s:Sound = new Sound(theirNS.play("audio"));//??
    s.play(); 

    //Custom tranformation ahead..
    }

2 个答案:

答案 0 :(得分:1)

该示例使用SoundMixer.computeSpectrum,因此只要您正在播放的NetStream不是RTMP流,一切都会正常工作。

  

此方法不能用于从RTMP流中提取数据

假设您没有使用RTMP流,以下是使用NetStream vs Sound的示例,我在正在播放的视频上覆盖了waveform

enter image description here

package {
    import flash.display.Graphics;
    import flash.events.Event;
    import flash.media.SoundMixer;
    import flash.display.Sprite;
    import flash.net.NetStream;
    import flash.net.NetConnection;
    import flash.utils.ByteArray;
    import flash.media.Video;

    public class Main extends Sprite {

        const PLOT_HEIGHT:int = 200;
        const CHANNEL_LENGTH:int = 256;
        private var bytes:ByteArray = new ByteArray();

        private var videoURL:String = "YourVideoURL";
        private var connection:NetConnection;
        private var video:Video = new Video();
        private var waveForm:Sprite = new Sprite();

        public function Main() {

            connection = new NetConnection();
            connection.connect(null);
            video.width = parent.stage.stageWidth;
            video.height = parent.stage.stageHeight;
            addChild(video);

            var stream:NetStream = new NetStream(connection);
            stream.client = new CustomClient();
            video.attachNetStream(stream);
            stream.play(videoURL);

            addChild(waveForm);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        function onEnterFrame(event:Event):void {
            SoundMixer.computeSpectrum(bytes, false, 0);

            var g:Graphics = waveForm.graphics;

            g.clear();
            g.lineStyle(0, 0xffeeff);
            g.beginFill(0xeeffee);
            g.moveTo(0, PLOT_HEIGHT);

            var n:Number = 0;

            // left channel
            for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
                n = (bytes.readFloat() * PLOT_HEIGHT);
                g.lineTo(i * 2, PLOT_HEIGHT - n);
            }
            g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
            g.endFill();

            // right channel
            g.lineStyle(0, 0xeeffee);
            g.beginFill(0xffeeff, 0.5);
            g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

            for (i = CHANNEL_LENGTH; i > 0; i--) {
                n = (bytes.readFloat() * PLOT_HEIGHT);
                g.lineTo(i * 2, PLOT_HEIGHT - n);
            }
            g.lineTo(0, PLOT_HEIGHT);
            g.endFill();
        }

        function onPlaybackComplete(event:Event) {
            removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

    }
}

答案 1 :(得分:1)

为了能够将'与RTMP流的音频一起使用,您应首先启用对服务器端音频原始数据的访问,如下所示:

适用于Adobe / Flash Media Server(AMS / FMS):

Application.xml:

SoundMixer.computeSpectrum()

或使用 Main.asc:

<Application> 

    <!-- ... -->

    <Client>
        <Access>
            <AudioSampleAccess enabled="true">/</AudioSampleAccess>
        </Access>
    </Client>

    <!-- ... -->

</Application>

对于Wowza Media Server:

Application.xml:

application.onConnect = function( p_client, p_autoSenseBW )
{
    // ...

    p_client.audioSampleAccess = "/";

    // ...      
}

对于RED5:

red5-web.xml:

<Application>

    <!-- ... -->

    <Client>
        <Access>
            <StreamAudioSampleAccess>*</StreamAudioSampleAccess>
        </Access>
    </Client>

    <!-- ... -->

</Application>

然后您只需像平常一样播放流,并且对于声波数据的图形表示,您可以使用例如SoundMixer.computeSpectrum()的文档页面中提供的示例代码:

<bean id="rtmpSampleAccess" class="org.red5.server.stream.RtmpSampleAccess">
    <property name="audioAllowed" value="true"/>
</bean> 

var server:String = 'rtmp://127.0.0.1/vod', stream:String = 'sample', nc:NetConnection, ns:NetStream, vd:Video, spectrum:Sprite, last_status:String = ''; nc = new NetConnection(); nc.connect(server); nc.addEventListener(NetStatusEvent.NET_STATUS, on_NetStatus); nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, function(e:AsyncErrorEvent): void {}); function on_NetStatus(e:NetStatusEvent):void { var code:String = e.info.code; switch (code) { case 'NetConnection.Connect.Success' : ns = new NetStream(nc); ns.bufferTime = 3; ns.addEventListener(NetStatusEvent.NET_STATUS, on_NetStatus); ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, function(e:AsyncErrorEvent): void {}); vd = new Video(320,180); vd.x = vd.y = 0; vd.attachNetStream(ns); addChild(vd); spectrum = new Sprite(); spectrum.x = spectrum.y = 0; addChild(spectrum); ns.play(stream); break; case 'NetStream.Play.Start' : addEventListener(Event.ENTER_FRAME, on_EnterFrame); break; case 'NetStream.Buffer.Flush': if(last_status == 'NetStream.Play.Stop'){ removeEventListener(Event.ENTER_FRAME, on_EnterFrame); } break; } last_status = code; } function on_EnterFrame(event:Event):void { var bytes:ByteArray = new ByteArray(); const PLOT_HEIGHT:int = stage.stageHeight / 2; const CHANNEL_LENGTH:int = 256; SoundMixer.computeSpectrum(bytes, false, 0); var g:Graphics = spectrum.graphics; g.clear(); g.lineStyle(0, 0xffffff); g.beginFill(0x00ff00); g.moveTo(0, PLOT_HEIGHT); var n:Number = 0; for (var i:int = 0; i < CHANNEL_LENGTH; i++) { n = (bytes.readFloat() * PLOT_HEIGHT); g.lineTo(i * 2, PLOT_HEIGHT - n); } g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT); g.endFill(); g.lineStyle(0, 0xCC0066); g.beginFill(0xCC0066, 0.5); g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT); for (i = CHANNEL_LENGTH; i > 0; i--) { n = (bytes.readFloat() * PLOT_HEIGHT); g.lineTo(i * 2, PLOT_HEIGHT - n); } g.lineTo(0, PLOT_HEIGHT); g.endFill(); } 这是与AMS / FMS一起提供的sample视频文件。您当然可以使用任何类型的支持视频或音频文件(MP4,MP3,......)。

上面的代码为您提供了以下内容:

希望可以提供帮助。