How do i play a song from library when the preloader is a "x"% of total loaded? - actionscript-3

I am currently undertaking a final exam based on the presentation of an offline portfolio and I need to make a preloader, where when the process of loading is 25% of total bytes, to play a sound that is imported into the library. I've tried several ways and can’t do it.
I'll leave you the code for my preloader.
//(also this code is a mouse loader in text form)
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS,checkingProgress);
function checkingProgress(event:ProgressEvent):void{
var procentLoaded:Number = event.bytesLoaded/event.bytesTotal*100;
ptxt.text=int(procentLoaded)+"%";
if(procentLoaded == 100){
this.gotoAndPlay(1,"Video-Int");
}
}
stage.addChild(ptxt);
ptxt.mouseEnabled = false;
ptxt.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event) {
ptxt.x = stage.mouseX;
ptxt.y = stage.mouseY;
}
Mouse.hide();
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(e:Event):void {
var total:Number = this.stage.loaderInfo.bytesTotal;
var loaded:Number = this.stage.loaderInfo.bytesLoaded;
prel.scaleX = loaded/total;
if (total == loaded){
play();
this.removeEventListener(Event.ENTER_FRAME, loading);
}
}

You need to make the sound file in your library available for actionscript.
Give it a Class name ex. MySound
...
var mysound:MySound = new MySound();
var soundPLaying:Boolean = false;
function checkingProgress(event:ProgressEvent):void{
var procentLoaded:Number = event.bytesLoaded/event.bytesTotal*100;
ptxt.text= procentLoaded +"%";
if(procentLoaded > 25 && !soundPlaying){ //check if loaded mor then 25% and sound isn't playing
mysound.play();
soundPlaying = true;
}
if(procentLoaded == 100){
this.gotoAndPlay(1,"Video-Int");
}
}
...
if you need to do some extra stuff like pause, change volume etc you will need a SoundChannel Object

Related

mute unmute button in actionscript 3

i need some help with my actionscript 3 project. I have a button with a sound in it. I have some code (see below) that when i press the button it plays the sound and if i press the button again it stops the sound (like a mute/unmute button). The problem is that when i press the button to play the sound for the second time it plays two sounds (the same sound twice) and if i press the button to play the sound more times the same sound plays many times. Can you please help me solve the problem? Thank you.
function setMute1(vol){
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
}
var sTransform1:SoundTransform = new SoundTransform(1,0);
var Mute1:Boolean = true;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
function toggleMuteBtn1(event:Event) {
if(Mute1 === false) {
Mute1 = true;
setMute1(0);
} else {
Mute1 = false;
setMute1(1);
}
}
From what I understand you start the sound by assigning it to the buttons hit frame? You need to have the sound started by the code in order to control the sound in good way.
Here is an working example based on your code, that loads an external mp3 file. The sound is played and stopped via the same button.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Start/stop sound
function startOrStop()
{
if (Mute1 === false)
{
myChannel.stop();
setMute1(0);
}
else
{
setMute1(1);
myChannel = mySound.play();
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
startOrStop()
}
In actionscrip 2 there was a function that would stop all sounds, in actionscript 3 you can't do that anymore, but you can still assign sounds to frames.
This example mutes and unmutes the sound. The sound is't stopped, just muted.
Also here the sound must be assigned in the code, and not to the frame.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Toggle mute on/off
function toggleMute()
{
if (Mute1 === false)
{
setMute1(0);
}
else
{
setMute1(1);
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
// if not playing, the sound
if (myChannel.position != 0) {
} else {
myChannel = mySound.play();
}
toggleMute();
}

action script 3.0 .I had tried a program that should move the dragger as well as the content movieclip,

In this code i need a progressbar to progress through while the imported .swf file is playing, meanwhile i should able to drag the dragger in the progress bar (i.e the rate of movement of dragger should sync with rate of .swf file playing). I got Argument error: #2109 Frame label 459.99 not found in scene1.
var loader:Loader = new Loader();
playBtn.visible = true;
pauseBtn.visible = false;
btn_00.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_01.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_02.addEventListener(MouseEvent.CLICK, fileLoaded);
function fileLoaded(evt:MouseEvent):void
{
var fileName:String = evt.currentTarget.name;
var fileNumber:String = fileName.split("_")[1];
var urlPath:String = "assets/file_" + fileNumber + ".swf";
loader.load(new URLRequest(urlPath));
addChild(loader);
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
function swfLoaded(event:Event):void
{
addEventListener(Event.ENTER_FRAME, trackPlayback);
}
function trackPlayback(event:Event):void
{
var perPlayed:Number = MovieClip(loader.content).currentFrame / MovieClip(loader.content).totalFrames;
progressbar.drag.x = (progressbar.bar.width - progressbar.drag.width) * perPlayed;
}
progressbar.drag.buttonMode = true;
var dragClicked:Boolean = false;
var xpos:Number = progressbar.bar.x * progressbar.drag.width;
progressbar.drag.addEventListener(MouseEvent.MOUSE_DOWN,dragMouseDown);
function dragMouseDown(evt:MouseEvent):void
{
trace(" inside mouse down ");
dragClicked = true;
progressbar.drag.startDrag(false,new Rectangle(xpos,0,progressbar.width-progressbar.drag.width,0));
}
progressbar.drag.addEventListener(MouseEvent.MOUSE_UP,dragMouseUp);
function dragMouseUp(evt:MouseEvent):void
{
dragClicked = false;
progressbar.drag.stopDrag();
var cnt:Number = (progressbar.drag.x/(progressbar.width-progressbar.drag.width))*MovieClip(loader.content).totalFrames;
MovieClip(loader.content).gotoAndPlay(cnt);
}
Pls solve my issue.
Thanks in advance.
To access a specific frame, you need to provide an integer value where at the moment you are using a floating-point value.
A simple fix would be to cast the Number to an int:
MovieClip(loader.content).gotoAndPlay(int(cnt));

Flash video in movieclip problems

I have several video files playing on the stage. I've converted them into movieclips so that I can scale and drag them by clicking. The problem is that I cannot loop them.
Then I tried to make them as SWF playback object's but after that my code wasn't working with them.
Next step was to make them Embedded video objects so that they loop automatically and the code is working. After that there appeared a problem that the objects are duplicating at some point.
Here's the original code as the videos are movieclips.
var allDraggables:Array = new Array();
var mouseHold = false;
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mUp(MouseEvent)
{
mouseHold = false;
}
function draggableObject(mc)
{
var mouseOnThisObject = false;
allDraggables.push(mc);
mc.addEventListener(Event.ENTER_FRAME, drag);
mc.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
function mDown(MouseEvent)
{
mouseHold = true;
mouseOnThisObject = true;
}
function drag(MouseEvent)
{
if (mouseHold == true && mouseOnThisObject == true)
{
mc.addEventListener(Event.ENTER_FRAME, dragger);
}
if (mouseHold == false)
{
mc.removeEventListener(Event.ENTER_FRAME, dragger);
mouseOnThisObject = false;
}
}
mc.doubleClickEnabled = true;
mc.addEventListener(MouseEvent.DOUBLE_CLICK, scaleMe);
function scaleMe(e:MouseEvent)
{
if (e.target.scaleX < 2)
{
e.target.scaleX= e.target.scaleY = 2;
}
else (e.target.scaleX= e.target.scaleY = 1);
}
function dragger(Event)
{
mc.x+=(mouseX-mc.x)/3;
mc.y+=(mouseY-mc.y)/3;
for (var i:int=0; i<allDraggables.length; i++){
if(mc.hitTestObject(allDraggables[i]) && getChildIndex(allDraggables[i]) > getChildIndex(mc)){
swapChildren(allDraggables[i], mc)
}
}
}
}
draggableObject(green);
draggableObject(red);
draggableObject(video1);
draggableObject(video2);
draggableObject(video3);
Well it's hard to tell what you've tried exactly, since you haven't provided any code (yet)..
However, from the top of my head, I think this will work:
if(videoMC1.currentFrame == 250) { //put the number of the last frame of the movieclip in place of 250
loopMC();
}
function loopMC() {
videoMC1.stop();
videoMC1.gotoAndPlay(1);
}
What you do here is simple; you check the current frame that is passed/playing and when it reaches the desired number (in your case most likely the last frame) it calls a function that resets and plays the video.
I found some old code I once used in a project, maybe you could try this. In my flash app it worked, although I didn't put the video into a movieclip.
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
var nc:NetConnection = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.bufferTime = 10;
var vid:Video = new Video(1024,640);
vid.attachNetStream(ns);
addChild(vid);
ns.addEventListener(NetStatusEvent.NET_STATUS, ns_onPlayStatus)
function ns_onPlayStatus(event:NetStatusEvent):void {//loop video
if(event.info.code == "NetStream.Play.Stop") {
ns.seek(0);
}
}

mp3 Looping Problems-Actionscript 3

This is so weird, this post wont let me start the sentence with hello hi or howdy maybe cache issue, anyways, hello, i'm very very new to AS3 so my knowledge is limited, hoping you pros can take it easy on the new guy :).
I'm building a flash site with an animation that loops and i've added an mp3 song as background music, it's also set to loop and auto play. the problem is, that the song is longer than the animation, and when the animation goes back to frame 1, the song overlaps the one that's still playing. I'd like for the song to fully finish before it starts playing again. yeaaah there's probably a heap of code missing, but i got closer to what i want, just need your help to polish it a little.
here is the code so far
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean=false;
var pausePosition:Number=0;
var myMusic:Sound=new Sound();
var soundFile:URLRequest=new URLRequest("Fluke.mp3");
myMusic.load(soundFile);
var channel:SoundChannel;
//if commands
if (AutoPlay==true) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
//pausebutton functions
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
play_btn.addEventListener(MouseEvent.CLICK,playMus ic);
function playMusic(event:MouseEvent):void
{ if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
}
THIS is the full working code thanks to Josha, with an .addEventListener that i added so that the song would loop after its finished playing.
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;
// only create music and register event listeners one time
if (myMusic == null)
{
soundFile = new URLRequest("Fluke.mp3");
myMusic = new Sound();
myMusic.load(soundFile);
pausePosition = 0;
if (AutoPlay)
{
channel = myMusic.play(pausePosition);
isPlaying = true;
}
else
{
isPlaying = false;
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event) {
pausePosition = 0;
channel = myMusic.play(pausePosition);
isPlaying = true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
//playbutton functions
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
function playMusic(event:MouseEvent):void
{
if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
}
I can not place comments yet, so have to use answer.
Are you placing your code in separate AS files; or is this code placed at the first frame of your animation?
If the latter is the case, that indeed you will get overlapping sounds. Since the actionscript code is executed every time the playhead returns to frame 1; creating a new Sound object and starting to play that sound.
A simple solution is to create a key frame at the last frame and add the following action script:
gotoAndPlay(2);
This will loop your animation from frame 2, skipping over the action script.
Another solution that might work (you would have to test it):
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;
// only create music and register event listeners one time
if (myMusic == null)
{
soundFile = new URLRequest("Fluke.mp3");
myMusic = new Sound();
myMusic.load(soundFile);
pausePosition = 0;
if (AutoPlay)
{
channel = myMusic.play(pausePosition);
isPlaying = true;
}
else
{
isPlaying = false;
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
}
//playbutton functions
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
function playMusic(event:MouseEvent):void
{
if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
}

music playing over and over in Actionscript 3

Greeting,
I I developed a website using flash with Actionscript 3.
I included a music as a background for the website and the music will loaded when the site loaded.
but the problem that when I click buttons to move between pages(frames) then go and click button_01 the music will play again so I will have music playing more than one time in the background
and the sound_btn will not work any more so even I click sound_btn the music will not stop.
the code I'm using is listed down.
Please advice me what I should modify to not allow the music play more than one time in the background while moving from one page(frame) to another.
Regards,
stop();
//number that is redefined when the pause button is hit
var pausePoint:Number = 0.00;
//a true or false value that is used to check whether the sound is currently playing
var isPlaying:Boolean;
//think of the soundchannel as a speaker system and the sound as an mp3 player
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound(new URLRequest("music.mp3"));
//you should set the xstop and xplay values to match the instance names of your stop button and play/pause buttons
//mute_btn.addEventListener(MouseEvent.CLICK, clickStop);
sound_btn.addEventListener(MouseEvent.CLICK, clickPlayPause);
soundChannel = sound.play();
isPlaying = true;
myVideo.stop();
function clickPlayPause(evt:MouseEvent) {
if (isPlaying) {
pausePoint = soundChannel.position;
soundChannel.stop();
isPlaying = false;
} else {
soundChannel = sound.play(pausePoint);
isPlaying = true;
}
}
button_01.addEventListener(MouseEvent.CLICK, onClick1);
button_02.addEventListener(MouseEvent.CLICK, onClick2);
button_03.addEventListener(MouseEvent.CLICK, onClick3);
button_04.addEventListener(MouseEvent.CLICK, onClick4);
button_05.addEventListener(MouseEvent.CLICK, onClick5);
button_06.addEventListener(MouseEvent.CLICK, onClick6);
function onClick1(e:MouseEvent):void
{
gotoAndStop(1);
}
function onClick2(event:MouseEvent):void
{
gotoAndStop(2);
}
function onClick3(event:MouseEvent):void
{
gotoAndStop(3);
}
function onClick4(event:MouseEvent):void
{
gotoAndStop(4);
}
function onClick5(event:MouseEvent):void
{
gotoAndStop(5);
}
function onClick6(event:MouseEvent):void
{
gotoAndStop(6);
}
The problem is your code for the sound is initialized on the frame that you send the timeline to when clicking button_01. It will reinitialize each time you do that. Try initializing your sound code one frame earlier so that you do not land on that frame ever again once your page loads.
You also might find that wrapping your pages into movieclips, and using visible = true/false to change sections might be a better approach than advancing the timeline to change sections. That method would not result in the sound code reinitializing each time you changed sections. something like this:
function onClick1(e:MouseEvent):void
{
hideAll();
section_01.visible = true;
}
function onClick2(event:MouseEvent):void
{
hideAll();
section_02.visible = true;
}
function onClick3(event:MouseEvent):void
{
hideAll();
section_03.visible = true;
}
function onClick4(event:MouseEvent):void
{
hideAll();
section_04.visible = true;
}
function onClick5(event:MouseEvent):void
{
hideAll();
section_05.visible = true;
}
function onClick6(event:MouseEvent):void
{
hideAll();
section_06.visible = true;
}
function hideAll():void
{
section_01.visible = false;
section_02.visible = false;
section_03.visible = false;
section_04.visible = false;
section_05.visible = false;
section_06.visible = false;
}
If you wanted tweened transitions you could use a tweening class to handle the transitions by tweening the current section out in the hide function and then tweening the next section in in its respective onCLick function.