if i am using the mediaplayback component in Adobe flash, the videostream on a crtmp-server runs correctly.
if i try to do this on my own without using that component i got some errors, unluckily there is no much support explaining streaming over as3 without a mediaplayback-component.
i have following code
package
{
import flash.display.MovieClip;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
/**
* ...
* #author Siam Modi
*/
public class Main extends MovieClip
{
public function Main()
{
var nc:NetConnection = new NetConnection();
nc.connect("rtmp://213.136.73.230/maya");
var vid:Video = new Video();
addChild(vid);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
ns.play("atlas.mp4");
}
}
}
Any ideas ? Thx for help
You can connect a NetStream to a NetConnection instance only after it fires a NetStatusEvent with NetConnection.Connect.Succes info.code.
To get this event you need to call nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler)
before calling
nc.connect("rtmp://213.136.73.230/maya");
and there attach NetStream / Video:
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success":
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
ns.play("atlas.mp4");
break;
case "NetStream.Play.StreamNotFound":
trace("Stream not found: " + videoURL);
break;
}
}
here is an example from the reference
Related
I'm building an application with an Strobe Media Playback in it and I can't play a RTMP stream.
The weird thing here is that I can play the stream on JWPlayer and even in a simple Flash video player with the classic NetStream and NetConnection code.
http://www.longtailvideo.com/jw-player/wizard/
This is the stream: rtmp://fl.world-television.com/streamstudio/vod/flv:endesa/20130422/video_full_es_v2.flv
I don't know if I need to set up any special configuration through the OSMF params.
This is how I have been played the video in a simple Flash CS application.
package {
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.Event;
public class VideoTest extends Sprite {
private var videoURL:String = "rtmp://fl.world-television.com/streamstudio/vod/flv:endesa/20130422/video_full_es_v2.flv";
private var connection:NetConnection;
private var stream:NetStream;
public function VideoTest()
{
initialize();
}
private function initialize():void
{
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.connect("rtmp://fl.world-television.com/streamstudio/vod/" );
}
private function netStatusHandler(event:NetStatusEvent):void {
trace("net status handler: " + event.info.code);
switch (event.info.code) {
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.StreamNotFound":
trace("Stream not found: " + videoURL);
break;
}
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function connectStream():void
{
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
var video:Video = new Video();
video.attachNetStream(stream);
stream.play("endesa/20130422/video_full_es_v2");
addChild(video);
}
}
}
I really think that the problem here is that OSMF it's not slicing the URL correctly and it's trying to connect (NetConnection) or trying to play (NetStream) a bad String.
EDIT:
Solved removing "/vod" in the URL.
You could try it here:
http://osmf.org/dev/2.0gm/debug.html?wmode=direct&width=470&height=320&src=rtmp%3A%2F%2Ffl.world-television.com%2Fstreamstudio%2Fendesa%2F20130422%2Fvideo_full_es_v2
I have a Flash Barcode scanner (camera) and want to use it in a mobile project to scan QR-Codes. It would be nice that it is possible to re-use this SWF and embedded it into a mobile Flex application. The SWF is made in Flash CS5.
So far, embedding (and add it to the stage and showing it) is successful but how do i communicate with the SWF? For example calling a function of it or by using events.
Here is a code snippet:
[Embed(source="../cam/cam.swf")]
private var cam:Class;
....
....
public const EVT_SNAPSHOT : String = "onSnapShot";
public var camera : Object;
public function onInit(e:Event) : void
{
this.camera = new cam();
this.camera.addEventListener(Event.ADDED_TO_STAGE, this.cameraInit );
this.stage.addChild( this.camera as DisplayObject );
}
private function cameraInit(e:Event):void
{
trace( 'Added to stage' );
this.stage.addEventListener( EVT_SNAPSHOT, this.cameraDoScan ); // does not bind?
trace( this.camera.hasOwnProperty('getAppInfo') ); // shows 'false'
}
private function cameraDoScan(e:MouseEvent):void
{
trace('MouseClick!');
}
Does anyone know to communicate with this 'thing'?
The most functional way to use external swf module is to load it into current ApplicationDomain, so you will have access to all classes contained in this loaded swf:
package
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
import flash.utils.getDefinitionByName;
public class astest extends Sprite
{
[Embed(source="/../assets/art.swf", mimeType="application/octet-stream")]
private static const art:Class;
public function astest()
{
var artBytes:ByteArray = new art() as ByteArray;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onArtLoaded);
loader.loadBytes(artBytes, new LoaderContext(false, ApplicationDomain.currentDomain));
}
protected function onArtLoaded(e:Event):void
{
var domain:ApplicationDomain = ApplicationDomain.currentDomain;
if(domain.hasDefinition("welcome_view"))
{
var moduleClass:Class = domain.getDefinition("welcome_view") as Class;
var module:Object = new moduleClass();
//module.moduleFunction();
addChild(module as DisplayObject);
}else
{
trace("loaded swf hasn't class 'welcome_view'");
}
}
}
}
I have an AIR App I'm working on and need to load a swf(always from localhost) which will access some methods in it's parent and vice versa. I'm not concerned with opening gaping security holes in a desktop app though. I've been searching all over but keep hitting walls with each implementation out there.
My current setup loads in the swf and it plays but I get a small error from the sandbox since I'm not in the same one as the app. Does anyone know how to get past this error so there is complete freedom opened up between the AIR app and the swf?
* Security Sandbox Violation *
SecurityDomain 'http://localhost/test.swf' tried to access incompatible context 'app:/Test_Player.swf'
public function loadSWF():void {
//var context:LoaderContext = new LoaderContext();
//context.checkPolicyFile = true;
//context.applicationDomain = ApplicationDomain.currentDomain;
//context.securityDomain = SecurityDomain.currentDomain;
var req:URLRequest = new URLRequest(swfURL);
adLoader = new Loader();
videoCanvas.rawChildren.addChild(adLoader);
loader.contentLoaderInfo.addEventListener(Event.INIT, adLoadedHandler, false, 0, true);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError, false, 0, true);
//loader.load(req, context);
loader.load(req);
}
You need to load your distant SWF with an UrlLoader and then reload it through loadByte. With this method you will by pass security.
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
public class TestDistantSwf extends Sprite
{
private var _urlLoader : URLLoader = new URLLoader;
private var _loader : Loader = new Loader;
public function TestDistantSwf()
{
addChild(_loader);
// Won't work
//_loader.load(new URLRequest("http://localhost/test.swf"));
// Load it as binary
_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
_urlLoader.addEventListener(Event.COMPLETE, onLoad);
_urlLoader.load(new URLRequest("http://localhost/test.swf"));
}
private function onLoad(e : Event) : void
{
// Load distant swf data locally
_loader.loadBytes(_urlLoader.data, new LoaderContext(false, ApplicationDomain.currentDomain));
}
}
}
If you need to pass arguments like flash var, two way to do it, if use Air 2.6 or later, you can use LoaderContext.parameters :
private function onLoad(e : Event) : void
{
var lc : LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
lc.parameters ={
foo:"hello !"
};
lc.allowCodeImport = true;
// Load distant swf data locally
_loader.loadBytes(_urlLoader.data, lc);
}
And then get it with loaderInfo.parameters in your loaded SWF.
Or you can call a function of loaded Swf :
private function onLoadBinary(e : Event) : void
{
e.target.content.init("hello 2 !");
}
private function onLoad(e : Event) : void
{
var lc : LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
lc.allowCodeImport = true;
// Load distant swf data locally
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadBinary);
_loader.loadBytes(_urlLoader.data, lc);
}
This will call from loaded swf in its main class:
public function init(foo : String) : void
{
trace(foo);
}
Is there a way you can use as3 to access iPad's camera?
I mean start the Camera within the app itself
have ability take a shoot and save the image to byteArray and applying the image to the background or doing some manipulation
I have done some research most of them just showing how to access the android devices.
Thanks for any suggestion or help.
Yes, you can absolutely do this. The beauty of Flash is that the code to do it is the same that you would use on Android or a PC.
Literally, you can do this to connect the camera to a Video object:
var camera:Camera = Camera.getCamera();
var video=new Video();
video.attachCamera(camera);
this.addChild(video); // 'this' would be a Sprite or UIComponent, etc...
There's a lot more to do if you want to do something useful, but it's fairly straight forward once you get started :)
bluebill1049, I'm not certain from the thread if you got what you were looking for, but I did see your request for the whole class. I found the same information (that Jason Sturges posted in his answer) in this post.
take photo using Adobe Builder (flex) for iOS
Unlike his reply here, his reply to that post had had a link to a great tutorial on building a mobile app and it was from that tutorial that this code was lifted/quoted. It requires an event class (event.CameraEvent - only a few lines) that's contained in that project/tutorial so it's important to be able to go back to the source, as it were. That source is located here:
http://devgirl.org/files/RIAUnleashed/
My thanks to Jason. Just so you don't have to dig, here's the event class that's missing from the quote:
package events
{
import flash.events.Event;
import flash.filesystem.File;
public class CameraEvent extends Event
{
public static const FILE_READY:String = "fileReady";
public var file:File;
public function CameraEvent(type:String, file:File=null, bubbles:Boolean = true, cancelable:Boolean = true)
{
super(type, bubbles, cancelable);
this.file = file;
}
}
}
Hope that helps!
Using the loader is not the only way to access the image bytes on iOS. It turns out the data is already in JPEG format to begin with, so encoding it again is not necessary.
Just do a mediaPromise.open() to get at the bytes and save them directly instead.
XpenseIt example code offers this camera implementation:
Class: CameraUtil:
package utils
{
import events.CameraEvent;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MediaEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.media.CameraRoll;
import flash.media.CameraUI;
import flash.media.MediaPromise;
import flash.media.MediaType;
import flash.utils.ByteArray;
import mx.events.DynamicEvent;
import mx.graphics.codec.JPEGEncoder;
[Event(name = "fileReady", type = "events.CameraEvent")]
public class CameraUtil extends EventDispatcher
{
protected var camera:CameraUI;
protected var loader:Loader;
public var file:File;
public function CameraUtil()
{
if (CameraUI.isSupported)
{
camera = new CameraUI();
camera.addEventListener(MediaEvent.COMPLETE, mediaEventComplete);
}
}
public function takePicture():void
{
if (camera)
camera.launch(MediaType.IMAGE);
}
protected function mediaEventComplete(event:MediaEvent):void
{
var mediaPromise:MediaPromise = event.data;
if (mediaPromise.file == null)
{
// For iOS we need to load with a Loader first
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleted);
loader.loadFilePromise(mediaPromise);
return;
}
else
{
// Android we can just dispatch the event that it's complete
file = new File(mediaPromise.file.url);
dispatchEvent(new CameraEvent(CameraEvent.FILE_READY, file));
}
}
protected function loaderCompleted(event:Event):void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
if (CameraRoll.supportsAddBitmapData)
{
var bitmapData:BitmapData = new BitmapData(loaderInfo.width, loaderInfo.height);
bitmapData.draw(loaderInfo.loader);
file = File.applicationStorageDirectory.resolvePath("receipt" + new Date().time + ".jpg");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
var j:JPEGEncoder = new JPEGEncoder();
var bytes:ByteArray = j.encode(bitmapData);
stream.writeBytes(bytes, 0, bytes.bytesAvailable);
stream.close();
trace(file.url);
dispatchEvent(new CameraEvent(CameraEvent.FILE_READY, file));
}
}
}
}
I'm having an issue with flash, which I am not really familiar with. I'm basing this code off of what came with the wowza media server in the video chat example, but unlike that example flash is not prompting me for whether or not to allow the video camera.
Below is my actionscript:
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.media.Camera;
import flash.media.Microphone;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.system.Security;
import flash.system.SecurityPanel;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.StatusEvent;
public class QandA extends Sprite {
Security.LOCAL_TRUSTED;
private var nc:NetConnection = null;
private var camera:Camera;
private var microphone:Microphone;
private var nsPublish:NetStream = null;
private var nsPlay:NetStream = null;
private var videoCamera:Video;
public var prompt:TextField;
public function QandA():void {
stage.align = "TL";
stage.scaleMode = "noScale";
videoCamera = new Video(160,120);
addChild(videoCamera);
camera = Camera.getCamera();
microphone = Microphone.getMicrophone();
if (camera.muted) {
trace("Camera Muted");
Security.showSettings(SecurityPanel.CAMERA);
camera.addEventListener(StatusEvent.STATUS, statusHandler);
} else {
startCamera();
}
}
private function statusHandler(e:StatusEvent):void {
if (e.code == "Camera.Unmuted") {
trace("Camera Unmuted");
startCamera();
camera.removeEventListener(StatusEvent.STATUS, statusHandler);
} else {
trace("StatusEvent: " + e.code + " " + e.toString());
}
}
private function startCamera():void {
// here are all the quality and performance settings that we suggest
camera.setMode(160, 120, 12, false);
camera.setQuality(0, 75);
camera.setKeyFrameInterval(24);
microphone.rate = 11;
microphone.setSilenceLevel(0);
nc = new NetConnection();
nc.connect("rtmp://localhost/live/");
// get status information from the NetConnection object
nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);
}
private function nsPublishOnStatus(infoObject:NetStatusEvent):void
{
trace("nsPublish: "+infoObject.info.code+" ("+infoObject.info.description+")");
}
private function ncOnStatus(infoObject:NetStatusEvent):void
{
trace("nc: "+infoObject.info.code+" ("+infoObject.info.description+")");
nsPublish = new NetStream(nc);
nsPublish.addEventListener(NetStatusEvent.NET_STATUS, nsPublishOnStatus);
nsPublish.bufferTime = 0;
nsPublish.publish("testing");
// attach the camera and microphone to the server
nsPublish.attachCamera(camera);
nsPublish.attachAudio(microphone);
}
}
I'm fairly confident it's something simple; as I've seen this code in/on countless sites when discussing how to publish to a live server.
Any help would be greatly appreciated, I've attempted using this code on a webserver to see if it was simply local security settings, but that was not the case.
Logs I receive when debugging the application in Flash CS5:
Attempting to launch and connect to Player using URL D:\development\qanda\qandaHost.swf
[SWF] D:\development\qanda\qandaHost.swf - 3583 bytes after decompression
Camera Muted
nc: NetConnection.Connect.Success (Connection succeeded.)
nsPublish: NetStream.Publish.Start (Publishing testing.)
Below is wrong:
Security.showSettings(SecurityPanel.**CAMERA**);
You should write:
Security.showSettings(SecurityPanel.**PRIVACY**);
I wasn't attaching the camera to the video, thus I couldn't see myself -- even though the video was in fact streaming.
private function startCamera():void {
trace("Attempting to start camera");
// here are all the quality and performance settings that we suggest
camera.setMode(160, 120, 12, false);
camera.setQuality(0, 75);
camera.setKeyFrameInterval(24);
videoCamera.attachCamera(camera);
microphone.rate = 11;
microphone.setSilenceLevel(0);
}