I found this code on the net, and it's almost perfect for my needs - but I need it to continuously loop the clip.
var so: Sound= new Sound();
var audioChannel: SoundChannel= new SoundChannel();
var audioTransform: SoundTransform = new SoundTransform();
var video:Video=new Video(320,240);
var nc:NetConnection=new NetConnection ;
nc.connect(null);
var ns:NetStream=new NetStream(nc);
var meta: Object = new Object();
meta.onMetaData = function(meta:Object){
trace(meta.duration);
};
ns.client=meta;
addChild(video);
video.attachNetStream(ns);
ns.play("vid.flv");
//clip is a transparent clip (alpha=0) for the entire scene
clip.addEventListener(MouseEvent.MOUSE_OVER,mute);
clip.addEventListener(MouseEvent.MOUSE_OUT,unmute);
function mute(e:MouseEvent) {
ns.soundTransform=audioTransform;
audioTransform.volume=0;
}
function unmute(e:MouseEvent) {
ns.soundTransform=audioTransform;
audioTransform.volume=1;
}
To loop a video stream in AS3:
Listen for the streams NET_STATUS event.
//ns is a netstream object
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
In your streamStatus event handler, check if the status code is the video stop event:
function streamStatus(status:NetStatusEvent) {
if (status.info.code == "NetStream.Play.Stop") {
//the video has stopped
}
}
To loop the video, just seek to the beginning.
ns.seek(0);
So this is the final code:
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
ns.play("vid.flv");
function streamStatus(status:NetStatusEvent) {
if (status.info.code == "NetStream.Play.Stop") {
ns.seek(0);
}
}
To see whatever status codes you can check for, see the documentation.
Related
I am trying to to use the ENTER_FRAME Event on playing audio ,but the code is not executing the handler function.
public function play_mp3(path:String,path1:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
addEventListener(Event.ENTER_FRAME,myFunction);}
public function myFunction(e:Event){
LogFB.info('test1234'); }
You're issue is likely that the class whose code you've posted, is not on the display tree. (it's not a display object or hasn't been added to the stage).
If it's not on the display tree, then ENTER_FRAME will not dispatch on it.
You can get around this a few ways.
Pass into the class a reference to something that is on the stage (or the stage itself). Then add the ENTER_FRAME listener on that object.
var stage:Stage;
public function MyClass(stage_:Stage){
stage = stage_;
}
....
stage.addEventListener(Event.ENTER_FRAME, myFunction);
Forgo ENTER_FRAME, and just use a Timer
var timer:Timer
public function play_mp3(path:String,path1:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
timer = new Timer(250); //tick every quarter second
timer.addEventListener(TimerEvent.TIMER, myFunction);
timer.start();
}
public function myFunction(e:Event){
LogFB.info('test1234');
}
Your problem is in LogFB.
Try this and you will see the trace from inside the function...
var channel : SoundChannel = new SoundChannel();
function play_mp3(path:String,path1:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
addEventListener(Event.ENTER_FRAME,myFunction);
}
function myFunction(e:Event){
//LogFB.info('test1234'); here is the problem
// trace("is working!");
hi();
}
play_mp3('aasa','aaa');
function hi() {
trace("goooooddddd!"); //is working, I am using Flash CC
}
All the best!
there doesnt seem any error try this one
public function play_mp3(path:String,path1:String):void {
addEventListener(Event.ENTER_FRAME,myFunction);
var snd:Sound = new Sound();
snd.load(new URLRequest(path), new SoundLoaderContext());
channel = snd.play();
}
public function myFunction(e:Event){
LogFB.info('test1234'); }
ie put the enterframe event the first thing that hapen in your code to check if at least it initialise it?
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
}
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);
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();
}
New to this one. Have this code for viewing multiple netstream events. Want to use the onPlayStatus to loop the "flv/intro.flv" and for the rest of the videos, I would like them to return to the intro.flv after they are done playing, but I can't find anything that helps enough. Can anyone offer a link or some help with the function? Here is my code so far :
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var metaDataListener:Object = new Object();
metaDataListener.onMetaData = function(meta:Object){
}
ns.client = metaDataListener
var myVideo:Video = new Video(800, 600);
myVideo.x = 0;
myVideo.y = 0;
addChild(myVideo);
setChildIndex(myVideo, 0);
myVideo.attachNetStream(ns);
ns.play("flv/intro.flv");
duetBTN.addEventListener(MouseEvent.CLICK, playVideo1);
vantageBTN.addEventListener(MouseEvent.CLICK, playVideo2);
cabrioBTN.addEventListener(MouseEvent.CLICK, playVideo3);
classicBTN.addEventListener(MouseEvent.CLICK, playVideo4);
laundryBTN.addEventListener(MouseEvent.CLICK, playVideo5);
resourceBTN.addEventListener(MouseEvent.CLICK, playVideo6);
industryBTN.addEventListener(MouseEvent.CLICK, playVideo7);
homeBTN.addEventListener(MouseEvent.CLICK, playVideo8);
function playVideo1(e:MouseEvent):void {
ns.play ("flv/duet.flv");
}
function playVideo2(e:MouseEvent):void {
ns.play("flv/vantage.flv");
}
function playVideo3(e:MouseEvent):void {
ns.play("flv/cabrio.flv");
}
function playVideo4(e:MouseEvent):void {
ns.play("flv/classic.flv");
}
function playVideo5(e:MouseEvent):void {
ns.play("flv/laundry.flv");
}
function playVideo6(e:MouseEvent):void {
ns.play("flv/resource.flv");
}
function playVideo7(e:MouseEvent):void {
ns.play("flv/industry.flv");
}
function playVideo8(e:MouseEvent):void {
ns.play ("flv/intro.flv");
}
You can attach an event listener to the NetStream object that detects when a video has finished playing.
var introPlayer:NetStream = new NetStream(nc); // nc refers to shared net connection declared earlier
var introVid:Video = new Video(800, 600);
ns.addEventListener(NetStatusEvent.NET_STATUS, checkStreamStatus);
function checkStreamStatus(e:NetStatusEvent):void {
switch (e.info.code) {
case "NetStream.Play.Complete":
playIntro();
break;
}
};
function playIntro():void {
addChild(introVid);
introVid.attachNetStream(introPlayer);
introPlayer.play("flv/intro.flv");
}
Have changed the code completely to match the set-up you were using originally. This is how you're supposed to do it, apologies for previous answer. This is the correct way of doing it though.
If NetStream.Play.Complete is not dispatched, replace it with:
NetStream.Play.Stop or NetStream.Buffer.Flush