声音字节数组的AS3播放不会在开始时开始

时间:2011-12-13 14:53:11

标签: actionscript-3 audio bytearray

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

有人会有任何想法可能会发生这种情况吗?

谢谢,

标记

var soundBA:ByteArray = new ByteArray();
var sound:Sound = new Sound();
var ch:SoundChannel = new SoundChannel();
var recordingsArray:Array = new Array();

//想象我已成功录制并将声音存储到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);

    }

}

1 个答案:

答案 0 :(得分:3)

这是因为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 :我指的是使用其他Sound对象解码声音的情况,例如:

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))));
        }
    }
}