Skip to content

Commit

Permalink
avm2: Report ProgressEvent when a sound is loaded (close ruffle-rs#8932)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCheeze authored and Lord-McSweeney committed Nov 21, 2023
1 parent 1ce772b commit d23d16c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
22 changes: 22 additions & 0 deletions core/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,12 +1434,34 @@ impl<'gc> Loader<'gc> {
tracing::error!("Encountered AVM2 error when setting sound: {}", e);
}

let total_len = response.body.len();

// FIXME - the "open" event should be fired earlier, and not fired in case of ioerror.
let mut activation = Avm2Activation::from_nothing(uc.reborrow());
let open_evt =
Avm2EventObject::bare_default_event(&mut activation.context, "open");
Avm2::dispatch_event(&mut activation.context, open_evt, sound_object);

// FIXME - As in load_url_loader, we should fire "progress" events as we receive data,
// not just at the end
let progress_evt = activation
.avm2()
.classes()
.progressevent
.construct(
&mut activation,
&[
"progress".into(),
false.into(),
false.into(),
total_len.into(),
total_len.into(),
],
)
.map_err(|e| Error::Avm2Error(e.to_string()))?;

Avm2::dispatch_event(&mut activation.context, progress_evt, sound_object);

let complete_evt = Avm2EventObject::bare_default_event(
&mut activation.context,
"complete",
Expand Down
20 changes: 19 additions & 1 deletion tests/tests/swfs/avm2/sound_constructor_with_args/Test.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,37 @@
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;

public class Test extends MovieClip {
public function Test() {
var sound:Sound = new Sound(new URLRequest("noise.mp3"));
sound.addEventListener(Event.OPEN, OnOpen);
sound.addEventListener(ProgressEvent.PROGRESS, OnProgress)
sound.addEventListener(Event.COMPLETE, OnComplete);
trace("Loaded sound");
var sound_channel:SoundChannel = sound.play();
sound_channel.addEventListener(Event.SOUND_COMPLETE, this.PlaybackFinished);
trace("Playing sound");
}

private function OnOpen(evt:Event):void {
trace("Callback: Open");
}

private function OnProgress(evt:Event):void {
if (evt.target.bytesLoaded >= evt.target.bytesTotal) {
trace("Callback: Progress - " + evt.target.bytesLoaded + " / " + evt.target.bytesTotal);
}
}

private function OnComplete(evt:Event):void {
trace("Callback: Complete - " + evt.target.bytesLoaded + " / " + evt.target.bytesTotal);
}

private function PlaybackFinished(evt:Event):void {
trace("Finished playback");
trace("Callback: Finished playback");
}
}
}
5 changes: 4 additions & 1 deletion tests/tests/swfs/avm2/sound_constructor_with_args/output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Loaded sound
Playing sound
Finished playback
Callback: Open
Callback: Progress - 4180 / 4180
Callback: Complete - 4180 / 4180
Callback: Finished playback
Binary file modified tests/tests/swfs/avm2/sound_constructor_with_args/test.swf
Binary file not shown.

0 comments on commit d23d16c

Please sign in to comment.