mp3 Looping Problems-Actionscript 3 - 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;
}
}

Related

Actionscript 3 Code Keeps Giving Error 1009

I managed to apply this code, which fetches a text file and places it into a dynamic text box. Here's the code:
// reference code from permadi
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
import fl.controls.ProgressBarMode;
import fl.controls.TextArea;
var mFileReference:FileReference;
stop();
// Setup button to handle browsing
browseButton.buttonMode = true;
browseButton.mouseChildren = false;
browseButton.addEventListener(MouseEvent.CLICK, onBrowseButtonClicked);
// Hide progress bar;
progressBar.visible = false;
// This function is called when the BROWSE button is clicked.
function onBrowseButtonClicked(event:MouseEvent):void
{
trace("onBrowse");
mFileReference=new FileReference();
mFileReference.addEventListener(Event.SELECT, onFileSelected);
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; *.html;*.htm;*.php");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
mFileReference.browse([swfTypeFilter, allTypeFilter]);
}
// This function is called after user selected a file in the file browser dialog.;
function onFileSelected(event:Event):void
{
trace("onFileSelected");
// This callback will be called when the file is uploaded and ready to use
mFileReference.addEventListener(Event.COMPLETE, onFileLoaded);
// This callback will be called if there's error during uploading;
mFileReference.addEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
// Optional callback to track progress of uploading;
mFileReference.addEventListener(ProgressEvent.PROGRESS, onProgress);
// Tells the FileReference to load the file;
mFileReference.load();
// Show progress bar;
progressBar.visible = true;
progressBar.mode = ProgressBarMode.MANUAL;
progressBar.minimum = 0;
progressBar.maximum = 100;
browseButton.visible = false;
}
// This function is called to notify us of the uploading progress
function onProgress(event:ProgressEvent):void
{
var percentLoaded:Number = event.bytesLoaded / event.bytesTotal * 100;
trace("loaded: "+percentLoaded+"%");
progressBar.setProgress(percentLoaded, 100);
}
// This function is called after the file has been uploaded.;
function onFileLoaded(event:Event):void
{
var fileReference:FileReference = event.target as FileReference;
// These steps below are to pass the data as DisplayObject
// These steps below are specific to this example.
var data:ByteArray = fileReference["data"];
codeText.text = data.toString();
browseButton.visible = true;
progressBar.visible = false;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}
function onFileLoadError(event:Event):void
{
// Hide progress bar
progressBar.visible = false;
browseButton.visible = true;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
trace("File load error");
}
It works fine, I added the loaded file to a string, but now I want Flash to read the string and send it to the related frames corresponding to it. Here's the code:
stage.addEventListener(Event.ENTER_FRAME, updateFunction);
function updateFunction(e:Event)
{
if (codeText.text == "Preference 01")
{
gotoAndStop(50);
}
else if (codeText.text == "Preference 02")
{
gotoAndStop(51);
}
else if (codeText.text == "Preference 03")
{
gotoAndStop(52);
}
else if (codeText.text == "Preference 04")
{
gotoAndStop(53);
}
else
{
gotoAndStop(54);
}
}
I Keep Getting Error 1009 About A Null Object, I Tried For Hours But I Think I Need Some Help.
If all you want is to go to a different frame based on what you loaded, simply remove your updateFunction and replace your onFileLoaded() function with the following:
function onFileLoaded(event:Event):void {
var fileReference:FileReference = event.target as FileReference;
// These steps below are to pass the data as DisplayObject
// These steps below are specific to this example.
var data:ByteArray = fileReference["data"];
browseButton.visible = true;
progressBar.visible = false;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
codeText.text = data.toString();
switch (codeText.text) {
case "Preference 01":
gotoAndStop(50);
break;
case "Preference 02":
gotoAndStop(51);
break;
case "Preference 03":
gotoAndStop(52);
break;
case "Preference 04":
gotoAndStop(53);
break;
default:
gotoAndStop(54);
}
}
Please be sure to enable Debug mode and compile in that mode so you can get specifics like line numbers on your errors. Also, to avoid BotMaster's complaints in the future, be sure to read https://stackoverflow.com/help/mcve

error in my actionscript for a sound slider I'm working on

Hello I' trying to make a sound slider in flash, while working on the code for it; I keep on getting this error Scene 1, 'S Action', Frame 352, line 8 1152: A conflict exists with inherited definition flash.display:MoveClip.isPlaying in namespace public. I'm wondering what I done to cause this error and how can I fix it?
Please get back to me if you can.
stop();
import flash.media.SoundTransform;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.SoundChannel;
var isPlaying:Boolean = true;
var lastPosition:Number = 0;
var mySound:takeachance = new takeachance ;
var myChannel:SoundChannel = new SoundChannel();
var myTransform:SoundTransform = new SoundTransform();
myTransform.volume = .5;
slider02_mc.groove02_mc.scaleX = .5;
myChannel = mySound.play(85,5,myTransform);
myChannel.addEventListener(Event.SOUND_COMPLETE,
soundCompleteHandler);
function soundCompleteHandler(e:Event):void
{
lastPosition=0;
myChannel.stop();
msg_mc.text = "Music stoped playing"
isPlaying = false;
}
play_btn.addEventListener(MouseEvent.CLICK,startSound);
pause_btn.addEventListener(MouseEvent.CLICK,stopSound);
function startSound(myEvent:MouseEvent):void
{
if(!isPlaying)
{
myChannel = mySound.play(lastPosition,5,myTransform);
isPlaying = true;
msg_mc.text="";
myChannel.addEventListener(Event.SOUND_COMPLETE,
soundCompleteHandler);
}
}
function stopSound(myEvent:MouseEvent):void
{
lastPostion = myChannel.position;
myChannel.stop();
isPlaying = false;
}
slider02_mc.mc.addEventListener(MouseEvent.MOUSE_DOWN, myfunction);
function myfunction(event:MouseEvent):void
{
slider02_mc.mc.startDrag(false,new Rectangle(0,0,200,0))
addEventListener(Event.ENTER_FRAME,xpos);
function xpos(event:Event)
{
var myvolume=(slider02_mc.mc.x)/200;
slider02_mc.groove02_mc.scaleX = myvolume;
volume_mc.text = "Volume is"+int(myvolume*100)+"%";
myTransform.volume = myvolume;
myChannel.soundTransform = myTransform;
}
}
stage.addEventListener(MouseEvent.MOUSE_UP,myfunction1);
function myfunction1(event:MouseEvent):void
{
slider02_mc.mc.stopDrag();
addEventListener(Event.ENTER_FRAME,msg);
function msg(event:Event)
{
volume_mc.text="";
}
}
Movieclips already have a property called isPlaying. You should change your variable to something else like soundIsPlaying.

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);
}
}

Music overlapping in Flash 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');