I'm making a connection with netconnection in AS3 Flash CS6. It works fine when I test the swf from my PC to the app on my Android. But when I install the app on 2 android devices they don't connect. Why is that? If I test with my PC and the two android devices it works too. How can I make it work with only the two android devices?
Here's the code I'm using:
var nc:NetConnection;
var group:NetGroup;
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.connect("rtmfp:");
function netStatus(event:NetStatusEvent):void{
switch(event.info.code){
// The connection attempt succeeded
case "NetConnection.Connect.Success":
setupGroup();
break;
// The NetGroup is successfully constructed and authorized to function.
case "NetGroup.Connect.Success":
break;
// A new group posting is received
case "NetGroup.Posting.Notify":
trace("notify");
break;
case "NetGroup.SendTo.Notify":
trace("notify received");
break;
// user joins?
case "NetGroup.Neighbor.Connect":
break;
}
}
// Create a group
function setupGroup():void {
// Create a new Group Specifier object
var groupspec:GroupSpecifier = new GroupSpecifier("test");
// Enable posting
groupspec.postingEnabled = true;
//groupspec.routingEnabled=true;
// Specifies whether information about group membership can be exchanged on IP multicast sockets
groupspec.ipMulticastMemberUpdatesEnabled = true;
// Causes the associated NetStream or NetGroup to join the specified IP multicast group and listen to the specified UDP port.
groupspec.addIPMulticastAddress("225.225.0.1:30000");
// Constructs a NetGroup on the specified NetConnection object and joins it to the group specified by groupspec.
group = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
// Set the NET_STATUS event listener
group.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
}
I suggest adding a default case statement to your switch. It may or may not shed some light on what is happening:
switch (event.info.code)
{
case "NetConnection.Connect.Success":
// etc... Not showing orig code
break;
// rest of the case statements here
// then lastly add this:
default:
trace(event.info.code);
}
Related
I am developing a running tracker/pedometer app, I am using geolocator for the same,I am keeping the movement threshold property of the geoLocator to 10 here is my piece of code.
button click event
private void StartButton_Click(object sender, RoutedEventArgs e)
{
myLocator = new Geolocator();
myLocator.DesiredAccuracy = PositionAccuracy.Default;
myLocator.MovementThreshold = 10;
myLocator.ReportInterval=500;
myLocator.PositionChanged += myGeoLocator_PositionChanged;
_startTime = System.Environment.TickCount;
_timer.Start();
}
void myGeoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
var coord = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
if (_line.Path.Count > 0)
{
var previousPoint = _line.Path.Last();
distance += coord.GetDistanceTo(previousPoint);
var millisPerKilometer = (1000.0 / distance) * (System.Environment.TickCount - _previousPositionChangeTick);
_kilometres += Math.Round(distance, 2);
distanceLabel.Text = string.Format("{0:f2} meters", _kilometres);
MessageBox.Show("Changed");
}
else
{
Map.Center = coord;
}
_line.Path.Add(coord);
_previousPositionChangeTick = System.Environment.TickCount;
});
}
The problem is that the position changed event is only getting called once, I am trying to debug the code in emulator by changing the location points but still the event do not get called. where am I doing wrong??
Your code will work on a real device. However, in order to test on the emulator, try by setting the DesiredAccuracy property to High.
From How to test apps that use location data for Windows Phone:
If your app uses the GeoCoordinateWatcher class, you have to specify a value of GeoPositionAccuracy.High in the constructor or in the DesiredAccuracy property of the class before you can test your app with the location sensor simulator. If you leave the accuracy at its default value of GeoPositionAccuracy.Default, the PositionChanged event doesn’t recognize position changes that occur in the location sensor simulator.
There is also another workaround, that consists on running the native Maps app, which seems to fix the problem:
Set a current location in the emulator.
Run your app. It reports the current location as Redmond.
Run the Maps application. It correctly goes to the location set in
step 1.
Run your app again. Now it uses the correct current location.
Source: http://social.msdn.microsoft.com/Forums/wpapps/en-US/c2cc57b1-ba1f-48fb-b285-d6cfbb8f393a/windows-phone-8-emulator-returns-microsofts-location-only
I'm trying to stream a video from Amazon's CloudFront/S3 service. Despite my filename being correct, and a "NetConnection.Connect.Success" status, my NetConnection onBWDone callback is giving me an "undefined" error and the video doesn't play or show up anywhere on the stage. The connection reports that it's successful so I don't know where the problem lies. Here is my code:
var amazonCloudFrontDomain:String = "myCloudFrontDistributionID.cloudfront.net";
var amazonCloudFrontStreamURL:String = "rtmp://" + amazonCloudFrontDomain + "/cfx/st/";
var videoFileName:String = "myVideo.flv";
Security.allowDomain(amazonCloudFrontDomain);
Security.allowDomain("rtmp://" + amazonCloudFrontDomain);
Security.allowDomain(amazonCloudFrontStreamURL);
var client:Object = new Object();
client.onBWDone = function(e){trace("onBWDone: " + e);}
client.onMetaData = function(e){trace("onMetaData: " + e);}
client.onCuePoint = function(e){trace("onCuePoint: " + e);}
var video:Video = new Video(300, 400); // create a new Video item and set its width and height
video.x = 0; // position the video's x position
video.y = 0; // position the video's y position
var duration:Number; // use this later to get the duration of the video being played
this.addChild(video);
var nc:NetConnection = new NetConnection(); // variable for a new NetConnection
nc.client = client;
nc.addEventListener(NetStatusEvent.NET_STATUS,netConnectionStatusHandler,false,0,true);
nc.connect(amazonCloudFrontStreamURL); // set the nc variable to null
var ns:NetStream;
function netConnectionStatusHandler(e):void
{
switch(e.info.code)
{
case "NetConnection.Connect.Success":
trace("S3 Connected");
ns = new NetStream(nc); // create a variable for a new NetStream connection & connect it to the nc variable
ns.addEventListener(NetStatusEvent.NET_STATUS, netStreamStatusHandler); // add a listener to the NetStream to listen for any changes that happen with the NetStream
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, netStreamAsyncErrorHandler); // add a listener to the NetStream for any errors that may happen
ns.client = client;
video.attachNetStream(ns); // attach the NetStream variable to the video object
video.smoothing = true;
video.deblocking = 1;
ns.bufferTime = 5; // set the buffer time to 5 seconds
ns.play(videoFileName); // tell the netstream what video to play and play it
break;
}
trace(e.info.code);
}
function netStreamAsyncErrorHandler(Event:AsyncErrorEvent):void
{
// trace(event.text); // this will handle any errors with video playback
}
function netStreamStatusHandler(event:NetStatusEvent):void
{
trace(event.info.code); // this will handle any events that are fired when the video is playing back
switch(event.info.code) // switch statement to handle the various events with the NetConnection
{
case "NetStream.Buffer.Full": // when the buffer is full fire the code below
ns.bufferTime = 10; // set buffer time to 10 seconds break;
case "NetStream.Buffer.Empty": // when the buffer is empty fire, code below
ns.bufferTime = 10; // set buffer time to 10 seconds
break;
case "NetStream.Play.Start": // when the video starts playing, fire the code below
ns.bufferTime = 10; // set the buffer time to 10 seconds
break;
case "NetStream.Seek.Notify": // when you seek with the scrubber it sends a notify signal of the time
ns.bufferTime = 10; // set the buffer time to 10 seconds
break;
case "NetStream.Seek.InvalidTime": // when you release the scrubber ahead of the video that has been loaded, you get this error. it will jump you back to the last frame that has been loaded
ns.bufferTime = 10; // set the buffer time to 10 seconds
break;
case "NetStream.Play.Stop": // when you reach the end of the video
ns.pause(); // pause the video
ns.seek(1); // seek the video to the first frame
break;
}
}
This is the console output:
S3 Connected
netConnectionStatusHandler: NetConnection.Connect.Success
onBWDone: undefined
NetStream.Play.Reset
NetStream.Play.Start
...and nothing happens. No video, no audio, nothing. Can anyone see any problems with my code?
I believe that not all streaming applications have to use the onBWDone function, so if they don't use it, it probably passes null or undefined to the function instead of the kbps.
So you're problem is likely with the e in your trace statement below:
client.onBWDone = function(e){trace("onBWDone: " + e);}
Also, the function does not recieve an event object, but rather a ... rest array that usually only has one item in it. try this instead:
client.onBWDone = function(... rest){ //
if(rest && rest.length > 0){ //since the function might not get any data, you need to check before trying to trace it out
trace("onBWDone: " + rest[0]); //this is the kbps of the bandwidth available
}
};
You can learn more at the Adobe docs:
http://help.adobe.com/en_US/FlashMediaServer/3.5_Deving/WS5b3ccc516d4fbf351e63e3d11a0773d56e-7ffa.html
EDIT
In regards to your video path:
Flash Media Server applications are accessed in the following format over rtmp: "server:port/application/instance". For VOD, instance is the name of your file and for FLV's it doesn't require the extension.
http://help.adobe.com/en_US/FlashMediaServer/3.5_Deving/WS5b3ccc516d4fbf351e63e3d11a0773cfae-7ff3.html
I changed
var videoFileName:String = "myVideo.flv";
to
var videoFileName:String = "myVideo";
and now it works. For some reason, excluding the file extension makes it work for me. I don't know why...strange.
I currently have an SWF application that records audio and saves it to FMS. Then inside Flash Professional I'm trying to use a FLVPlayback Component to play the recording, but it never plays.
The recording is saved here: RootInstall/applications/myapp/streams/folder/audioFile.flv
Then I have my FLVPlayback component source looking at rtmp://server-ip/myapp/streams/folder/audioFile but I can't get it to play.
I've also copied the recored audio file and stuck it in the vod application but couldn't get it to play. When I tried the sample.flv videos, they worked.
Here's how I'm recording the audio in AS3:
nc.connect("rtmp://server-ip/myapp/folder");
...
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
mic = Microphone.getMicrophone();
ns.attachAudio(mic);
// start publishing
ns.publish("audioFile", "record");
Everything on the recording side was correct, it was the way I was trying to read the FLV was incorrect.
Based on the code above here's the player side I coded.
...
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmp://server-ip/myapp/folder");
private function netStatusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
//Connected Successfully
playLiveStream();
break;
case "NetConnection.Connect.Rejected" :
// Connection Rejected
break;
case "NetConnection.Connect.Failed" :
// The server may be down or unreachable
break;
case "NetConnection.Connect.AppShutDown" :
// The application is shutting down
nc.close();
break;
case "NetConnection.Connect.Closed" :
//Connection Closed
break;
}
}
private function playLiveStream():void {
nsPlay = new NetStream(nc);
nsPlay.client = this;
nsPlay.bufferTime = 0.1;
nsPlay.play("audioFile", -1);
}
I have a NetGroup established using Adobe Cirrus. All clients can connect fine and see each other, since I receive NetGroup.Neighbor.Connect and NetGroup.MulticastStream.PublishNotify events when a new stream is being published.
However, if a user subscribes to a published stream, the publisher does not receive a notification (no NetStatusEvent and no callback to the onPeerConnect method). The subscriber receives the stream without problems, though.
All other questions about the non working onPeerConnect method were related to NetStream.DIRECT_CONNECTIONS, but in my case, I am using a NetGroup.
What is wrong here?
// Only the relevant parts, a few things have been stripped (e.g. connect the netGroup only when the NetConnection has been established etc.)
var groupSpecifier:GroupSpecifier = new GroupSpecifier("group");
groupSpecifier.multicastEnabled = true;
groupSpecifier.postingEnabled = true;
groupSpecifier.serverChannelEnabled = true;
groupSpecifier.objectReplicationEnabled = true;
groupSpecifier.ipMulticastMemberUpdatesEnabled = true;
groupSpecifier.routingEnabled = true;
var netGroup:NetGroup = new NetGroup(netConnection, groupSpecifier.groupspecWithAuthorizations());
var netStream:NetStream = new NetStream(netConnection, groupSpecifier.groupspecWithAuthorizations());
netStream.client = {onPeerConnect:onPeerConnect};
netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
// Never gets called
public function onPeerConnect(netStream:NetStream):Boolean {
trace("onPeerConnect: "+netStream.farID);
return true;
}
private function onNetStatus(event:NetStatusEvent):void {
trace(event.info.code);
switch(event.info.code) {
case EventCodes.STREAM_CONNECT_SUCCESS :
netStream.attachCamera(camera);
netStream.attachAudio(microphone);
netStream.publish(streamName);
break;
}
}
onPeerConnect is only being called when using DIRECT_CONNECTIONS, not for NetGroups. Unfortunately, this is not mentioned in the documentation or anywhere else.
I am trying to publish the video of a webcam to a Flash Media Server 2.
My code is working in the Flash standalone player (tested with 10.0 and 10.2), but not in the browser plugin (tested with 10.2, both in IE and Opera.
The connection to my FMS is working successfully, but after the publish, nothing happens, I never get the NetStream.Publish.Start Event. On the server I can see the connection in the management console, even the stream in the streams tab. But I cannot connect to that strea.
Anybody an idea what could be going wrong?
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0; // needed to get correct connection to FMS2
private function netStatusHandler(event:NetStatusEvent):void {
output.appendText("\n" + event.info.code);
switch (event.info.code) {
case "NetConnection.Connect.Success":
output.text = ("Connection successful, streaming camera...");
connectCamera();
break;
case "NetConnection.Connect.Failed":
break;
case "NetStream.Play.StreamNotFound":
break;
case "NetStream.Publish.Start":
output.appendText("\nPublishing video!");
break;
}
}
private function connectCamera(ev:Event = null):void {
var stream:NetStream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.attachCamera(camera);
videoURL = createGUID();
stream.publish(videoURL, "live");
output.text = "publish stream...";
}
Ok, I found what my problem is here:
I declare the reference to the stream variable inside the connectCamera function:
private function connectCamera(ev:Event = null):void {
var stream:NetStream = new NetStream(connection);
}
So stream is only declared inside the scope of that function.
This doesn't seem to be a problem in the standalone player, but it is in the browser plugin. The browser plugin seems to do a much more thourough job on the garbage collector, garbage collecting my stream after the function has executed.
So what I have to do is declare the stream variable outside of the function scope, inside the class scope.
var stream:NetStream;
private function connectCamera(ev:Event = null):void {
stream = new NetStream(connection);
}
You should always declare stuff that you need later as a field on your class, and not inside a function. You just never know when GC might clean up that stuff.