Mix and playback multiple tracks Actionscript 3 - actionscript-3

I am creating a sound mixer in AS3. I have several sound samples that a user can choose from to create their own track. The mixer will "mixdown" a max of 4 tracks and playback the "mixed" track on demand. I'm having trouble getting the sounds to playback in sync. I'm new to working with sound in flash and wondering what I'm missing... Here is the "mix" function at present:
function mixIt(e:MouseEvent) {
if (isPlaying) {
channel1.stop();
}
var mChannel1:SoundChannel = new SoundChannel;
var mChannel2:SoundChannel = new SoundChannel;
var tUrl1:URLRequest = new URLRequest(trackChoice1);
var tUrl2:URLRequest = new URLRequest(trackChoice2);
var s1:Sound = new Sound();
var s2:Sound = new Sound();
var s1Done:Boolean = false;
var s2Done:Boolean = false;
s1.addEventListener(Event.COMPLETE, onSoundLoaded1);
s2.addEventListener(Event.COMPLETE, onSoundLoaded2);
s1.load(tUrl1);
s2.load(tUrl2);
function onSoundLoaded1(e:Event) {
s1Done = true;
if (s2Done) {
playMix()
}
}
function onSoundLoaded2(e:Event) {
s2Done = true;
if (s1Done) {
trace("s2 Done")
playMix()
}
}
function playMix(){
mChannel1 = s1.play(0, 2);
mChannel2 = s2.play(0, 2);
//trace("MIXING TRACKS :" + tUrl1 + " + " + tUrl2);
}
}

try mixing sounds as byte arrays using Sound.extract() and SampleDataEvent

Related

Actionscript 3 Playing videos back to back with in a sequence with no break inbetween

I'm wondering at the possiblitity of setting up an array of videos that play back to play with absolutely no pause in between? Is there anyway to set it up that there is no way to to have any buffering or pause in between sequential playback? For example:
import flash.events.NetStatusEvent;
var videos:Array = new Array("Ad01.flv", "Ad02.flv", "Ad03.flv");
var currentVideo:uint = 0;
var duration:uint = 0;
var ready:Boolean = true;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
myVideo.attachNetStream(ns);
ns.play(videos[currentVideo]);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {
duration = evt.duration;
ready = true;
};
ns.client = listener;
ns.addEventListener(NetStatusEvent.NET_STATUS,nsHandler );
function nsHandler(evt:NetStatusEvent):void {
if (ready && ns.time > 0 && ns.time >= (duration - 0.5)) {
ready = false;
currentVideo++;
if (currentVideo < videos.length) {
ns.play(videos[currentVideo]);
} else {
ns.removeEventListener(NetStatusEvent.NET_STATUS, nsHandler
);
}
}
};
I'd like to break up one video into multiple parts and have it play seamlessly back. I've tested the above but I need to know definitively.
One way you could do it is to embed the videos to the timeline of separate swfs, and then subload the swfs, and play each timeline when needed after all the loads are complete.

AS3 Play & Pause Current (loaded) MP3 (Flash CC)

I have 8 external mp3s that are loaded and will be played sequencially. I cannot figure how to pause the mp3 currently playing and start playing from the point it was paused. I did have code that would pause it but when clicking the play button, it would play the first mp3 from the beginning.
var s1:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var s2:Sound = new Sound(new URLRequest("Audio Files/2.mp3"));
var s3:Sound = new Sound(new URLRequest("Audio Files/3.mp3"));
var s4:Sound = new Sound(new URLRequest("Audio Files/4.mp3"));
var s5:Sound = new Sound(new URLRequest("Audio Files/5.mp3"));
var s6:Sound = new Sound(new URLRequest("Audio Files/6.mp3"));
var s7:Sound = new Sound(new URLRequest("Audio Files/7.mp3"));
var s8:Sound = new Sound(new URLRequest("Audio Files/8.mp3"));
s1.addEventListener(Event.COMPLETE, doLoadComplete);
s2.addEventListener(Event.COMPLETE, doLoadComplete);
s3.addEventListener(Event.COMPLETE, doLoadComplete);
s4.addEventListener(Event.COMPLETE, doLoadComplete);
s5.addEventListener(Event.COMPLETE, doLoadComplete);
s6.addEventListener(Event.COMPLETE, doLoadComplete);
s7.addEventListener(Event.COMPLETE, doLoadComplete);
s8.addEventListener(Event.COMPLETE, doLoadComplete);
var channel:SoundChannel = new SoundChannel();
channel = s1.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
function doLoadComplete($evt:Event):void
{
trace("Song loaded.");
}
function doSoundComplete($evt:Event):void
{
trace("1 done.");
channel = s2.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete2)
}
function doSoundComplete2($evt:Event):void
{
trace("2 done.");
channel = s3.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete3);
}`
Here is what I have so far:
This loads the mp3s and plays them. The pause btn works but the play button to resume the audio gives me an error : ReferenceError: Error #1069: Property 0 not found on flash.media.Sound and there is no default value. at mp3sequence_fla::MainTimeline/playSound()
My guess is that the value for the current position or last position is incorrect.
var myArray:Array=[0,1,2,3,4,5,6,7];
var i:uint=1;
var req:URLRequest = new URLRequest("mp3/"+myArray[i]+".mp3");
var VSound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; //last position of the sound
var curSoundIndex:int = 0; //var to store the current sound that is playing
VSound.load(req);
channel = VSound.play();
function playSound(e:Event = null) {
//if no sound channel, load the current sound into it
channel = VSound[curSoundIndex].play(lastPosition);
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true);
lastPosition = channel.position;
}
function pauseSound(e:Event = null) {
if (channel) {
lastPosition = channel.position;
channel.stop();
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= VSound.length) curSoundIndex = 0;
}
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
To pause a sound you can store it's position when you pause it, and use that variable to replay from that position.
var s:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var channel:SoundChannel = new SoundChannel();
channel = s.play();
var pausePosition:int;
function pause():void {
soundPosition = channel.position;
channel.stop();
}
function resume():void {
channel = s.play(soundPosition);
}
You can store the position property of the corresponding SoundChannel. I've added some code to make the whole thing less redundant
var curSoundIndex:int = 0; //var to store the current sound that is playing
var lastPosition:Number = 0; //last position of the sound
var soundChannel:SoundChannel;
var sounds:Vector.<Sound> = new Vector.<Sound>(); //array of all sounds
loadNextSound();
function loadNextSound(e:Event = null):void {
//check if all sounds are loaded
if (sounds.length >= 8) {
curSoundIndex = 0; //select first sound
resume(); //start playing
return;
}
//if not, load the next sound and add it to the array/vector
var sound:Sound = new Sound(new URLRequest("Audio Files/" + (sounds.length + 1) + ".mp3"));
sounds.push(sound);
if(sound.bytesLoaded < sound.bytesTotal){ //check if already loaded
sound.addEventListener(Event.COMPLETE, loadNextSound);
}else{
loadNextSound();
}
}
function pause(e:Event = null) {
if (soundChannel) {
lastPosition = soundChannel.position;
soundChannel.stop();
}
}
function resume(e:Event = null) {
//if no sound channel, load the current sound into it
soundChannel = sounds[curSoundIndex].play(lastPosition);
soundChannel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true); //use weak listener to avoid memory leaks
lastPosition = 0;
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= sounds.length) curSoundIndex = 0;
}

AS3 - my sound player seems to work well but the sound is not there

I've been doing a soundplayer for my assignment.Came across some issues. No compile error neither runtime error is appearing and my sound player functions well but I can't hear the song playing.
var mySound:Sound = new Sound ();
var myURL:URLRequest = new URLRequest ("playList.xml");
var mySoundChannel:SoundChannel = new SoundChannel;
var playing:Boolean = true;
var resumeTime:Number = 0;
var loadedXML:XML;
var mySongs:XMLList;
var myTotal:Number;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(myURL);
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML (e:Event):void {
loadedXML = XML(e.target.data);
mySongs = loadedXML.SONG;
myTotal = mySongs.length();
}
btnPlayPause.buttonMode =true;
btnStop.buttonMode =true;
btnPlayPause.addEventListener(MouseEvent.CLICK, playSound);
btnStop.addEventListener(MouseEvent.CLICK, stopSound);
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
function stopSound (f:MouseEvent):void {
mySoundChannel.stop();
}
Help is appreciated very much.
It seems like you trying to play sound only if it already playing, while stoping sound if it is NOT playing:
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
I see this code like this:
function playSound(){
if(playing){
mySound.play();
} else {
mySoundChannel.stop();
}
}
Maybe this is the root of problem

Movie clips not on the scene automatically playing, when the scene loads

I'm developing an app for android using flash and adobe air for android.
In flash i have a set of Scenes which can be navigated in the following way.
in the lower scenes (morning, noon and evening scenes), i have one movie clip every 5 frames per scene, which have audio embedded in them. on the first frame of the movie clips i have, i call the stop(); function. and at the end of the clip an event is dispatched ( dispatchEvent(new Event ("MEANING_TEXT_COMPLETE")); ), so that code from the main timeline could act accordingly.
Here is the code on my main timeline
import flash.events.MouseEvent;
MovieClip(this.root).stop();
btn_home_parent.addEventListener(MouseEvent.CLICK, goto_dh_morn);
mc_sound.addEventListener(MouseEvent.CLICK, control_sound_morn);
btn_next_morn.addEventListener(MouseEvent.CLICK, goto_next_morn);
btn_prev_morn.addEventListener(MouseEvent.CLICK, goto_prev_morn);
//load all sound files
var mn_1:Mn1 = new Mn1();
var mn_2:Mn2 = new Mn2();
var mn_3:Mn3 = new Mn3();
var mn_4:Mn4 = new Mn4();
var mn_5:Mn5 = new Mn5();
var mn_6:Mn6 = new Mn6();
var mn_7:Mn7 = new Mn7();
var mn_8:Mn8 = new Mn8_();
var mn_9:Mn9 = new Mn9();
var mn_10:Mn10 = new Mn10();
var mn_11:Mn11 = new Mn11();
var mn_12:Mn12 = new Mn12();
var mn_13:Mn13 = new Mn13();
var mn_14:Mn14 = new Mn14();
var frameCount_morn:int = 1;
var lang1Init_morn:Boolean = false;
var lang1:Object;
var ar:Object;
playSound_morn();
function playSound_morn()
{
var currentSound:Sound = new Sound();
channel.stop();
trace("play sound");
currentSound = this["mn_" + frameCount_morn];
channel = currentSound.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundComplete_morn);
trans.volume = 1;
channel.soundTransform = trans;
}
function soundComplete_morn(e:Event):void
{
if (showlang1Text)
{
trace("ar Sound Complete");
ar = getChildByName("arText" + (currentFrame - 1));
ar.visible = false;
trace("ar child acquired");
lang1 = getChildByName("lang1Text" + (currentFrame - 1));
lang1Init_morn = true;
lang1.visible = true;
lang1.play();
lang1.addEventListener("MEANING_TEXT_COMPLETE", translationComplete_morn);
}
}
function translationComplete_morn(e:Event)
{
trace("translate Complete");
ar= getChildByName("arText" + (currentFrame - 1));
ar.visible = true;
lang1 = getChildByName("lang1Text" + (currentFrame - 1));
lang1.visible = false;
}
function goto_next_morn(e:MouseEvent)
{
if (MovieClip(this.root).currentFrame <= 61)
{
var next_frame = MovieClip(this.root).currentFrame + 5;
frameCount_morn = frameCount_morn + 1;
channel.stop();
if (lang1Init_morn){lang1.stop();lang1Init_morn = false;}
MovieClip(this.root).gotoAndStop(next_frame);
playSound_morn();
}
trace("Current Frame: " + currentFrame);
}
function goto_prev_morn(e:MouseEvent)
{
if (MovieClip(this.root).currentFrame > 0)
{
var prev_frame = MovieClip(this.root).currentFrame - 5;
frameCount_morn = frameCount_morn - 1;
channel.stop();
if (lang1Init_morn){lang1.stop();lang1Init_morn = false;}
MovieClip(this.root).gotoAndStop(prev_frame);
playSound_morn();
}
trace("Current Frame: " + currentFrame);
}
function control_sound_morn(e:MouseEvent)
{
control_sound(e);
if (!showlang1Text) {
mc_sound.gotoAndStop(5);
if (lang1Init_morn){
lang1.stop();
lang1Init_morn = false;
ar.visible = true;
lang1.visible = false;
}
}
else {
mc_sound.gotoAndStop(1)
}
}
function goto_dh_morn(e:MouseEvent) {
if (lang1Init_morn){lang1.stop();lang1Init_morn = false;}
channel.stop();
goto_dh(e);
}
the movie clip object is lang1
when i load the scene "Morning Scene" it works perfectly well, as it should. however when i navigate to the "noon scene" or "evening scene", audio from all the movie clips that were put on the "morning scene" automatically starts to play. The funny thing is that those movie clips are not even added to the "noon scene" and "evening scene". Any idea on why this happens or how to fix it.
thanks
as I got no answers. the way I fixed it was to compile each scene to a separate swf file and then load/unload them when needed.

How do I know the length of the video (external FLV video)?

How do I know the length of the video (external FLV video)?
I have tried several ways but the result is 0.
videonya.addEventListener(fl.video.VideoEvent.READY, onFlvPlayback_READY);
function onFlvPlayback_READY(event:fl.video.VideoEvent):void
{
var metaDataObj:Object = videonya.metadata as Object;
trace("metaDataObj.duration: "+metaDataObj.duration);
}
The standard way of playing video and accessing metadata is this:
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = this;
ns.play("video.flv");
var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);
function onMetaData(infoObject:Object):void
{
var key:String;
for (key in infoObject)
{
trace(key + ": " + infoObject[key]);
}
}
This will trace out all of the metadata codes including duration. If you just want the duration: trace(infoObject.duration); inside the onMetaData(infoObject) function, of course.