How to off sound background when video played di as3 - actionscript-3

I have a sound background in my application. This is the code in frame 1
var music:Sound;
var trans:SoundTransform;
var channel:SoundChannel;
var musicOn:Boolean = false;
music=new Sound(new URLRequest("src/lonely.mp3"));
trans=new SoundTransform(1,-1);
channel= music.play(0, 1000, trans);
play_btn.visible = false;
play_btn.addEventListener(MouseEvent.CLICK,onSound);
stop_btn.addEventListener(MouseEvent.CLICK,offSound);
function offSound(e:Event) {
musicOn = false;
play_btn.visible = true;
stop_btn.visible = false;
trans.volume=0;
SoundMixer.soundTransform = trans;
}
function onSound(e:Event) {
musicOn = true;
play_btn.visible = false;
stop_btn.visible = true;
trans.volume=.3;
SoundMixer.soundTransform = trans;
}
In frame 210, I embedded file video. But, when my video played the sound background still playing, and when I stop the sound of background, my sound in video also stop playing. How to stop just background sound without stop the sound of video?
Thank You...

Change the volume of your sound with the soundTransform property of your channel
channel = music.play(0, 1000, trans);
change the volume with:
channel.soundTransform = new SoundTransform('Your volume here', 0);
change 'Your volume here' with 0 or anything from ranges 0-1

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
}

Lower volume of certain audio on frame with multiple audios. AS3

Okay, so, here's the deal, i have a sound steaming from an URL since the first frame (60 total), and i added another sound file (from the library) to a certain frame. As expected, both sounds overlapp, so i decided to make a mute function... the problem is, i cant get it to apply only to the streaming audio, the function mutes all audio, and i cant use the variable mySound because the mute function needs to be on other keyframe to work...
Here's the code of the streaming audio:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;// pause button
mySound.load(new URLRequest("http://trollfacequiz.16mb.com/MusicFiles/Goat%20Songs.mp3"));
//mySound.play();
SoundMixer.stopAll();
myChannel = mySound.play();
btn_mute.btn_pause.addEventListener(MouseEvent.CLICK, onClickPause);
btn_mute.btn_play.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPause(e:MouseEvent):void
{
btn_mute.btn_pause.visible = false;
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void
{
btn_mute.btn_pause.visible = true;
myChannel = mySound.play(lastPosition);
}
And the mute function:
function setVolume(vol){
var volTransform:SoundTransform = new SoundTransform();
volTransform.volume = vol;
SoundMixer.soundTransform = volTransform;
}
setVolume(0);

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.

mute unmute button in actionscript 3

i need some help with my actionscript 3 project. I have a button with a sound in it. I have some code (see below) that when i press the button it plays the sound and if i press the button again it stops the sound (like a mute/unmute button). The problem is that when i press the button to play the sound for the second time it plays two sounds (the same sound twice) and if i press the button to play the sound more times the same sound plays many times. Can you please help me solve the problem? Thank you.
function setMute1(vol){
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
}
var sTransform1:SoundTransform = new SoundTransform(1,0);
var Mute1:Boolean = true;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
function toggleMuteBtn1(event:Event) {
if(Mute1 === false) {
Mute1 = true;
setMute1(0);
} else {
Mute1 = false;
setMute1(1);
}
}
From what I understand you start the sound by assigning it to the buttons hit frame? You need to have the sound started by the code in order to control the sound in good way.
Here is an working example based on your code, that loads an external mp3 file. The sound is played and stopped via the same button.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Start/stop sound
function startOrStop()
{
if (Mute1 === false)
{
myChannel.stop();
setMute1(0);
}
else
{
setMute1(1);
myChannel = mySound.play();
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
startOrStop()
}
In actionscrip 2 there was a function that would stop all sounds, in actionscript 3 you can't do that anymore, but you can still assign sounds to frames.
This example mutes and unmutes the sound. The sound is't stopped, just muted.
Also here the sound must be assigned in the code, and not to the frame.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Toggle mute on/off
function toggleMute()
{
if (Mute1 === false)
{
setMute1(0);
}
else
{
setMute1(1);
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
// if not playing, the sound
if (myChannel.position != 0) {
} else {
myChannel = mySound.play();
}
toggleMute();
}

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