How to add 2 volume sliders to my mp3 players in as3 - actionscript-3

Easy people. Im currently trying to add 2 volume sliders to my existing project. I have two turntables, deck1 & deck2 but i need to be able to control the volume of each deck individually.
Im relatively new to actionscript 3 so im struggling abit. Can anyone help me please, here is my code..
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.text.TextField;
//variables
var mySound:Sound;
var myChannel:SoundChannel;
var nowPlaying:Boolean = false;
var nowPaused:Boolean = false;
var p:uint = 0;
var songfile:String;
var songtitle:String;
deck1_btn.addEventListener(MouseEvent.CLICK, deck1_data);
deck2_btn.addEventListener(MouseEvent.CLICK, deck2_data);
//song3_btn.addEventListener(MouseEvent.CLICK, song3_data);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
stop2_btn.addEventListener(MouseEvent.CLICK, stopSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function deck1_data(myEvent:MouseEvent):void {
songfile = "audio/desire.mp3";
songtitle = "Skeptical - Desire";
playSound(null);
}
function deck2_data(myEvent:MouseEvent):void {
songfile = "audio/tundra.mp3";
songtitle = "Skeptical - Tundra";
playSound(null);
}
//function song3_data(myEvent:MouseEvent):void {
songfile = "audio/always_be_mine.mp3";
songtitle = "Skeptical - Always Be Mine";
playSound(null);
function stopSound(myEvent:MouseEvent):void {
if (nowPlaying) {
myChannel.stop();
p = 0;
nowPlaying = false;
nowPaused = false;
}
}
function playSound(myEvent:Event):void {
mySound = new Sound;
mySound.load(new URLRequest(songfile));
title_txt.text = songtitle;
if (isPlaying) {
myChannel.stop();
myChannel = mySound.play(0);
} else {
myChannel = mySound.play(0);
nowPlaying = true;
}
}
function pauseSound(myEvent:MouseEvent):void {
if (isPlaying) {
p = Math.floor(myChannel.position);
myChannel.stop();
nowPlaying = false;
nowPaused = true;
} else if (nowPaused) {
myChannel = mySound.play(p);
nowPlaying = true;
nowPaused = false;
}
}
title_txt.text = "";

For controlling volume you will need SoundTransform. Create sliders, and store volumes as numbers. And when you start deck sound, apply soundTransform with deck volume value:
myChannel = mySound.play(0, 0, new SoundTransform(myDeck1Volume));
//or
//myChannel = mySound.play(0, 0, new SoundTransform(myDeck2Volume));
As for sliders. You could use Slider component

Related

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.

AS3 BitmapData Collision

I am having a problem with BitmapData Collision detection, my class is not detecting any collision at all
BitmapCollision.as
package com.ipromoweb.demoapp
{
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
/**
* ...
* #author Me
*/
public class BitmapCollision {
private var collides:Boolean = false;
public function check(obj1:DisplayObjectContainer, obj2:DisplayObjectContainer):Boolean {
// obj1
var obj1Rect:Rectangle = obj1.getBounds(obj1);
var obj1Offset:Matrix = obj1.transform.matrix;
obj1Offset.tx = obj1.x - obj1Rect.x;
obj1Offset.ty = obj1.y - obj1Rect.y;
var obj1ClipBmpData:BitmapData = new BitmapData(obj1Rect.width, obj1Rect.height, true, 0);
obj1ClipBmpData.draw(obj1, obj1Offset);
// obj2
var obj2Rect:Rectangle = obj2.getBounds(obj2);
var obj2Offset:Matrix = obj2.transform.matrix;
obj2Offset.tx = obj2.x - obj2Rect.x;
obj2Offset.ty = obj2.y - obj2Rect.y;
var obj2ClipBmpData:BitmapData = new BitmapData(obj2Rect.width, obj2Rect.height, true, 0);
obj2ClipBmpData.draw(obj2, obj2Offset);
var rLoc:Point = new Point(obj2Rect.x, obj2Rect.y);
var bLoc:Point = new Point(obj1Rect.x, obj1Rect.y);
if (obj1ClipBmpData.hitTest(bLoc, 255, obj2ClipBmpData, rLoc, 255)) {
trace("hit");
collides = true;
}else {
collides = false;
}
obj1ClipBmpData.dispose();
obj2ClipBmpData.dispose();
return collides;
}
}
}
And i am calling checkCollision function on a ENTER_FRAME event, the hitTestObject is firing well but the bitmapData Collision is not firing at all.
private function checkCollision():void {
for (var i:int = 0; i < parent.numChildren; i++) {
if (Object(parent.getChildAt(i)).constructor == '[class CollisionTest]') {
if (this.hitTestObject(parent.getChildAt(i))) {
trace('colliding');
if ( _collision.check(Sprite(this),Sprite(parent.getChildAt(i))) ) {
trace('>>>>>>>>>>> perfect hit');
}
}
}
}
}
Any help on this would be greatly appreciated.
I discarded the previous method of BitmapData Collision that i was trying to implement and downloaded a free library called CDK:
https://code.google.com/p/collisiondetectionkit/
it solved all my problems.

Changing the UrlLoader.load

package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.*
import flash.net.*
public class SpeechBox extends MovieClip{
public var textLoader:URLLoader = new URLLoader();
public var box:Sprite = new Sprite();
public var nextBox:Sprite = new Sprite();
private var nextText:TextField = new TextField();
private var textBox:TextField = new TextField();
private var speechText:String;
public var _speechBoxCheck:Timer = new Timer(1000);
public var clickedNext:Boolean = false;
public function SpeechBox()
{
textLoader.addEventListener(Event.COMPLETE, onLoaded);
textBox.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownScroll);
_speechBoxCheck.addEventListener(TimerEvent.TIMER, speechBoxCheck);
_speechBoxCheck.start();
//////////////////SPEECH BOX///////////////////
box.graphics.lineStyle(3.5,0xffffff);
box.graphics.beginFill(0x003366, .35);
box.graphics.drawRoundRect(0,0,650,145,20);
box.graphics.endFill();
box.x = 100;
box.y = 450;
addChild(box);
//////////////////SPEECH TEXT///////////////////
var speechFont = new DataText();
var textFormat:TextFormat = new TextFormat();
textFormat.font = speechFont.fontName;
textFormat.align = TextFormatAlign.LEFT;
textFormat.leading = 3;
textFormat.color = 0xFFFFFF;
textFormat.size = 16;
textBox.defaultTextFormat = textFormat;
textBox.width = 620;
textBox.height = 115;
textBox.x = box.x + 14;
textBox.y = box.y + 14;
textBox.multiline = true;
textBox.wordWrap = true;
textBox.selectable = false;
addChild(textBox);
//////////////////NEXT BUTTON///////////////////
nextBox.graphics.beginFill(0x000000, 0);
nextBox.graphics.drawRect(0,0,50,30);
nextBox.graphics.endFill();
nextBox.x = box.x + 600;
nextBox.y = box.y + 115;
nextText.defaultTextFormat = textFormat;
nextText.text = "Next";
nextText.textColor = 0xffffff;
nextText.autoSize = "left";
nextText.selectable = false;
nextText.mouseEnabled = false;
nextText.x = nextBox.x + 2
nextText.y = nextBox.y + 5
nextBox.buttonMode = true;
//nextBox.mouseEnabled = true;
nextBox.addEventListener(MouseEvent.MOUSE_DOWN, clickNext);
nextBox.addEventListener(MouseEvent.MOUSE_OVER, moveOver);
nextBox.addEventListener(MouseEvent.MOUSE_OUT, moveOut);
}
function onLoaded(e:Event):void {
trace(e.target.data);
textBox.text = e.target.data;
}
function mouseDownScroll(event:MouseEvent):void
{
textBox.scrollV+=4;
textBox.addEventListener(MouseEvent.MOUSE_UP,mouseup);
}
function mouseup(event:MouseEvent):void
{
if(textBox.scrollV == textBox.maxScrollV)
{
addChild(nextBox);
addChild(nextText);
}
}
function clickNext(event:MouseEvent):void
{
trace("click");
clickedNext = true;
_speechBoxCheck.stop();
(parent as Main).onTransition.start();
textBox.scrollV = 0;
textLoader.removeEventListener(Event.COMPLETE, onLoaded);
this.parent.removeChild(this);
}
function moveOver(event:MouseEvent):void
{
nextText.textColor = 0xffcc00;
}
function moveOut(event:MouseEvent):void
{
nextText.textColor = 0xffffff;
}
///////////////////////////////////////////////////////////////
function speechBoxCheck(event:TimerEvent)
{
if ((parent as Main).introduction == true)
{
textLoader.load(new URLRequest("Texts/LV1introduction.txt"));
trace("beginning");
(parent as Main).onTransition.stop();
}
if ((parent as Main).levelNum == 1)
{
textLoader.load(new URLRequest("Texts/LV1complete.txt"));
trace("go to lv 2")
(parent as Main).onTransition.stop();
}
if ((parent as Main).levelNum == 2)
{
textLoader.load(new URLRequest("Texts/LV2complete.txt"));
trace("go to lv 3")
(parent as Main).onTransition.stop();
}
}
}
}
EDIT: When the game starts, the LV1 introduction text starts. Once the scrollV equals maxScrollV, a next buttons appears. Click that, it will delete itself and the game starts. Once you beat stage one, levelNum automatically equals 2 and I add this class again from my main document class. However, it will show the same text over and over, regardless of what level.
So does the urlLoader always stay the same? If so, how can I change it?
URLLoader can be reused to load another data with new URLRequest instance. It is completely OK to reuse same URLLoader instance to load another file. Your problem is not in URLLoader, but in logics or somewhere else. You'd better try debuggers to make sure variable level has correct value of 2.
Why are you loading text file EVERY second? It would be ok to load them only level is changed.
does this textLoader instance have event listeners attached?
:::::::::EDITED:::::::::
You are removing event Listeners from textLoader in clickNext() call. textLoader will load file, but will not run onLoaded() to update textBox.text
Your speechBoxCheck() method is doing it wrong. 1. You must make only 1 load() call in speechBox() method. Your conditionals in the method are not exclusive, and it may cause trouble when multiple load() calls are made (previous loading operation will be canceled). consider "else if" chain.
it is not recommended to do something like loading files in this fashion. Unnecessary I/O operations, especially in runtime, should be avoided. Only load files when it is needed; In this case, it is when level changes.

actionscript 3 synchronous loader

I have typical situation where big loop is loading lots of images and its done asynchronous which make browser to frees during loading and I want to make it synchronous but having big trouble doing it. I found this class synchronous loader and it work great but you cant add mouse event listener to loader. Here is sample code:
for (var i = 0; i < items.length; i++) {
item = items[i];
if(item.layer>1){
ld:Loader = new Loader();
ld.load(new URLRequest(item.url));
ld.rotation = item.rotation;
ld.x = item.x ;
ld.y = item.y;
ld.addEventListener(Event.COMPLETE, loadComplete);
ld.scaleX = item.scaleX;
ld.scaleY = item.scaleY;
ld.addEventListener(MouseEvent.MOUSE_DOWN, select);
layers_arr[item.layer].addChild(ld);
}
}
Any idea how this can be done?
like arieljake says, here is a little example on how to use it:
package
{
import com.greensock.TweenLite;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.ImageLoader;
import com.greensock.loading.LoaderMax;
import com.greensock.loading.MP3Loader;
import com.greensock.loading.SWFLoader;
import com.greensock.loading.XMLLoader;
import com.greensock.loading.display.ContentDisplay;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Main extends Sprite
{
public var itemUrl:String;
public var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
queue.maxConnections = 1; //Checks how much items that can be loaded at the same time
queue.append( new ImageLoader("http://www.myurl.com/myimage.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0,scaleMode:"proportionalInside"}) );
queue.append( new ImageLoader("http://www.myotherurl.com/awesomeimage.jpg", {name:"photo2", estimatedBytes:2400, container:this, alpha:0, scaleMode:"proportionalInside"}) );
queue.addEventListener(LoaderEvent.CHILD_COMPLETE, childCompleteHandler); //checks when a child has completed to load
queue.addEventListener(LoaderEvent.CHILD_PROGRESS, childProgressHandler); //checks the child progress
//prioritize the loader named "photo1"
LoaderMax.prioritize("photo1"); //same as LoaderMax.getLoader("photo1").prioritize();
//start loading
queue.load();
}
protected function childProgressHandler(event:LoaderEvent):void
{
var procent:Number = Math.floor(event.target.progress*100);
var targetName:String = event.target.name;
trace(procent+'% loaded of item: '+targetName);
}
protected function childCompleteHandler(event:LoaderEvent):void
{
var targetName:String = event.target.name;
trace(targetName+' is loaded!');
}
private function completeHandler(event:LoaderEvent):void {
var objects:Array = event.currentTarget.content;
for(var i:uint=0; i < objects.length; i++)
{
var image:ContentDisplay = LoaderMax.getContent(objects[i].name);
TweenLite.to(image, 1, {alpha:1, y:100});
}
trace(event.target + " is complete!");
}
private function errorHandler(event:LoaderEvent):void {
trace("error occured with " + event.target + ": " + event.text);
}
}
}
Check this out: http://www.greensock.com/loadermax/
Let's you specify max number of simultaneous loaders

Upload picture directly to the server

In the following link http://www.tuttoaster.com/create-a-camera-application-in-flash-using-actionscript-3/ how to make the picture upload directly to the server after taking a picture from webcam
package
{
import flash.display.Sprite;
import flash.media.Camera;
import flash.media.Video;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.utils.ByteArray;
import com.adobe.images.JPGEncoder;
public class caml extends Sprite
{
private var camera:Camera = Camera.getCamera();
private var video:Video = new Video();
private var bmd:BitmapData = new BitmapData(320,240);
private var bmp:Bitmap;
private var fileReference:FileReference = new FileReference();
private var byteArray:ByteArray;
private var jpg:JPGEncoder = new JPGEncoder();
public function caml()
{
saveButton.visible = false;
discardButton.visible = false;
saveButton.addEventListener(MouseEvent.MOUSE_UP, saveImage);
discardButton.addEventListener(MouseEvent.MOUSE_UP, discard);
capture.addEventListener(MouseEvent.MOUSE_UP, captureImage);
if (camera != null)
{
video.smoothing = true;
video.attachCamera(camera);
video.x = 140;
video.y = 40;
addChild(video);
}
else
{
trace("No Camera Detected");
}
}
private function captureImage(e:MouseEvent):void
{
bmd.draw(video);
bmp = new Bitmap(bmd);
bmp.x = 140;
bmp.y = 40;
addChild(bmp);
capture.visible = false;
saveButton.visible = true;
discardButton.visible = true;
}
private function saveImage(e:MouseEvent):void
{
byteArray = jpg.encode(bmd);
fileReference.save(byteArray, "Image.jpg");
removeChild(bmp);
saveButton.visible = false;
discardButton.visible = false;
capture.visible = true;
}
private function discard(e:MouseEvent):void
{
removeChild(bmp);
saveButton.visible = false;
discardButton.visible = false;
capture.visible = true;
}
}
}
The FileReference.upload() and FileReference.download() functions are nonblocking. These functions return after they are called, before the file transmission is complete. In addition, if the FileReference object goes out of scope, any upload or download that has not yet been completed on that object is cancelled upon leaving the scope. So, be sure that your FileReference object will remain in scope for as long as the upload or download could be expected to continue. http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001063.html