Actionscript 3 - Netstream get published name - actionscript-3

I have a netstream and I want to publish a stream with its own name, but multiple streams might be published at the same time. What causes the conflict is that all the streams have the same name because I can't figure out how to get other streams' names from other clients and play them, and publish my stream with a different name.
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.media.Microphone;
import flash.media.Sound;
var Mic:Microphone;
var aud:Sound;
var nc:NetConnection = new NetConnection;
nc.client = this;
var istream:NetStream;
var ostream:NetStream;
Security.showSettings("2");
if(Microphone.names.length <= 0) {
// no microphone
} else {
Mic = Microphone.getMicrophone();
Mic.rate = 44;
Mic.setUseEchoSuppression(true);
Mic.setLoopBack(false);
Mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
Mic.addEventListener(StatusEvent.STATUS, statusHandler);
}
icon_btn.addEventListener(MouseEvent.MOUSE_DOWN, talkDown);
icon_btn.addEventListener(MouseEvent.MOUSE_UP, talkUp);
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.addEventListener(NetGroup.MulticastStream.PublishNotify
nc.connect("rtmp://server.xxxxxxxxxxxxx.us:1935/oflaDemo");
function talkDown(e:MouseEvent):void {
ostream.publish("mcp");
istream.receiveAudio(false);
}
function talkUp(e:MouseEvent):void {
ostream.close();
istream.receiveAudio(true);
timer.stop();
}
function netStatusHandler(event:NetStatusEvent):void {
trace(event.info.code)
ostream = new NetStream(nc);
istream = new NetStream(nc);
istream.play("mcp");
ostream.attachAudio(Mic);
}
function activityHandler(event:ActivityEvent):void {
trace("activityHandler: " + event);
}
function statusHandler(event:StatusEvent):void {
trace("statusHandler: " + event);
}
By reading the code, I'm pretty sure you figured out what the conflict is.

Related

as3 preloader stop loaded swf from caching?

sorry if this has been answered elsewhere but I can't find anything matching.
I have two swfs; a preloader (let's call it A) and some content (B). A loads B and adds it as a child. Everything is working beautifully (you can even see it here).
There's just one little problem I'm having. Normally when loading, say an image, into flash using the URLLoader Class, I add + "?" + new Date().getTime() to the URLRequest to force flash to load the latest version of the target, in other words to stop it using a cached version. Now, when I try to do this to the Loader that adds B to A, it can't find the URL (#2035 URL not found). So my question is: is what I'm trying to do possible, or should I take another approach to stopping B from caching?
Here's the preloader code:
package
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.net.URLLoader;
import flash.text.TextField;
public class claude_loader extends MovieClip
{
public var main_movie:Loader = new Loader();
public var rss_loader:URLLoader;
private var perc_text:TextField = new TextField();
public function claude_loader()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
with (perc_text)
{
x = this.stage.stageWidth / 2;
y = this.stage.stageHeight / 2;
}
addChild(perc_text);
main_movie.load(new URLRequest("claudia_summers.swf"+ "?" + new Date().getTime()));
main_movie.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, load_progress);
main_movie.contentLoaderInfo.addEventListener(Event.COMPLETE,on_complete);
}
public function load_progress(e:ProgressEvent):void
{
var perc:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
perc_text.text = "Loading " + perc + "%";
if (e.bytesLoaded == e.bytesTotal)
{
perc_text.text = "Loading done";
}
}
public function on_complete(e:Event):void
{
rss_loader = new URLLoader(new URLRequest("http://news.sulsc.org/feed"));
rss_loader.addEventListener(Event.COMPLETE,rss_complete);
perc_text.text = "Loading RSS";
}
public function rss_complete(e:Event):void
{
MovieClip(main_movie.content).rss_xml = XML(e.target.data);
addChild(main_movie);
}
}
}
Not sure how/why, but adding String() around new Date().getTime() and giving main_movie an IOErrorEvent listener resolves the issue. I think I have a bug somewhere, as this surely should have worked without these additions, no? Anyway thanks for all your help!
Working code:
package
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.net.URLLoader;
import flash.text.TextField;
import flash.events.IOErrorEvent;
public class claude_loader extends MovieClip
{
public var main_movie:Loader = new Loader();
public var rss_loader:URLLoader;
private var perc_text:TextField = new TextField();
public function claude_loader()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
with (perc_text)
{
x = this.stage.stageWidth / 2;
y = this.stage.stageHeight / 2;
textColor = 0xFFFFFF;
}
addChild(perc_text);
main_movie.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,catch_error);
main_movie.load(new URLRequest("claudia_summers.swf" + "?" + String(new Date().getTime())));
function catch_error(e:IOErrorEvent):void
{
main_movie.load(new URLRequest("claudia_summers.swf"));
perc_text.text = "Failed non-cache load"
}
main_movie.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, load_progress);
main_movie.contentLoaderInfo.addEventListener(Event.COMPLETE,on_complete);
}
public function load_progress(e:ProgressEvent):void
{
var perc:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
perc_text.text = "Loading " + perc + "%";
if (e.bytesLoaded == e.bytesTotal)
{
perc_text.text = "Loading done";
}
}
public function on_complete(e:Event):void
{
rss_loader = new URLLoader(new URLRequest("http://news.sulsc.org/feed"));
rss_loader.addEventListener(Event.COMPLETE,rss_complete);
perc_text.text = "Loading RSS";
}
public function rss_complete(e:Event):void
{
MovieClip(main_movie.content).rss_xml = XML(e.target.data);
addChild(main_movie);
}
}
}

1195 Attempted access of inaccessible method playVideo through a reference with static type NSPv4

Call:
import fl.video.*
import NSPv4;
var playVid:NSPv4 = new NSPv4();
playVid.playVideo(this.parent);
Class:
package
{
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.events.*;
import flash.net.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import fl.video.*;
public class NSPv4 extends MovieClip
{
var __PARENT;
var __PARENT1;
public function playAudio(_PARENT):void
{
__PARENT = _PARENT;
var audioLabel:String = _PARENT.currentLabel;
var audioLabelNum:String = audioLabel.replace("sct","audio/");
audioLabelNum += ".mp3";
var vo:Sound = new Sound(new URLRequest(audioLabelNum));
var channel:SoundChannel = new SoundChannel();
channel = vo.play();
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
public function playVideo(_PARENT1):void
{
__PARENT1 = _PARENT1;
var videoLabel:String = _PARENT1.currentLabel;
var videoLabelNum:String = videoLabel.replace("sct","audio/");
videoLabelNum += ".f4v";
var videoPlayer:FLVPlayback = new FLVPlayback();
videoPlayer.source = videoLabelNum;
_PARENT1.addChild(videoPlayer);
}
public function onPlaybackComplete(event:Event):void
{
SoundMixer.stopAll();
MovieClip(__PARENT).nextFrame();
}
}
}
Can call the playAudio with no problems but when I try playVideo() I get the following error:
1195 Attempted access of inaccessible method playVideo through a reference with static type NSPv4
I set up a separate class for the video to stand alone without the playAudio() function and this works too.
Most confused!

Variable cmodule.shine::CLibInit is not defined. AS3

I am using AS3 on Flash... I am trying to modify a .fla file which is audio recording and add a mp3 convert using ShineMP3Encoder, but when I ran the program, record and convert I always end at this error
<!-- Flash output - read from bottom to top
ReferenceError: Error #1065: Variable cmodule.shine::CLibInit is not defined. // this is the error
at fr.kikko.lab::ShineMP3Encoder/start()
at Main/encodeToMP3()
at Main/recordComplete()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at org.bytearray.micrecorder::MicRecorder/stop()
at Main/stopRecording()
-->
Here is my Code:
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.net.*;
import flash.media.*;
import flash.utils.*;
import flash.display.Sprite;
import flash.media.Microphone;
import flash.system.Security;
import org.bytearray.micrecorder.*;
import org.bytearray.micrecorder.events.RecordingEvent;
import org.bytearray.micrecorder.encoder.WaveEncoder;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.ActivityEvent;
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.net.FileReference;
import flash.utils.ByteArray;
import fr.kikko.lab.ShineMP3Encoder;
public class Main extends Sprite
{
private var mic:Microphone;
private var waveEncoder:WaveEncoder = new WaveEncoder();
private var recorder:MicRecorder = new MicRecorder(new WaveEncoder());
private var recBar:RecBar = new RecBar();
private var tween:Tween;
private var fileReference:FileReference = new FileReference();
private var mp3Encoder:ShineMP3Encoder;
public function Main():void
{
recButton.stop();
activity.stop();
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.gain = 100;
mic.setLoopBack(true);
mic.setUseEchoSuppression(true);
Security.showSettings("2");
addListeners();
}
private function addListeners():void
{
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
recorder.addEventListener(RecordingEvent.RECORDING, recording);
recorder.addEventListener(Event.COMPLETE, recordComplete);
activity.addEventListener(Event.ENTER_FRAME, updateMeter);
}
private function startRecording(e:MouseEvent):void
{
if (mic != null)
{
recorder.record();
e.target.gotoAndStop(2);
recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);
addChild(recBar);
tween = new Tween(recBar,"y",Strong.easeOut, - recBar.height,0,1,true);
}
}
private function stopRecording(e:MouseEvent):void
{
recorder.stop();
mic.setLoopBack(false);
e.target.gotoAndStop(1);
recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
tween = new Tween(recBar,"y",Strong.easeOut,0, - recBar.height,1,true);
}
private function updateMeter(e:Event):void
{
activity.gotoAndPlay(100 - mic.activityLevel);
}
private function recording(e:RecordingEvent):void
{
var currentTime:int = Math.floor(e.time / 1000);
recBar.counter.text = String(currentTime);
if (String(currentTime).length == 1)
{
recBar.counter.text = "00:0" + currentTime;
}
else if (String(currentTime).length == 2)
{
recBar.counter.text = "00:" + currentTime;
}
}
private function recordComplete(e:Event):void
{
//trace(recorder.output.bytesAvailable)
encodeToMP3(recorder.output)
//fileReference.save(recorder.output, "recording.wav");
}
//this is the function I added
private function encodeToMP3(wavData:ByteArray):void {
mp3Encoder = new ShineMP3Encoder(wavData);
mp3Encoder.addEventListener(Event.COMPLETE, mp3EncodeComplete);
mp3Encoder.start();
}
private function mp3EncodeComplete(event : Event) : void {
trace("Done !", mp3Encoder.mp3Data.length);
}
}
}
Thanks.... :)
Mody the link type of *shineMP3_alchemy.swc* from "External" to "Merged into code" where you set the Library path for ActionScript 3.0 files

NetStream Info Returning 0's (Icecast Stream)

I've searched high and low for a fix to this issue, but cannot find ANYTHING on why almost all the properties are being returned as 0.
I am using FLV wrapping to pull a live audio stream from Icecast (since Adobe, 15 years later, still haven't fixed their live audio memory leak issue). It works great, everything functions perfectly.
However, I'm wanting to create a bandwidth monitor (for my iPhone port, and for my normal Flash player)... But whenever I retrieve netStreamInfo it returns as 0! For dataBytesPerSecond, audioBytesPerSecond, byteCount, dataByteCount, nearly EVERY SINGLE PROPERTY returns 0. I have this run on a 1-second timer.
Here's the total info output:
currentBytesPerSecond=0
byteCount=0
maxBytesPerSecond=0
audioBytesPerSecond=0
audioByteCount=0
videoBytesPerSecond=0
videoByteCount=0
dataBytesPerSecond=0
dataByteCount=0
playbackBytesPerSecond=16296.296296296296
droppedFrames=0
audioBufferLength=0.072
videoBufferLength=0
dataBufferLength=0
audioBufferByteLength=1540
videoBufferByteLength=0
dataBufferByteLength=0
srtt=0
audioLossRate=0
videoLossRate=0 Data Bytes Per Second
That output was about 5 minutes in. I noted the playBackBytesPerSecond never changed, and the audioBufferByteLength liked to switch between 1540 and 23xx randomly.
Can anyone pleaaase help me out here?
My actionscript:
package
{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.events.NetStatusEvent;
import flash.utils.Timer;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.display.Loader;
import flash.errors.IOError;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.NetStreamInfo;
import flash.utils.ByteArray;
import flash.media.*
public class soundContainer extends Sprite
{
public var slider:SliderMC = new SliderMC();
private var _video:Video;
private var _stream:NetStream;
private var _playbackTime:TextField;
private var _duration:uint;
private var _timer:Timer;
private var _soundChannel:SoundChannel;
public var audioTransform:SoundTransform = new SoundTransform();
public var _URL:String;
public var flvUrl:String = "s";
public var dragging:Boolean = false;
public var rectangle:Rectangle = new Rectangle(0,0,100,0);
private var ba:ByteArray;
private var bn;
public function soundContainer() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void
{
ba = new ByteArray();
slider.x = 73.85;
slider.y = 10.95;
addChild(slider);
slider.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);
_timer = new Timer(1000);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
}
public function dragIt(e:MouseEvent):void
{
slider.slider_mc.startDrag(false, rectangle);
dragging = true;
slider.slider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume);
}
public function dropIt(e:MouseEvent = null):void
{
if (dragging)
{
slider.slider_mc.stopDrag();
dragging = false;
}
}
public function adjustVolume(e:Event):void
{
var vol:Number = slider.slider_mc.x / 100;
var st:SoundTransform = new SoundTransform(vol);
SoundMixer.soundTransform = st;
}
public function playMyFlv(flvUrl)
{
_URL = flvUrl;
_video = new Video();
var connection:NetConnection = new NetConnection();
connection.connect(null);
_stream = new NetStream(connection);
_stream.soundTransform = audioTransform;
_stream.play(flvUrl);
var Client:Object = new Object();
_stream.client = Client;
_video.attachNetStream(_stream);
addChild(_video);
}
public function stopMyFlv()
{
SoundMixer.stopAll();
trace("stop");
try
{
_stream.close();
}
catch (error:IOError)
{
}
}
private function onNetStatus(e:NetStatusEvent)
{
}
private function onTimer(t:TimerEvent):Number
{
trace(_stream.info + " Data Bytes Per Second");
}
public function onIOError(e:IOError)
{
trace("Failed to load");
}
}
}

Security: Restrict plugin access to file system and network

In a plugin context (a swf loaded by an another swf), is there any way to restrict access to file system and network in the same time to the loaded swf ?
Compiler option "-use-network=true|false" does not fit because you cannot restrict both file/network.
Code example :
Air App :
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.filesystem.File;
import flash.net.URLRequest;
public class TestContentSecurity extends Sprite
{
private var l :Loader = new Loader;
public function TestContentSecurity()
{
addChild(l);
l.load(new URLRequest(File.documentsDirectory.nativePath + "/Content.swf"));
}
}
}
Loaded swf :
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.text.TextField;
public class Content extends Sprite
{
private var _log : TextField = new TextField;
private var l: URLLoader;
public function Content()
{
addChild(_log)
_log.multiline = true;
_log.width = 500;
_log.height = 500;
l = new URLLoader();
l.addEventListener(Event.COMPLETE, onLoad);
l.addEventListener(IOErrorEvent.IO_ERROR, onError);
l.load(new URLRequest("c:/Windows/regedit.exe"))
}
public function onLoad(e:Event) : void{
_log.text += "SUCCESS\n" ;
}
public function onError(e:IOErrorEvent) : void{
_log.text += "ERROR\n";
}
}
}
The loaded swf is in user's document folder, outside Air app folder. Currently, the loaded swf is abble to load "c:/Windows/regedit.exe" and I don't want it (neither sending informations on the network).
I've found one solution in AIR, I don't like it but it works. The idea is to have a mini http server and to load content from this server.
I load targeted file with :
new URLRequest("http://localhost:1111/Content.swf")
By doing this, flash will load "Content.swf" as a remote file and place it in a REMOTE security sandbox. Loaded swf won't be able to access to any local files neither to network.
If anyone have a cleaner solution to get this REMOTE security sand box, I will be happy.
/**
* HTTP server original idea :
* http://coenraets.org/blog/2009/12/air-2-0-web-server-using-the-new-server-socket-api/
*/
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.ServerSocketConnectEvent;
import flash.net.ServerSocket;
import flash.net.Socket;
import flash.utils.ByteArray;
public class TestContentSecurity extends Sprite
{
private var l :Loader = new Loader;
private var serverSocket:ServerSocket;
public function TestContentSecurity()
{
init();
l.load(new URLRequest("http://localhost:1111/Content.swf"));
}
private function init():void
{
// Initialize the web server directory (in applicationStorageDirectory) with sample files
listen(1111);
}
private function listen(port : uint):void
{
try
{
serverSocket = new ServerSocket();
serverSocket.addEventListener(Event.CONNECT, socketConnectHandler);
serverSocket.bind(port, "127.0.0.1");
serverSocket.listen();
trace("Listening on port " + port + "...\n");
}
catch (error:Error)
{
trace("Port " + port +
" may be in use. Enter another port number and try again.\n(" +
error.message +")", "Error");
}
}
private function socketConnectHandler(event:ServerSocketConnectEvent):void
{
var socket:Socket = event.socket;
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
}
private function socketDataHandler(event:ProgressEvent):void
{
try
{
var socket:Socket = event.target as Socket;
var bytes:ByteArray = new ByteArray();
socket.readBytes(bytes);
var request:String = "" + bytes;
var filePath:String = request.substring(5, request.indexOf("HTTP/") - 1);
var file:File = File.applicationDirectory.resolvePath(filePath);
if (file.exists && !file.isDirectory)
{
var stream:FileStream = new FileStream();
stream.open( file, FileMode.READ );
var content:ByteArray = new ByteArray();
stream.readBytes(content);
stream.close();
socket.writeUTFBytes("HTTP/1.1 200 OK\n");
socket.writeUTFBytes("Content-Type: application/x-shockwave-flash\n\n");
socket.writeBytes(content);
}
else
{
socket.writeUTFBytes("HTTP/1.1 404 Not Found\n");
socket.writeUTFBytes("Content-Type: text/html\n\n");
socket.writeUTFBytes("<html><body><h2>Page Not Found</h2></body></html>");
}
socket.flush();
socket.close();
}
catch (error:Error)
{
trace("Error");
}
}
}
}
Not unless you deploy as an AIR application.
You can, however, store some data in a SharedObject, even when you deploy with -use-network=true. This should work for storing game state and such.
Edit:
In AIR, security between content from different domains is regulated by using AIR sandbox bridges. This should give you all the leverage you need.