Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3声音字节数组的播放不';不要从头开始_Actionscript 3_Audio_Bytearray - Fatal编程技术网

Actionscript 3 AS3声音字节数组的播放不';不要从头开始

Actionscript 3 AS3声音字节数组的播放不';不要从头开始,actionscript-3,audio,bytearray,Actionscript 3,Audio,Bytearray,我目前正在录制和存储一组声音,然后再播放。但由于某些原因,ByteArray的播放起始位置是163840,而不是我需要的0 有人知道为什么会这样吗 谢谢 标记 //假设我已经成功地将声音录制并存储到recordingsArray中 soundBA.clear(); soundBA.length = 0; //I collect the recorded byteArray within an array soundBA.writeBytes(recordingsArray[0]); soun

我目前正在录制和存储一组声音,然后再播放。但由于某些原因,ByteArray的播放起始位置是163840,而不是我需要的0

有人知道为什么会这样吗

谢谢

标记

//假设我已经成功地将声音录制并存储到recordingsArray中

soundBA.clear();
soundBA.length = 0;

//I collect the recorded byteArray within an array
soundBA.writeBytes(recordingsArray[0]);

soundBA.position = 0;
trace("Start POS "+soundBA.position); //traces 0

sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sound_sampleDataHandler, false, 0, true);
ch=sound.play();
this.addEventListener(Event.ENTER_FRAME, updateSeek, false, 0, true);



public function updateSeek(event:Event):void {

    trace("current Pos "+soundBA.position); //the first trace event is "current Pos 163840"
}



function sound_sampleDataHandler(event:SampleDataEvent):void {

    for (var i:int = 0; i < 8192; i++)
    {
        if (soundBA.bytesAvailable < 4)
        {
            break;
        }
        var sample:Number = soundBA.readFloat();
        event.data.writeFloat(sample);
        event.data.writeFloat(sample);

    }

}
soundBA.clear();
soundBA.length=0;
//我在一个数组中收集录制的byteArray
soundBA.writeBytes(recordingsArray[0]);
soundBA.position=0;
跟踪(“开始位置”+声霸位置)//跟踪0
sound.addEventListener(SampleDataEvent.SAMPLE_数据,sound_sampleDataHandler,false,0,true);
ch=声音。播放();
this.addEventListener(Event.ENTER_FRAME,updateSeek,false,0,true);
公共函数更新Seek(事件:事件):无效{
跟踪(“当前位置”+soundBA.position);//第一个跟踪事件是“当前位置163840”
}
函数sound\u sampleDataHandler(事件:SampleDataEvent):void{
对于(变量i:int=0;i<8192;i++)
{
如果(声音ba.bytes小于4)
{
打破
}
var-sample:Number=soundBA.readFloat();
event.data.writeFloat(示例);
event.data.writeFloat(示例);
}
}

这是因为
soundBA.position
是字节数组中的位置,而不是播放的位置。由于声音滞后,它在播放位置之前运行。要确定当前播放位置,请使用SoundChannel.position:

public function updateSeek(event:Event):void {
    trace("current pos in ms: " + ch.position);
    trace("current pos in bytes: " + (ch.position * 44.1 * 4 * 2));
    trace("current pos in %: " + (100 * ch.position / sound.length));
}
UPD:我指的是使用附加的
声音
对象解码声音的情况,例如:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;

    public class SoundTest extends Sprite
    {
        private var soundSrc:Sound;
        private var soundPlayer:Sound;
        private var soundData:ByteArray;
        private var soundChannel:SoundChannel;

        public function SoundTest()
        {
            soundSrc = new Sound();
            soundSrc.addEventListener(Event.COMPLETE, startPlayback);
            soundSrc.load(new URLRequest("sound.mp3"));
        }

        private function startPlayback(e:Event = null):void
        {
            soundData = new ByteArray();
            soundSrc.extract(soundData, soundSrc.length * 44.1, 0);
            soundData.position = 0;

            soundPlayer = new Sound();
            soundPlayer.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
            soundChannel = soundPlayer.play();
            addEventListener(Event.ENTER_FRAME, updateTime);
        }

        private function onSampleData(e:SampleDataEvent):void
        {
            for (var i:int = 0; i < 8192; i++)
            {
                if (soundData.bytesAvailable < 4)
                {
                    break;
                }
                var sampleL:Number = soundData.readFloat();
                var sampleR:Number = soundData.readFloat();
                e.data.writeFloat(sampleL);
                e.data.writeFloat(sampleR);

            }
        }

        private function updateTime(e:Event):void
        {
            trace("current pos in ms: " + soundChannel.position);
            trace("current pos in bytes: " + (soundChannel.position * 44.1 * 4 * 2));
            trace("current pos in % (method 1): " + (100 * soundChannel.position / soundSrc.length));
            // it also works
            trace("current pos in % (method 2): " + (100 * soundChannel.position / (soundData.length / (44.1 * 4 * 2))));
        }
    }
}
包
{
导入flash.display.Sprite;
导入flash.events.Event;
导入flash.events.SampleDataEvent;
导入flash.media.Sound;
导入flash.media.SoundChannel;
导入flash.net.URLRequest;
导入flash.utils.ByteArray;
公共类SoundTest扩展了Sprite
{
私有var soundSrc:声音;
私人var声音播放器:声音;
私有数据:ByteArray;
专用语音频道:语音频道;
公共功能声音测试()
{
soundSrc=新声音();
soundSrc.addEventListener(Event.COMPLETE,startPlayback);
load(新的URL请求(“sound.mp3”);
}
私有函数startPlayback(e:Event=null):void
{
soundData=新的ByteArray();
soundSrc.extract(soundData,soundSrc.length*44.1,0);
soundData.position=0;
soundPlayer=新声音();
soundPlayer.addEventListener(SampleDataEvent.SAMPLE_数据,onSampleData);
soundChannel=soundPlayer.play();
addEventListener(Event.ENTER_FRAME,updateTime);
}
SampleData上的私有函数(e:SampleDataEvent):void
{
对于(变量i:int=0;i<8192;i++)
{
如果(soundData.bytesAvailable<4)
{
打破
}
var sampleL:Number=soundData.readFloat();
var采样器:Number=soundData.readFloat();
e、 data.writeFloat(sampleL);
e、 数据写入流(采样器);
}
}
私有函数更新时间(e:事件):void
{
跟踪(“当前位置(毫秒):+soundChannel.position);
跟踪(“当前位置(字节):+(soundChannel.position*44.1*4*2));
跟踪(“当前位置%(方法1):”+(100*soundChannel.position/soundSrc.length));
//它也起作用
跟踪(“当前位置%(方法2):”+(100*soundChannel.position/(soundData.length/(44.1*4*2));
}
}
}

太棒了!谢谢你的回复。只是一个小问题。跟踪(“当前位置%:”+(100*ch.position/sound.length));这似乎只运行到大约0.56。i、 e.如果我让录音完全播放,不管它的长度,结束百分比值总是在0.56左右。当前pos在%:0.5668934240362812您知道为什么会这样吗?谢谢,教授,我误解了你的问题。如何用声音数据准备字节数组?当您通过usng
Sound.extract()
方法获得
ByteArray
时,我指的是这个案例。这是一种常见的技术:首先使用
Sound
对象(
srcSound.load(…)
)加载和解码mp3文件,然后提取字节(
srcSound.extract(soundBA…)
),最后用另一种(动态)声音播放提取的字节(
dstSound.play()
)。在这种情况下,您可以获得位置为
100*dstChannel.position/srcSound.length
。如果您有
ByteArray
和已经解码的声音,我假设您可以使用
decodedBA.length/(44.1*4*2)
以毫秒为单位获得声音长度(而不是sound.length)。太棒了。谢谢你的精液。对于任何一个将来的ref,%行是:(sound是解码的bytearray)跟踪(“当前位置在%:”+(100*ch.position/(sound.length/(44.1*4));顺便说一下,从
sound
对象中提取整个声音数据不是很好的方法,因为它会导致大量内存消耗。在每个
SampleDataEvent
期间提取小块数据会更好。您甚至可以将数据直接提取到
event.data
字节数组中。
package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;

    public class SoundTest extends Sprite
    {
        private var soundSrc:Sound;
        private var soundPlayer:Sound;
        private var soundData:ByteArray;
        private var soundChannel:SoundChannel;

        public function SoundTest()
        {
            soundSrc = new Sound();
            soundSrc.addEventListener(Event.COMPLETE, startPlayback);
            soundSrc.load(new URLRequest("sound.mp3"));
        }

        private function startPlayback(e:Event = null):void
        {
            soundData = new ByteArray();
            soundSrc.extract(soundData, soundSrc.length * 44.1, 0);
            soundData.position = 0;

            soundPlayer = new Sound();
            soundPlayer.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
            soundChannel = soundPlayer.play();
            addEventListener(Event.ENTER_FRAME, updateTime);
        }

        private function onSampleData(e:SampleDataEvent):void
        {
            for (var i:int = 0; i < 8192; i++)
            {
                if (soundData.bytesAvailable < 4)
                {
                    break;
                }
                var sampleL:Number = soundData.readFloat();
                var sampleR:Number = soundData.readFloat();
                e.data.writeFloat(sampleL);
                e.data.writeFloat(sampleR);

            }
        }

        private function updateTime(e:Event):void
        {
            trace("current pos in ms: " + soundChannel.position);
            trace("current pos in bytes: " + (soundChannel.position * 44.1 * 4 * 2));
            trace("current pos in % (method 1): " + (100 * soundChannel.position / soundSrc.length));
            // it also works
            trace("current pos in % (method 2): " + (100 * soundChannel.position / (soundData.length / (44.1 * 4 * 2))));
        }
    }
}