的Adobe录制声音,并保存 [英] Adobe record sound and save

查看:129
本文介绍了的Adobe录制声音,并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下codeI想保存话筒内容到锉的保存的文件无法播放,文件保存每次我看到大小只有10字节only.What是我做错了在code.Can有人请告诉我正确的code保存它。而保存的文件应该相应地播放录制的内容。

 < XML版本=1.0编码=UTF-8&GT?;
< S:应用的xmlns:FX =htt​​p://ns.adobe.com/mxml/2009
           XMLNS:S =库://ns.adobe.com/flex/spark
           的xmlns:MX =库://ns.adobe.com/flex/mx=了minWidth955了minHeight =600>
< FX:脚本>
    <![CDATA [
        进口flash.events.SampleDataEvent;
        进口flash.media.Microphone;
        导入器flash.net.FileReference;
        进口mx.controls.Alert;
        导入器flash.net.FileReference;
        进口flash.display.Sprite;
        进口flash.media.Sound;
        进口flash.utils.ByteArray的;
        进口的flash.external.ExternalInterface;




        公共变种_file:的FileReference =新的FileReference();
        [可绑定]私人变种micList:阵列;
        公共变种MIC:麦克风= Microphone.getMicrophone();
        保护VAR isRecording:布尔= FALSE;

        保护功能startMicRecording():无效
        {
            // VAR MIC:麦克风= Microphone.getMicrophone();
            mic.gain = 60;
            mic.rate = 11;
            mic.setUseEchoSup pression(真正的);
            mic.setLoopBack(真正的);
            mic.setSilenceLevel(5,1000);
            Alert.show(在录制);
            isRecording = TRUE;
            mic.addEventListener(的SampleDataEvent.SAMPLE_DATA,gotMicData);

        }

        保护功能stopMicRecording():无效
        {


            // isRecording = FALSE;

            尝试{
            // _ file.save(的SampleDataEvent.SAMPLE_DATArecorded.wav);
                _file.save(的SampleDataEvent.SAMPLE_DATArecorded.flv);
            }
            赶上(五:错误)
            {
                Alert.show(在Stopmicrecording+ E);
            }

        }

        私有函数gotMicData(micData:的SampleDataEvent):无效
        {

            //mic.removeEventListener(SampleDataEvent.SAMPLE_DATA,gotMicData);

        }


        保护VAR soundRecording:ByteArray的;
        保护VAR soundOutput:声音;
        保护功能playbackData():无效
        {



        }

        私有函数playSound(soundOutput:的SampleDataEvent):无效
        {

        }







    ]]≥
< / FX:脚本>
< FX:声明>
    <! - 将非视觉元素(例如,服务,价值的物品)在这里 - >
< / FX:声明>
< MX:组合框X =150ID =comboMicList的dataProvider ={micList}/>
< MX:按钮X =250ID =startmicrec标签=开始录制点击=startMicRecording()/>
< MX:按钮X =350ID =stopmicrec标签=停止录制点击=stopMicRecording()/>
!<  - < MX:按钮X =50ID =setupmic标签=选择麦克风点击=setupMicrophone()/>  - >
< MX:按钮X =450ID =playrecsound标签=播放声音点击=playbackData()/>

 < / S:用途>
 

解决方案

您需要存储的交给你的 gotMicData 的ByteArray ,然后保存的ByteArray 。要保存的事件名称,它是一个字符串(10个字符)。

一旦你这样做,你需要将文件和手工采样数据加载声音。你玩的声音后面的8倍......因为你在11千赫采样,但在播放声音在44千赫(4x写入)和声音是立体声,但麦克风是单声道(2X再次)。

您无法将数据保存为WAV文件直接...你记录的原始数据。如果你想通过写一个适当的WAV头的麻烦,那么你就不必打的移交样本数据的游戏和手中的文件,在声音对象。这是行使这个问题的范围之外。

祝你好运!

 进口mx.controls.Alert;

        公共变种MIC:麦克风= Microphone.getMicrophone();
        公共变种recordedData:ByteArray的;

        保护功能startMicRecording():无效
        {
            mic.gain = 60;
            mic.rate = 11;
            mic.setUseEchoSup pression(真正的);
            mic.setLoopBack(假);
            mic.setSilenceLevel(5,1000);

            recordedData =新的ByteArray();
            mic.addEventListener(的SampleDataEvent.SAMPLE_DATA,gotMicData);

        }

        保护功能stopMicRecording():无效
        {
            mic.removeEventListener(的SampleDataEvent.SAMPLE_DATA,gotMicData);

            尝试{
                var文件:的FileReference =新的FileReference();
                file.save(recordedDatarecorded.dat);
            }
            赶上(五:错误)
            {
                Alert.show(在Stopmicrecording+ E);
            }

        }

        私有函数gotMicData(示例:的SampleDataEvent):无效
        {
            recordedData.writeBytes(sample.data,0,sample.data.bytesAvailable);
        }


        保护VAR playbackFile:的FileReference;
        保护VAR soundRecording:ByteArray的;
        保护VAR soundOutput:声音;
        保护功能playbackData():无效
        {
            playbackFile =新的FileReference();
            playbackFile.addEventListener(Event.SELECT,playbackFileSelected);
            playbackFile.browse();
        }

        私有函数playbackFileSelected(事件:事件):无效{
            playbackFile.addEventListener(引发Event.COMPLETE,playbackFileLoaded);
            playbackFile.load();
        }

        私有函数playbackFileLoaded(事件:事件):无效{
            soundRecording = playbackFile.data;
            soundOutput =新的声音();
            soundOutput.addEventListener(的SampleDataEvent.SAMPLE_DATA,moreInput);
            soundOutput.play();
        }

        私有函数moreInput(事件:的SampleDataEvent):无效{
            VAR示例:号码;
            对于(VAR我:= 0; I< 1024;我++){
                如果(soundRecording.bytesAvailable大于0){
                    样品= soundRecording.readFloat();

                    //写相同的字节8次:
                    //升频为11千赫到44千赫(X4)
                    //单声道到立体声(2次)
                    对于(VAR X:INT = 0,X< 8,X ++)
                        event.data.writeFloat(样品);
                }
            }
        }
 

In the following code i am trying to save the microphone contents to a file.The saved file doesn't play and Every time the file is saved i see that the size is only of 10 bytes only.What am i doing wrong in the code.Can someone please show me the correct code to save it .And the saved file should play the recorded contents accordingly.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
    <![CDATA[
        import flash.events.SampleDataEvent; 
        import flash.media.Microphone;
        import flash.net.FileReference;
        import mx.controls.Alert;
        import flash.net.FileReference;
        import flash.display.Sprite;
        import flash.media.Sound;
        import flash.utils.ByteArray;
        import flash.external.ExternalInterface;




        public var _file:FileReference = new FileReference();
        [Bindable] private var micList:Array;
        public var mic:Microphone = Microphone.getMicrophone();
        protected var isRecording:Boolean = false;

        protected function startMicRecording():void 
        { 
            //var mic:Microphone = Microphone.getMicrophone();
            mic.gain = 60;
            mic.rate = 11;
            mic.setUseEchoSuppression(true);
            mic.setLoopBack(true);
            mic.setSilenceLevel(5, 1000);
            Alert.show("In recording");
            isRecording = true;
            mic.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);

        }

        protected function stopMicRecording():void 
        { 


            //isRecording = false;

            try{
            //_file.save( SampleDataEvent.SAMPLE_DATA, "recorded.wav" );
                _file.save(SampleDataEvent.SAMPLE_DATA , "recorded.flv" );
            }
            catch(e:Error)
            {
                Alert.show("In Stopmicrecording"+e);
            }

        }

        private function gotMicData(micData:SampleDataEvent):void 
        { 

            //mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);

        }


        protected var soundRecording:ByteArray;
        protected var soundOutput:Sound;
        protected function playbackData():void 
        { 



        } 

        private function playSound(soundOutput:SampleDataEvent):void
        {

        }







    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:ComboBox x="150" id="comboMicList" dataProvider="{micList}" />
<mx:Button x="250" id="startmicrec" label="Start Rec" click="startMicRecording()"/>
<mx:Button x="350" id="stopmicrec" label="Stop Rec" click="stopMicRecording()"/> 
<!--<mx:Button x="50" id="setupmic" label="Select Mic" click="setupMicrophone()"/>-->
<mx:Button x="450" id="playrecsound" label="Play sound" click="playbackData()"/>

 </s:Application>

解决方案

You need to store the data that is handed to you in gotMicData into a ByteArray and then save that ByteArray. You are saving the event name, which is a string (10 characters long).

Once you do that, you need to load the file and hand sample data to the sound. You play the sound back 8 times... because you sampled at 11 KHz but the sound plays back at 44 KHz (4x writing) and the sound is Stereo but the mic is mono (2x again).

You can't save the data as a WAV file directly... you recorded raw data. If you want to go through the trouble of writing a proper WAV header, then you don't have to play the games of handing sample data and hand the file to the Sound object. That is an exercise outside of the scope of this question.

Good luck!

        import mx.controls.Alert;

        public var mic:Microphone = Microphone.getMicrophone();
        public var recordedData:ByteArray;

        protected function startMicRecording():void 
        { 
            mic.gain = 60;
            mic.rate = 11;
            mic.setUseEchoSuppression(true);
            mic.setLoopBack(false);
            mic.setSilenceLevel(5, 1000);

            recordedData = new ByteArray();
            mic.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);

        }

        protected function stopMicRecording():void 
        { 
            mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);

            try{
                var file:FileReference = new FileReference();
                file.save(recordedData, "recorded.dat" );
            }
            catch(e:Error)
            {
                Alert.show("In Stopmicrecording"+e);
            }

        }

        private function gotMicData(sample:SampleDataEvent):void 
        { 
            recordedData.writeBytes(sample.data, 0, sample.data.bytesAvailable);
        }


        protected var playbackFile:FileReference;
        protected var soundRecording:ByteArray;
        protected var soundOutput:Sound;
        protected function playbackData():void 
        { 
            playbackFile = new FileReference();
            playbackFile.addEventListener(Event.SELECT, playbackFileSelected);
            playbackFile.browse();
        } 

        private function playbackFileSelected(event:Event):void {
            playbackFile.addEventListener(Event.COMPLETE, playbackFileLoaded);
            playbackFile.load();
        }

        private function playbackFileLoaded(event:Event):void {
            soundRecording = playbackFile.data;
            soundOutput = new Sound();
            soundOutput.addEventListener(SampleDataEvent.SAMPLE_DATA, moreInput);
            soundOutput.play();
        }

        private function moreInput(event:SampleDataEvent):void {
            var sample:Number;
            for (var i:int = 0; i < 1024; i++) {
                if (soundRecording.bytesAvailable > 0) {
                    sample = soundRecording.readFloat();

                    // write the same byte 8 times:
                    //   Upsample from 11 KHz to 44 KHz (x4)
                    //   Mono to Stereo (x2)
                    for(var x:int = 0; x < 8; x++)
                        event.data.writeFloat(sample);
                }
            }
        }

这篇关于的Adobe录制声音,并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆