How to detect my sound already played to the end?

I’ve created a small project which has some images and one mp3 sound file. Basically, it looks like a slideshow with a narration audio (playing once, no loop). Users can use buttons to control the slideshow to pause, play, and stop. All 3 buttons fuction well.

Here’s my AS:


var ts:TheSound = new TheSound();
var sc:SoundChannel = ts.play();
var position:Number;
this.visible = true;
ts.addEventListener(Event.COMPLETE, completeHandler);
function completeHandler(e:Event):void {
 this.visible = true;
}
function soundComplete(e:Event):void {
 if (sc) {
  sc.stop();
  sc = null;
 }
 position = 0;
}
play_btn.addEventListener(MouseEvent.CLICK, bClick);
pause_btn.addEventListener(MouseEvent.CLICK, bClick);
stop_btn.addEventListener(MouseEvent.CLICK, bClick);
function bClick(e:MouseEvent) {
 switch (e.target) {
  case play_btn :
   sc = ts.play(position);
   play_btn.visible = false;
   pause_btn.visible = true;
   pic_mc.play();
   break;
  case stop_btn :
   soundComplete(null);
   play_btn.visible = true;
   pause_btn.visible = false;
   pic_mc.gotoAndStop("start");
   break;
  case pause_btn :
   position = sc.position;
   sc.stop();
   pause_btn.visible = false;
   play_btn.visible = true;
   pic_mc.stop();
   break;
 }
}

However, I want to have all 3 buttons invisible and add a replay button when the whole slideshow/narration plays to the end. I guess that I should first know how to detect this event? How to do it?

Thanks a lot.

I’d recommend sptitting that bClick function to three ones regarding less clutter and performance.
Look carefully http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/index.html?flash/media/SoundChannel.html&flash/media/class-list.html
The Event.SOUND_COMPLETE - Dispatched when a sound has finished playing.

[quote=Felixz;2350360]I’d recommend sptitting that bClick function to three ones regarding less clutter and performance.
Look carefully http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/index.html?flash/media/SoundChannel.html&flash/media/class-list.html
The Event.SOUND_COMPLETE - Dispatched when a sound has finished playing.[/quote]

Thanks for suggestion. :slight_smile:
I’ve spitited the bClick function to three ones.
However, my original question hasn’t solved yet.

Any one has solutions?

==p.s.==
You can see the swf result here:
http://gofatwallet.googlepages.com/slideshow02-test.swf

I also attached the fla file here, hope it helps…
http://gofatwallet.googlepages.com/slideshow02-test2.fla

Quote from site I gave you

soundComplete Event

Event Object Type: flash.events.Event
Event.type property = flash.events.Event.SOUND_COMPLETE

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

Dispatched when a sound has finished playing.

The Event.SOUND_COMPLETE constant defines the value of the type property of a soundComplete event object.
This event has the following properties:

Property Value
bubbles false
cancelable false; there is no default behavior to cancel.
currentTarget The object that is actively processing the Event object with an event listener.
target The Sound object on which a sound has finished playing.

Example

In the following example, the user selects songs from a playlist, and then clicks Play to play the song in the order selected.
In the constructor, a text field is defined that holds the song list and a line for the selection to play. (Usually, buttons are used for play and list boxes for a song list.) A text format object is defined that changes the format of the song lines to italic after they are selected. When a user clicks the text field, the clickHandler() method is invoked.

In the clickHandler() method, the getLineIndexAtPoint() method of the text field object returns the index of the line where the user clicked. Using the line index, the getLineText() method gets the content of the text. The if statement checks whether the user selected to play a song or add a song to the play list. If a user has selected to play and a song has been selected, then the event listener for mouse click is removed and the playNext() method is called to begin playing the songs. If the user selected a song title, the content of the line is added to the songList array and the format of the line is set to italic.

The playNext() method iterates through the array list to load and play each song. The song is also assigned to a sound channel. An event listener for the sound channel is added to respond when the song finishes playing and the Event.SOUND_COMPLETE event is dispatched. The soundCompleteHandler() method then invokes the playNext() method to play the next song. This process continues until all the songs listed in the array finish playing.

package {
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;
import flash.text.TextFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;

public class SoundChannel_event_soundCompleteExample extends Sprite {
private var channel:SoundChannel = new SoundChannel();
private var songList:Array = new Array();
private var listTextField:TextField = new TextField();
private var songFormat:TextFormat = new TextFormat();
private var arrayIndex:int = 0;
private var songSelected:Boolean = false;

public function SoundChannel_event_soundCompleteExample() {

listTextField.autoSize = TextFieldAutoSize.LEFT;
listTextField.border = true
listTextField.background = true;
listTextField.text = "Song1.mp3
" + "Song2.mp3
"

  • "Song3.mp3
    " + "Song4.mp3
    " + “PLAY”;

songFormat.italic = true;

listTextField.addEventListener(MouseEvent.CLICK, clickHandler);

addChild(listTextField);
}

private function clickHandler(e:MouseEvent):void {
var index:int = listTextField.getLineIndexAtPoint(e.localX, e.localY);
var line:String = listTextField.getLineText(index);
var firstIndex:uint = listTextField.getLineOffset(index);
var playLine:uint = listTextField.numLines - 1;

if((index == playLine) && (songSelected == true)) {
listTextField.removeEventListener(MouseEvent.CLICK, clickHandler);
playNext();

} else if (index != playLine) {
songList.push(line.substr(0, (line.length - 1)));
listTextField.setTextFormat(songFormat, firstIndex,
(firstIndex + listTextField.getLineLength(index)));
songSelected = true;
}
}

private function playNext():void {

if(arrayIndex < songList.length) {
var snd:Sound = new Sound();
snd.load(new URLRequest(songList[arrayIndex]));
channel = snd.play();

channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
arrayIndex++;

} else {
songSelected = false;

while(arrayIndex > 0) {
songList.pop();
arrayIndex–;
}
}
}

private function soundCompleteHandler(e:Event):void {
playNext();
}

private function errorHandler(errorEvent:IOErrorEvent):void {
trace(errorEvent.text);
}
}
}

Well, create a listener which would just set visible property of those buttons