I have video streaming from the server, and later on I want to add another one just side by side. thats all good, I done that. Now my problem comes when i want to remove video. I manage to remove it from display, but I can hear that video is still playing in the background. So how do I can stop streaming that video?
Here is my code for setting up the video:
ns = new NetStream(connection);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play(item[1].toString() + ".flv");
video = new Video();
video.attachNetStream( ns );
video.width = 160;
video.height = 120;
videoWrapper = new UIComponent();
videoWrapper.addChild( video );
videos.addElement( videoWrapper );
and here is for removing
videos.removeElement(myVideos[p][1]); // myVideos[p][1] is a reference of videoWrapper
You can drop the connection by calling video.attachNetStream(null), or close the stream with ns.close();
It's probably best to do both.
On a normal video object, do not call attachNetStream(null), because AS3, later will not let clear the last video frame from the video with .clear()
Looks like a bug.
Use ns.close(), then use vid.clear(), so that the video object can be transparent again.
On StageVideo, this is different:
You can call vid.attachNetStream(null), it will remove the last frame too from the stagevideo.
Be careful, it will not stop the playing itself. You have to call ns.close();
Related
I'm trying to achieve seamless looping of an MP4 (or M4A to be more specific) sound. The usual method of loading, playing to the end, and then seeking to position zero causes a small delay (less than a second) before the second playback begins.
So now I'm trying to preload the sound data and then use NetStream.appendBytes() to append the whole sound data at the end of the original sound, expecting the sound to repeat once. But first, I want to be able to play the sound just once using the appendBytes() method. Here is the code I wrote that doesn't give any errors but doesn't play a sound either:
var data:ByteArray = new SoundData; // SoundData class contains the embedded M4A file data
var connection: NetConnection = new NetConnection();
connection.connect(null);
var stream: NetStream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
stream.client = {};
function onNetStatus(event:NetStatusEvent): void {
trace(event.info.code, stream.time, stream.bytesLoaded, stream.bytesTotal);
}
stream.play(null);
stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
stream.appendBytes(data);
Am I using appendBytes() correctly?
I ended up using a hack for achieving apparent seamless looping: loading two sound files through NetStream, playing one, and 100ms before its end, starting to play the other.
I am using sound files from the library, not URLs.
I am making a soundboard and have a button that plays background music. The reason I have a button instead of automatically playing on load is that there are a few different background musics to choose from, each with their own button.
I also have several other buttons that play sounds. I want the background music volume to lower to something like 0.3 while other sounds are playing. I believe I need to create a variable like soundCount that goes up when a sound plays, and decreases when the sound finishes playing (so that if there are multiple sounds playing at once, the background music won't go back to normal until the count is at 0).
What's the best way to set this up? I made the soundCount variable but I'm having trouble creating an event listener for when sound effect finishes playing.
To change the volume of a sound in AS3, you need to use the SoundChannel and SoundTransform classes:
var backgroundMusic:Sound = // Assuming you've loaded a sound
var myChannel:SoundChannel = new SoundChannel();
myChannel = backgroundMusic.play();
var myTransform = new SoundTransform();
myChannel.soundTransform = myTransform;
Keep track of total sounds in the background by incrementing your count during calls to play and decrementing during SOUND_COMPLETE events. Add event listeners to other sounds' channels so they can modify background's volume:
var totalCurrentSounds:int = 0;
otherSoundChannel.addEventListener(Event.SOUND_COMPLETE, otherSoundComplete);
// Make sure this is on a SoundChannel, not the Sound itself.
Finally, modify the SoundTransform's volume member when the count is 0:
function otherSoundComplete(e:Event):void {
totalCurrentSounds--;
if (totalCurrentSounds <= 0)
myTransform.volume = 0.5;
}
I have NetStream attached to video control in flash movie.
I cannot understand how to jump forward or backward.
var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("Video.flv");
vid.attachNetStream(ns);
I need somethinf like
btnSkip.addEventListener(MouseEvent.CLICK, playClicked);
function playClicked(e:MouseEvent):void {
ns.pause();
//ns.step(1000)
//ns.seek(1);
ns.resume();
}
ns.step() - doesn't work and I don't know why.
ns.seek - works fine, but I don't know where is a position, there's no ns.position and no ns.fps properties to add ns.seek(ns.position+(ns.fps*3)) to skip 3 seconds forward.
It is rather simple actually. Use the seek method on your netStream. To get the position use the time method and then add the offset you need
For 3 sec forward:
ns.seek(ns.time + 3);
For 3 sec backward:
ns.seek(ns.time - 3);
This article might help you a little further: Netstream and step() or seek()?.
What are you trying to play? If it's just a static video you might be able to use the regular video objects, then you'll be able to use the playheadTime property (which might not be very accurate by the way - depending on the number of keyframes).
Good luck!
For HTML5 Audio, let's say you have a list of two songs you want to play. Currently I have it set up so that when the current song stops playing, it loads the new song and plays it. I want to have it so that it loads the next song while the current song is finishing, maybe 20 seconds before the current song finishes. I tried to change the src attribute for the audio object while the song is playing, but that just immediately stops playback for the current song. Is there some other method that allows me to preload the next song while the current song is playing?
You could use jQuery to create a jQuery object:
var nextSong = document.createElement('audio'); //Creates <audio></audio>
nextSong = $(nextSong); //Converts it to a jQuery object
$(nextSong).attr('autoplay') = false; //You don't want this dynamically loaded audio to start playing automatically
$(nextSong).attr('preload') = "auto"; //Make sure it starts loading the file
$(nextSong).attr('src') = url_to_src; //Loads the src
This should start load the song into an element in the browser's memory and when the song is over, call something like:
$(audio).replace(nextSong);
This isn't tested. You probably don't even need jQuery.
This may work without jQuery:
var nextSong = document.createElement('audio');
nextSong.autoplay = 'false';
nextSong.preload = 'auto';
nextSong.src = url_to_src;
Give it a whirl and let me know!
This might be off the mark, but have you tried calling the element's load() method?
http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#loading-the-media-resource
Edit:
Ah, I think I misunderstood the problem. You want to play two songs back to back in the same media element? I'm not sure how feasible that is... it might be easier to place each song in its own Audio element. You could always dynamically generate these, if you're worried about flexibility.
I am trying to stop the audio of the video swf and I can't get it to stop. Here is my code for loading the file:
var myLoader:Loader= new Loader();
myLoader.x=420;
myLoader.y=200;
// boolean variable set for use below in our function
var screenCheck:Boolean = false;
//These three linces keep stafe elements the same size, so they don't distort
var swfStage:Stage = this.stage;
video_btn.addEventListener(MouseEvent.CLICK,contentvideo);
function contentvideo (event:MouseEvent):void{
myLoader.load(new URLRequest("prevideo.swf"));
addChild(myLoader);
movie_btn.stop();
movie_btn.visible=false;
}
Now I have other functions that load different URLRequest and when they are loading, the audio keeps playing. Do I have to add a line of code to them? I also have an MP3 player and tried SoundMixer.stopAll(). I still need the mp3 player to keep playing.
I'm not familiar with what you're doing but just looking at the code and given the problem you're experiencing I wonder if that
addChild(myLoader);
has anything to do with it. It seems like the kind of thing that could easily create multiple child objects which is why you continue to experience the sound play back. Is there a removeChild[0] option or something?
A shot in the dark I know but I thought I'd offer the possibility anyway.