How to make a volume slider that uses channels - actionscript-3

I am trying to make a volume slider that uses channels.
I am not sure how to do this.
Here is my code:
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.media.Sound;
// Enter Frame Event
var sliderValue:uint = mySlider.sliderKnob.x;
addEventListener(Event.ENTER_FRAME, frame);
function frame(event):void {
sliderValue = mySlider.sliderKnob.x ;
status_txt.text = "Volume: "+sliderValue;
var snd:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var volumeLevel = channel.soundTransform;
var req:URLRequest = new URLRequest("Background.mp3")
snd.load(req);
channel = snd.play();
var newLevel:Number = mySlider.sliderKnob.x / 100;
volumeLevel.volume = newLevel;
channel.soundTransform = volumeLevel;
}

You are instantiating and loading the sound on every single frame. And you need to make a new SoundTransform instance! I take it that the slider itself is working!
var snd:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var volumeLevel:SoundTransform = new SoundTransform();
snd.load(new URLRequest("Background.mp3"));
channel = snd.play();
addEventListener(Event.ENTER_FRAME, frame);
function frame(event):void {
sliderValue = mySlider.sliderKnob.x ;
status_txt.text = "Volume: "+sliderValue;
var newLevel:Number = sliderValue / 100;
volumeLevel.volume = newLevel;
channel.soundTransform = volumeLevel;
}

Related

Fade IN sound with Actionscript 3.0

I have managed to fade OUT sound with this code and plugins from GreenSock:
import com.greensock.TweenLite;
import com.greensock.TweenMax;
var snd = new buller();
var channel:SoundChannel = snd.play(0,999);
TweenMax.to(channel, 1, {volume:0, onComplete:snd.stop});
What I can't do is fading IN the sound. Have tried to set an initial sound level but can't make it work. Appreciate any help I can get.
var snd = new buller();
var channel:SoundChannel = new SoundChannel();
var St::SoundTransform = new SoundTransform();
var TimerFade:Timer = new Timer(1,100);
//set the initial sound volume
St.volume = 1;
//play the sound
channel = snd.play(0,0,St);
//Fade in/out the sound
TimerFade.addEventListener(TimerEvent.TIMER,VolumeHandler);
TimerFade.addEventListener(TimerEvent.TIMER_COMPLETE,StopSound);
TimerFade.start();
private function VolumeHandler(e:TimerEvent):void{
var Lastpos:Number = channel.position;
channel.stop();
St.volume -= 0.1;
channel = snd.play(Lastpos,0,St);
}
private function VolumeHandler(e:TimerEvent):void{
channel.stop();
}

TypeError: Error #1006: addChild is not a function

I'm trying to embed a "pre-made" flash actionscript 3 gallery into my own flash animation. Both animations work in good ways independently. But once I insert the photo gallery into my own project I am getting an error of;
TypeError: Error #1006: addChild is not a function.
at project_fla::galeri_6/onCompleteXmlLoad()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
My flash button code which leads to gallery is;
stop();
galeri_btn.addEventListener(MouseEvent.CLICK, galeri);
function galeri(event:MouseEvent):void
{
gotoAndStop(3);
}
And lastly, the flash gallery code is;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
var xmlLoader:URLLoader = new URLLoader(new URLRequest("photo_gallery.xml"));
xmlLoader.addEventListener(Event.COMPLETE, onCompleteXmlLoad);
var xmlFile:XML;
var xcoord:int = 10;
var gal:gallery = new gallery();
gal.x = 10;
gal.y = 10;
addChild(gal);
var thumbsContainer:Sprite = new Sprite();
thumbsContainer.x = 10;
thumbsContainer.y = 320;
addChild(thumbsContainer);
var txtTitle:TextField = new TextField();
txtTitle.x = 15;
txtTitle.y = 15;
var format:TextFormat = new TextFormat();
format.bold = true;
format.color = 0xFFFFFF;
format.size = "20";
format.font = "Arial";
txtTitle.defaultTextFormat = format;
addChild(txtTitle);
var txtDesc:TextField = new TextField();
txtDesc.x = 15;
txtDesc.y = 40;
var format1:TextFormat = new TextFormat();
format1.color = 0x000000;
format.font = "Calibri";
format.size = 12;
txtDesc.defaultTextFormat = format1;
addChild(txtDesc);
function onCompleteXmlLoad(e:Event):void{
xmlFile = new XML(xmlLoader.data);
var len:int = xmlFile.photo.length();
txtTitle.text = xmlFile.photo.name[0];
txtDesc.text = xmlFile.photo.desc[0];
for(var i:int = 0;i<len;i++){
var t:thumbs = new thumbs();
t.x = xcoord;
t.y = 10;
t.buttonMode = true;
t.name = (i+1).toString();
thumbsContainer.addChild(t);
t.addEventListener(MouseEvent.MOUSE_OVER, onMouseover);
t.addEventListener(MouseEvent.MOUSE_OUT, onMouseout);
t.addEventListener(MouseEvent.CLICK, onMouseClick);
var tloader:Loader = new Loader();
tloader.load(new URLRequest("thumbs/" + (i+1) + ".jpg"));
t.addChild(tloader);
xcoord += t.width + 10;
}
var loader:Loader = new Loader();
loader.load(new URLRequest("img/1.jpg"));
gal.addChild(loader);
scroller.source = thumbsContainer;
scroller.setSize(550,110);
}
function onMouseover(e:MouseEvent):void{
e.currentTarget.alpha = 0.5;
}
function onMouseout(e:MouseEvent):void{
e.currentTarget.alpha = 1.0;
}
function onMouseClick(e:MouseEvent):void{
var loader:Loader = new Loader();
loader.load(new URLRequest("img/" + e.currentTarget.name + ".jpg"));
gal.addChild(loader);
txtTitle.text = xmlFile.photo.name[int(e.currentTarget.name) - 1];
txtDesc.text = xmlFile.photo.desc[int(e.currentTarget.name) - 1];
}
Any help is appreciated.
The problem is within one of those two lines:
t.addChild(tloader);
or
gal.addChild(loader);
And the reason for this is because of addChild is not a function error message. If you were calling addChild on non-existing object, the error would be something else.
t is of type thumbs - var t:thumbs = new thumbs(); and gal is gallery - var gal:gallery = new gallery();
So either one of those classes (thumbs or gallery) is problematic. They should do one of the following (inside the classes):
extend DisplayObjectContainer - Sprite or even MovieClip are safest
provide addChild kind of function

action script 3.0 import fla with xml

i want import fla movie from xml. i can't make it. Thank for help.
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.engine.TabAlignment;
import flash.net.URLRequest;
import flash.media.Sound;
//i create timer for sound.
var tmr:Timer=new Timer(1000,1);
tmr.addEventListener(TimerEvent.TIMER, sesiBaslat);
function sesiBaslat(evt:TimerEvent){
var yol:URLRequest=new URLRequest("../../../sound/aaa/1/1/1.mp3");
var ses:Sound=new Sound();
ses.load(yol);
ses.play();
}
tmr.start();
//i was use this code in old times
/*var vid:Video = new Video(1600, 910);
flvPlaceHolder.addChild(vid);
addChild(flvPlaceHolder);
flvPlaceHolder.x = stage.stageWidth/2-vid.width/2;
flvPlaceHolder.y = stage.stageHeight/2-vid.height/2;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("fla/683-bak.flv");
*/
//now i want to write this format now,because i want to make it dynamic
var veriler:XML;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, XMLokundu);
loader.load(new URLRequest("../../../xml/aaa/1/1/1.xml"));
function XMLokundu(e:Event):void{
veriler= new XML(e.target.data);
var loader1:Loader = new Loader();
loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loader1.load(new URLRequest(veriler.animasyonlar.animasyon[1].yolu));
//This blog was made a import sound,image etc. but doesnt work from fla
function imageLoaded(e:Event):void {
var flvPlaceHolder:MovieClip = new MovieClip();
var vid:Video = new Video();
e.target.content.smoothing = true;
addChild(loader1);
loader1.x = veriler.animasyonlar.animasyon[1].xkor;
loader1.y = veriler.animasyonlar.animasyon[1].ykor;
loader1.width = veriler.animasyonlar.animasyon[1].width;
loader1.height = veriler.animasyonlar.animasyon[1].height;
}
}
/*
//XML FİLE
//This is my xml file
<animasyonlar>
<animasyon>
<first>sound/16/1153.mp3</first>
<ikincises>sound/16/1154.mp3</ikincises>
<ucuncuses>sound/16/1151.mp3</ucuncuses>
<dorduncuses>sound/16/1152.mp3</dorduncuses>
<yolu>../../../../bbb/7/fla/683-look.flv</yolu> 1.fla way
<xkor>800</xkor> 2.x value
<ykor>100</ykor> 3.y value
<width>50%</width>
<height>50%</height>
</animasyon>
</animsyonlar>
Your XML is not formatted properly because your closing tag is missing the 2nd "a".
<animasyonlar>
...
</animsyonlar>

AS3 Video Playback with FLVPlayback

I am using the code below to load some data from an .xml file.
I am preloading all data (Audio Paths, Video Paths including a video from xml.
When everything is loaded complete i am loading the video in Frame 2 on FLVPlayback 2.5 with this code:
videoPlayer.source = videofile;
The problem is that the video shows a white screen for 3-4 seconds and then starts play.
At some other pc's it plays normaly when the loading ends.
My Code:
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
stop();
//******************************************************
// XML Loader
//******************************************************
var myLoader:URLLoader = new URLLoader();
//myLoader.load(new URLRequest("myxml.php"));
myLoader.load(new URLRequest("myxml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void{
var myXml:XML = new XML(e.target.data);
parseXML(myXml);
}
//******************************************************
// Extract XML value and fill up variables
//******************************************************
var thename:XML;
var soundpath:XML;
var theage:XML;
var theplace:XML;
var everyday:XML;
var youwill:XML;
var pic1:XML;
var pic2:XML;
var pic3:XML;
var videofile:XML;
var assetsList:Array;
//-----------------------------
var sound:Sound;
var soundChannel:SoundChannel;
//-----------------------------
function parseXML(xml:XML):void{
thename = xml.paths.thename[0];
soundpath = xml.paths.soundpath[0];
theage = xml.paths.theage[0];
theplace = xml.paths.theplace[0];
everyday = xml.paths.everyday[0];
youwill = xml.paths.youwill[0];
pic1 = xml.paths.pic1[0];
pic2 = xml.paths.pic2[0];
pic3 = xml.paths.pic3[0];
videofile = xml.paths.videofile[0];
txtThename.text = thename;
txtSoundpath.text = soundpath;
txtTheage.text = theage;
txtTheplace.text = theplace;
txtEveryday.text = everyday;
txtYouwill.text = youwill;
txtPic1.text = pic1;
txtPic2.text = pic2;
txtPic3.text = pic3;
txtVideofile.text = videofile;
assetsList = [soundpath,theage,theplace,everyday,youwill,pic1,pic2,pic3,videofile];
preloadAssets();
}
//******************************************************
// preloaded assets
//******************************************************
var assetsLoader:URLLoader
var assetsCtr:Number=0;
function preloadAssets():void{
assetsLoader = new URLLoader ();
var urlRequest:URLRequest = new URLRequest(assetsList[assetsCtr]);
assetsLoader.load(urlRequest);
assetsLoader.addEventListener(Event.COMPLETE, assetLoadedHanlder);
assetsLoader.addEventListener(ProgressEvent.PROGRESS, assetProgressHandler);
}
function assetProgressHandler(evt:ProgressEvent):void{
var bl:uint = evt.bytesLoaded;
var bt:uint = evt.bytesTotal;
var perEachAssets = 1/assetsList.length;
var assetsBlLoaded = ((bl / bt)*perEachAssets)+((assetsCtr)/assetsList.length*100)/100;
var _percentLoaded = Math.floor(assetsBlLoaded*100);
progBar.setProgress(_percentLoaded,100)
//trace("_percentLoaded:",_percentLoaded)
}
function assetLoadedHanlder(evt:Event):void{
assetsCtr+=1;
if(assetsCtr<assetsList.length){
//trace("preloading:"+assetsList[assetsCtr])
var urlRequest:URLRequest = new URLRequest(assetsList[assetsCtr]);
assetsLoader.load(urlRequest);
}else{
//trace("done!")
assetsLoader.removeEventListener(Event.COMPLETE, assetLoadedHanlder);
assetsLoader.removeEventListener(ProgressEvent.PROGRESS, assetProgressHandler);
gotoAndStop(2);
}
}
Rather than adding the component directly to the stage you might want to try creating and adding it with ActionScript.
By doing this you can instantiate the FLVPlayback instance before you need to show it, rather than having to wait until you hit frame 2 on your timeline.
I can't guarantee it will fix your problem but it's worth a go.
var _videoFLV:FLVPlayback;
_videoFLV = new FLVPlayback();
_videoFLV.fullScreenTakeOver = false;
_videoFLV.autoPlay = false;
_videoFLV.autoRewind = true;
_videoFLV.isLive = false;
_videoFLV.skin = null;
_videoFLV.bufferTime = .1;
_videoFLV.width = 320;
_videoFLV.height = 240;
_videoFLV.source = videofile;
_videoFLV.stop();
_videoFLV.x = 240;
_videoFLV.y = 240;
addChild(_videoFLV);

Actionscript 3 - DataGridColumn and having a specific column automatically sorted

I am using the DataGrid component in Flash, that is loaded with data via a external XML file. I have a column, A (Serial), which once loaded, I'd like for the information to be sorted Ascendingly, automatically. Does anyone have any idea on how to do this?
Here is my code:
import fl.controls.DataGrid;
import fl.data.DataProvider;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.ScrollPolicy;
import fl.events.DataGridEvent;
var dp:DataProvider;
var A:DataGridColumn = new DataGridColumn("Serial");
A.headerText = "Serial No.";
A.width = 100;
A.resizable = false;
var B:DataGridColumn = new DataGridColumn("Mold");
B.headerText = "Mold No.";
B.width = 150;
B.resizable = false;
var C:DataGridColumn = new DataGridColumn("Type");
C.headerText = "Grid Type: ";
C.width = 350;
C.resizable = false;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(A);
myDataGrid.addColumn(B);
myDataGrid.addColumn(C);
myDataGrid.verticalScrollPolicy = ScrollPolicy.ON;
myDataGrid.setSize(600, 800);
myDataGrid.move(0, 0);
myDataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, headerReleaseHandler);
addChild(myDataGrid);
var url:String = "xml/TEST.xml";
var req:URLRequest = new URLRequest(url);
var uLdr:URLLoader = new URLLoader();
uLdr.addEventListener(Event.COMPLETE, completeHandler);
uLdr.load(req);
function completeHandler(event:Event):void {
var ldr:URLLoader = event.currentTarget as URLLoader;
var xmlDP:XML = new XML(ldr.data);
dp = new DataProvider(xmlDP);
myDataGrid.dataProvider = dp;
}
function headerReleaseHandler(event:DataGridEvent):void {
var dg:DataGrid = event.currentTarget as DataGrid;
trace("column: " + String(event.dataField));
trace("descending: " + String(dg.sortDescending));
}
Thanx
Try the sortItemsOn() method on the DataGrid.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/SelectableList.html#sortItemsOn() for an example how to use it.