Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 11-28-2009, 11:46 AM   #1
bobcooper
Registered User
Delete / Destroy video sound

Hi There,

I have an annoying problem with a video player I am creating, here is the code:

Code:
package bc.utils {
	import flash.system.System;
	import flash.display.MovieClip;
	import flash.net.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.media.*;
	import bc.utils.*;
	
	public class LoadVideo extends MovieClip {
		// import global variables
		var gvc:GlobalVarContainer = new GlobalVarContainer;
			
		var videoMovie:MovieClip;
		var videoPath:String;
		var videoX:Number;
		var videoY:Number;
		var videoWidth:Number;
		var videoAutoPlay:Boolean;
		
		var ncConnection:NetConnection;
		var nsStream:NetStream;
		
		var videoLength:Number;
		// time to buffer for the video in sec.
		const BUFFER_TIME:Number = 8;
		// start volume when initializing player
		const DEFAULT_VOLUME:Number = 0.5;
		// update delay in milliseconds.
		const DISPLAY_TIMER_UPDATE_DELAY:int = 10;
		// smoothing for video. may slow down old computers
		const SMOOTHING:Boolean = true;
		// flag for knowing if flv has been loaded
		var bolLoaded:Boolean = false;
		// flag for volume scrubbing
		var bolVolumeScrub:Boolean = false;
		// flag for progress scrubbing
		var bolProgressScrub:Boolean = false;
		// holds the last used volume, but never 0
		var intLastVolume:Number = DEFAULT_VOLUME;
		
		var sndTransform;
		
		var colourChange:ColourChange = new ColourChange();
		var dropShadow:DropShadow = new DropShadow();
		

		
		public function LoadVideo(vidMovie, vidPath, vidX, vidY, vidWidth, vidAutoPlay):void{
			videoMovie = vidMovie;
			videoPath = vidPath;
			videoX = vidX;
			videoY = vidY;
			videoWidth = vidWidth;
			videoAutoPlay = vidAutoPlay;
			
			videoMovie.x = videoX;
			videoMovie.y = videoY;
			videoMovie.width = videoWidth;
			videoMovie.scaleY = videoMovie.scaleX;
			videoMovie.visible = true;
			videoMovie.videoBG_mc.visible = false;
			
			dropShadow.dropShadow(videoMovie.videoBG_mc, 0.5, 45, 5, 5, gvc.colour2, 10);
			addChild(dropShadow);
			
			initVideoPlayer();
		}
		
		// sets up the player
		function initVideoPlayer():void {
			// hide buttons
			videoMovie.mcVideoControls.btnUnmute.visible = false;
			videoMovie.mcVideoControls.btnPause.visible	= false;
			videoMovie.pauseBig_btn.visible = false;
			videoMovie.playBig_btn.visible = true;
			
			videoMovie.vidDisplay.visible = false;
			videoMovie.mcVideoControls.y = 272;
			
			videoMovie.playBig_btn.alpha = 1;
			videoMovie.pauseBig_btn.alpha = 0;
			
			// add global event listener when mouse is released
			videoMovie.parent.addEventListener( MouseEvent.MOUSE_UP, mouseReleased);
			
			// add event listeners to all buttons
			videoMovie.mcVideoControls.btnPause.addEventListener(MouseEvent.CLICK, pauseClicked);
			videoMovie.pauseBig_btn.addEventListener(MouseEvent.CLICK, pauseClicked);
			videoMovie.mcVideoControls.btnPlay.addEventListener(MouseEvent.CLICK, playClicked);
			videoMovie.playBig_btn.addEventListener(MouseEvent.CLICK, playClicked);
			videoMovie.mcVideoControls.btnStop.addEventListener(MouseEvent.CLICK, stopClicked);
			videoMovie.mcVideoControls.btnMute.addEventListener(MouseEvent.CLICK, muteClicked);
			videoMovie.mcVideoControls.btnUnmute.addEventListener(MouseEvent.CLICK, unmuteClicked);
			videoMovie.mcVideoControls.mcVolumeScrubber.btnVolumeScrubber.addEventListener(MouseEvent.MOUSE_DOWN, volumeScrubberClicked);
			videoMovie.mcVideoControls.mcPlayPosScrubber.btnVolumeScrubber.addEventListener(MouseEvent.MOUSE_DOWN, progressScrubberClicked);
			
			
			// create a new net connection, add event listener and connect
			// to null because we don't have a media server
			ncConnection = new NetConnection();
			ncConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			ncConnection.connect(null);
			
			// create a new netstream with the net connection, add event
			// listener, set client to this for handling meta data and
			// set the buffer time to the value from the constant
			nsStream = new NetStream(ncConnection);
			nsStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			//nsStream.client = this;
			nsStream.bufferTime = BUFFER_TIME;
			
			var netClient:Object = new Object();
			netClient.onMetaData = function(meta:Object){
				videoLength = meta.duration;
				trace(videoLength);
				// event listener
				videoMovie.videoBG_mc.addEventListener(Event.ENTER_FRAME, updateDisplay);
			};
			nsStream.client = netClient;
			
			videoMovie.mcVideoControls.playPosBar_mc.width = 0;
			videoMovie.mcVideoControls.mcPlayPosScrubber.x = videoMovie.mcVideoControls.playPosTrack_mc.x;
			
			// attach net stream to video object on the stage
			videoMovie.vidDisplay.attachNetStream(nsStream);
			// set the smoothing value from the constant
			videoMovie.vidDisplay.smoothing = SMOOTHING;
		
			// set default volume
			videoMovie.mcVideoControls.mcVolumeScrubber.x = (videoMovie.mcVideoControls.volumeTrack_mc.width * DEFAULT_VOLUME) + videoMovie.mcVideoControls.volumeTrack_mc.x;
			videoMovie.mcVideoControls.volume_mc.width = videoMovie.mcVideoControls.mcVolumeScrubber.x - videoMovie.mcVideoControls.volumeTrack_mc.x;
			setVolume(DEFAULT_VOLUME);
		}
		
		function playClicked(e:MouseEvent):void {
			// check's, if the flv has already begun
			// to download. if so, resume playback, else
			// load the file
			if(!bolLoaded) {
				nsStream.play(videoPath);
				bolLoaded = true;
			}
			else{
				nsStream.resume();
			}
			
			// show video display
			videoMovie.vidDisplay.visible = true;
			videoMovie.videoBG_mc.visible = true;
			
			// switch play/pause visibility
			videoMovie.mcVideoControls.btnPause.visible = true;
			videoMovie.mcVideoControls.btnPlay.visible = false;
			videoMovie.pauseBig_btn.visible = true;
			videoMovie.playBig_btn.visible = false;
			videoMovie.pauseBig_btn.alpha = 1;
			
			
			
			
		}
		
		function pauseClicked(e:MouseEvent):void {
			// pause video
			nsStream.pause();
			
			// switch play/pause visibility
			videoMovie.mcVideoControls.btnPause.visible = false;
			videoMovie.mcVideoControls.btnPlay.visible = true;
			videoMovie.pauseBig_btn.visible = false;
			videoMovie.playBig_btn.visible = true;
		}
		
		function stopClicked(e:MouseEvent):void {
			// calls stop function
			stopVideoPlayer();
		}
		
		function muteClicked(e:MouseEvent):void {
			// set volume to 0
			setVolume(0);
			
			// update scrubber and fill position/width
			videoMovie.mcVideoControls.mcVolumeScrubber.x = videoMovie.mcVideoControls.volumeTrack_mc.x;
			videoMovie.mcVideoControls.volume_mc.width = 1;
		}
		
		function unmuteClicked(e:MouseEvent):void {
			// set volume to last used value
			setVolume(intLastVolume);
		
			// update scrubber and fill position/width
			videoMovie.mcVideoControls.mcVolumeScrubber.x = (videoMovie.mcVideoControls.volumeTrack_mc.width * intLastVolume) + videoMovie.mcVideoControls.volumeTrack_mc.x;
			videoMovie.mcVideoControls.volume_mc.width = videoMovie.mcVideoControls.mcVolumeScrubber.x - videoMovie.mcVideoControls.volumeTrack_mc.x;
		}
		
		function volumeScrubberClicked(e:MouseEvent):void {
			// set volume scrub flag to true
			bolVolumeScrub = true;
			
			// start drag
			videoMovie.mcVideoControls.mcVolumeScrubber.startDrag(false, new Rectangle(videoMovie.mcVideoControls.volumeTrack_mc.x, videoMovie.mcVideoControls.volumeTrack_mc.y, videoMovie.mcVideoControls.volumeTrack_mc.width, 0));
		}
		
		function progressScrubberClicked(e:MouseEvent):void {
			// set progress scrub flag to true
			bolProgressScrub = true;
			
			// start drag
			videoMovie.mcVideoControls.mcPlayPosScrubber.startDrag(false, new Rectangle(videoMovie.mcVideoControls.playPosTrack_mc.x, videoMovie.mcVideoControls.playPosTrack_mc.y, videoMovie.mcVideoControls.playPosTrack_mc.width, 0));
		}
		
		function mouseReleased(e:MouseEvent):void {
			// set progress/volume scrub to false
			bolVolumeScrub = false;
			bolProgressScrub = false;
			
			// stop all dragging actions
			videoMovie.mcVideoControls.mcPlayPosScrubber.stopDrag();
			videoMovie.mcVideoControls.mcVolumeScrubber.stopDrag();
			
			// update progress/volume fill
			videoMovie.mcVideoControls.playPosBar_mc.width = videoMovie.mcVideoControls.mcPlayPosScrubber.x - videoMovie.mcVideoControls.playPosTrack_mc.x;
			videoMovie.mcVideoControls.volume_mc.width = videoMovie.mcVideoControls.mcVolumeScrubber.x - videoMovie.mcVideoControls.volumeTrack_mc.x;
			
			// save the volume if it's greater than zero
			if((videoMovie.mcVideoControls.mcVolumeScrubber.x - videoMovie.mcVideoControls.volumeTrack_mc.x) / videoMovie.mcVideoControls.volumeTrack_mc.width > 0){
				intLastVolume = (videoMovie.mcVideoControls.mcVolumeScrubber.x - videoMovie.mcVideoControls.volumeTrack_mc.x) / videoMovie.mcVideoControls.volumeTrack_mc.width;
			}
		}
		
		function updateDisplay(e:Event):void {
			// update volume and the red fill width when user is scrubbing
			if(bolVolumeScrub) {
				setVolume((videoMovie.mcVideoControls.mcVolumeScrubber.x - videoMovie.mcVideoControls.volumeTrack_mc.x) / videoMovie.mcVideoControls.volumeTrack_mc.width);
				videoMovie.mcVideoControls.volume_mc.width = videoMovie.mcVideoControls.mcVolumeScrubber.x - videoMovie.mcVideoControls.volumeTrack_mc.x;
			}
					
			if(mouseX > videoX && mouseX < videoX + videoWidth && mouseY > videoY && mouseY < videoY + videoMovie.vidDisplay.height) { 
				if(nsStream.time > 0){
					if(videoMovie.mcVideoControls.y > 250){
						videoMovie.mcVideoControls.y -= 2;
					} 
					if(videoMovie.playBig_btn.alpha < 1){
						videoMovie.playBig_btn.alpha += gvc.fadeSpeed;
						videoMovie.pauseBig_btn.alpha += gvc.fadeSpeed;
					}
						
				}
			} else {
				if(videoMovie.mcVideoControls.y < 272){
					videoMovie.mcVideoControls.y += 2;
				} 
				if(nsStream.time > 0){
					if(videoMovie.playBig_btn.alpha > 0){
						videoMovie.playBig_btn.alpha -= gvc.fadeSpeed;
						videoMovie.pauseBig_btn.alpha -= gvc.fadeSpeed;
					} else {
						videoMovie.playBig_btn.alpha = 0;
						videoMovie.pauseBig_btn.alpha = 0;
					}
				}
			}
			if(bolProgressScrub) {
				videoMovie.mcVideoControls.playPosBar_mc.width = videoMovie.mcVideoControls.mcPlayPosScrubber.x - videoMovie.mcVideoControls.playPosTrack_mc.x;
				var newTime = Math.floor(((videoMovie.mcVideoControls.mcPlayPosScrubber.x - videoMovie.mcVideoControls.playPosTrack_mc.x)/videoMovie.mcVideoControls.playPosTrack_mc.width)*videoLength)
				if(newTime >=0){
					nsStream.seek(newTime);
				}
			} else {
				var newPos = Math.floor(videoMovie.mcVideoControls.playPosTrack_mc.x + ((nsStream.time/videoLength)*videoMovie.mcVideoControls.playPosTrack_mc.width));
				videoMovie.mcVideoControls.mcPlayPosScrubber.x = newPos;
				videoMovie.mcVideoControls.playPosBar_mc.width = newPos - videoMovie.mcVideoControls.playPosTrack_mc.x;
				
			}
		}
		
		function netStatusHandler(event:NetStatusEvent):void {
			trace(event.info.code);
			// handles net status events
			switch (event.info.code) {
				// trace a messeage when the stream is not found
				case "NetStream.Play.StreamNotFound":
					trace("Stream not found: " + videoPath);
				break;
				
				case "NetConnection.Connect.Success":
					
				break;
				
				case "NetStream.Play.Start":
					
				break;
				
				// when the video reaches its end, we stop the player
				case "NetStream.Play.Stop":
					stopVideoPlayer();
				break;
			}
		}
		
		function setVolume(intVolume:Number = 0):void {
			// create soundtransform object with the volume from
			// the parameter
			sndTransform = new SoundTransform(intVolume);
			// assign object to netstream sound transform object
			nsStream.soundTransform	= sndTransform;
			
			// hides/shows mute and unmute button according to the
			// volume
			if(intVolume > 0) {
				videoMovie.mcVideoControls.btnMute.visible = true;
				videoMovie.mcVideoControls.btnUnmute.visible = false;
			} else {
				videoMovie.mcVideoControls.btnMute.visible = false;
				videoMovie.mcVideoControls.btnUnmute.visible = true;
			}
		}
		
		function formatTime(t:int):String {
			// returns the minutes and seconds with leading zeros
			// for example: 70 returns 01:10
			var s:int = Math.round(t);
			var m:int = 0;
			if (s > 0) {
				while (s > 59) {
					m++;
					s -= 60;
				}
				return String((m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s);
			} else {
				return "00:00";
			}
		}
		
		// STOP VIDEO PLAYER
		public function stopVideoPlayer():void {
			flash.media.SoundMixer.stopAll();
			// pause netstream, set time position to zero
			nsStream.pause();
			nsStream.close();
			videoLength = 0;
			videoMovie.videoBG_mc.removeEventListener(Event.ENTER_FRAME, updateDisplay);
			
			videoMovie.mcVideoControls.y = 272;
			var videoClass:LoadVideo = new LoadVideo(videoMovie, videoPath, 50, 100, 360, false);
			addChild(videoClass);
		}
	}
}
This works great for one video, but when I call stopVideoPlayer to stop and load a new video, it plays the new video and sound but then it plays the sound from the previous video over the top. I am thinking that I need to delete / destroy the sound from my previous video before loading the new one but cannot figure out how to do it. Anyone got any ideas?

Thanks,
Bob
www.bobcooper.org.uk

__________________
--
BC DESIGN
Creative Design For Creative People

www.bobcooper.org.uk
bobcooper is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-28-2009, 03:04 PM   #2
0L4F
Registered User
Location The Hague, The Netherlands

Posts 82
Hi bobcooper,
ActionScript Code:
nsStream.close();

should be all there is to it. I've been working with a videoPlayer class today, that only needs netStream.close() to stop both video and audio. I see you are correctly calling close() in stopVideoPlayer. Strange!
0L4F is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:35 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com