Вопрос

With the Sound.play function, you can specify a start time in milliseconds as one of the arguments, as well as the number of times to loop the sound. Am I missing something, or is there not a way to specify an end time? For example, if I want the loop milliseconds 5-105 of a 4 second sound, is there a way to specify it to start the next loop at 105 milliseconds?

If I'm not missing something, is there another way to go about this?

Это было полезно?

Решение

The Sound class doesn't allow you to perform that operation, the best way to do this is by using a Timer:

var musicTimer:Timer = new Timer(100, 5); // Loop total time(ms), N Loops (+1)
musicTimer.addEventListener(TimerEvent.TIMER, musicTimer_tick);
musicTimer.addEventListener(TimerEvent.TIMER_COMPLETE, musicTimer_complete);
musicTimer.start();

private function musicTimer_complete(e:TimerEvent):void 
{
    // Last loop stop all sounds
    channel.stop();
}

private function musicTimer_tick(e:TimerEvent):void 
{
    // At each loop, stop and (re)start the sound
    channel.stop();
    channel = sound.play(5); // 5 is the loop start time
}

Другие советы

You can create another Sound object tailored for your desired looping settings. You then call Sound:extract() against your source sound, extracting the exact values that your sound needs to loop, then on your target sound you call loadPCMFromByteArray() and then loop that sound.

function loopASound(sound:Sound,startTime:uint,endTime:uint,loops:int):SoundChannel {
    var ba:ByteArray=new ByteArray();
    var startPos:uint=Math.floor(startTime*44.1);
    var endPos:uint=Math.floor(endTime*44.1); 
    // sound rate is always 44100, and positions are expected in milliseconds,
    // as with all the functions of Sound class
    sound.extract(ba,endPos-startPos,startPos); // get raw data
    var ripped:Sound=new Sound();
    ba.position=0;
    ripped.loadPCMFromByteArray(ba,endPos-startPos); // format, stereo and sample rate at defaults
    return ripped.play(0,loops);
}

Warning: the code is untested and produced only by reading the manuals. Also you might want to store the ripped sound somewhere too, so you won't do the ripping all the time if you're planning to loop the same segment more than several times.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top