Music overlapping in Flash Actionscript 3 - actionscript-3

I'm new with AS3,I'm building a full flash website where I tried to include background music with play,pause and stop button(the play and pause is the same button). The problem is the music player play automatically when the page load and when I click the HOME button of my website the music plays again and overlap with the music that played first, so what should I do..thanks in advance
stop();
function goHome (e:MouseEvent):void{
gotoAndStop("HOME");
}
home_btn.addEventListener(MouseEvent.CLICK, goHome);
function goAbout (e:MouseEvent):void{
gotoAndStop("COMPANY");
}
company_btn.addEventListener(MouseEvent.CLICK, goAbout);
function goTestimony (e:MouseEvent):void{
gotoAndStop("TESTIMONY");
}
news_btn.addEventListener(MouseEvent.CLICK, goTestimony);
function goProduct (e:MouseEvent):void{
gotoAndStop("PRODUCT");
}
product_btn.addEventListener(MouseEvent.CLICK, goProduct);
function goContact (e:MouseEvent):void{
gotoAndStop("CONTACT");
}
contacts_btn.addEventListener(MouseEvent.CLICK, goContact);
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
//Load sound using URLRequest
soundClip.load(new URLRequest("thousandyears.mp3"));
//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
controller.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
stop_btn.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
function onComplete(evt:Event):void {
//Play loaded sound
sndChannel = soundClip.play();
isPlaying = true;
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
controller.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
controller.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
controller.gotoAndStop(2);
isPlaying = false;
}

Is this code in the frame 1?
Is frame 1 labelled 'HOME'?
If so I would add a new frame at the beginning of the timeline (new frame 1) and move the code into it. This way, the code would still be in frame 1, the frame 'HOME' would be frame 2, and so on.
You would need to slightly update your code:
replace stop() with gotoAndStop('HOME');

Related

How to display mp4 then pause and replay in Flash CC

I’m resurrecting a simple banner where we used an FLV movie and the following code to have the movie pause using setInterval and then play again. It’s a simple butterfly movie that flaps it’s wings and then goes back to a static state.
Looks like FLVs are now discouraged in the more recent Flash versions so I would like to know how to make a movie play, then pause using setInterval, then replay.
Here is the working AS3 when using an FLV on the Timeline in a frame based animation but does not work with an imported .mp4:
stop();
function timesUp()
{
gotoAndPlay(1,"Scene 1");
clearInterval(itv);
}
var itv=setInterval(timesUp, 15000);
Do the new suggested methods use a single frame movie where you load and play the mp4, then use setInterval as a timer to replay?
I can’t find any tutorials that have this method as an example.
I did read a tutorial on the Adobe site but the following did not load and play the movie (morpho.mp4)
var nc:NetConnection = new NetConnection();
nc.connect(null);
var vid:Video = new Video();
addChild(vid);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void
{
// handle netStatus events, described later
}
function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore error
}
vid.attachNetStream(ns);
ns.play("morpho.mp4");
Try this :
import flash.utils.Timer
import flash.events.TimerEvent
var video_playing:Boolean = false,
timer:Timer,
nc:NetConnection,
ns:NetStream,
video:Video,
video_name:String = 'video.mp4'
nc = new NetConnection()
nc.connect(null)
ns = new NetStream(nc)
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case 'NetStream.Play.Start' :
if(timer.currentCount == 0){
timer.start()
}
break
}
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
}
video = new Video(135, 135) // set the size of video to 135x135px
video.x = 165 // set the left of video to 300 - 135 = 165px
video.y = 0 // set the top of video to 0
video.attachNetStream(ns)
addChild(video)
timer = new Timer(1000, 3) // 3 seconds, just for example
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_on_Complete)
function timer_on_Complete(e:TimerEvent){
if(video_playing){
video_playing = false
ns.close()
} else {
start_video()
}
timer.reset()
timer.start()
}
start_video()
function start_video(){
ns.play(video_name)
video_playing = true
}

Why is there a lag when pausing a MP3 stream in flash

I have this code to stream a mp3 file:
var isPlaying:Boolean;
var pausePoint:Number;
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound( new URLRequest( 'mp3file.mp3' ) );
var volumeAdjust:SoundTransform = new SoundTransform();
soundChannel = sound.play();
isPlaying = true;
function clickPlayPause(evt:MouseEvent) {
if (isPlaying)
{
pausePoint = soundChannel.position;
soundChannel.stop();
//hide pause sign
control_mc.play_btn.pausee.alpha = 0;
//show play sign
control_mc.play_btn.playy.alpha = 1;
isPlaying = false;
} else {
soundChannel = sound.play(pausePoint);
isPlaying = true;
//hide play sign
control_mc.play_btn.playy.alpha = 0;
//show pause sign
control_mc.play_btn.pausee.alpha = 1;
}
}
function clickStop(evt:MouseEvent) {
if (isPlaying) {
soundChannel.stop();
isPlaying = false;
}
pausePoint = 0.00;
}
So as you can see, the code above also includes some event handling functions to handle my play/pause button. It all works, but it takes a good two seconds for the music to actually stop playing. It also takes a while to start back up again. Does anyone know why this is? And how I can fix it?
Thanks
Greensock tweener has the features to fade in and fade out the audio, that will help you to hide the lag while hearing the audio file.
Import the Greensock tweener and activate the plugins
import com.greensock.*;
import com.greensock.plugins.*;
TweenPlugin.activate([VolumePlugin]);
then
if(palying){
TweenLite.to(soundChannel, .5, {volume:0});
}else{
TweenLite.to(soundChannel, .5, {volume:1});
}

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.