Alternative to bitmapdata.draw for error - actionscript-3

I'm getting a
SecurityError: Error #2123: Security sandbox violation:
BitmapData.draw: file:///Users/.../project/bin-debug/Project.swf
cannot access rtmp://flash.project.com/project/. No policy files
granted access.
This happens at this line:
temporaryBitmap = new BitmapData(FlexGlobals.topLevelApplication.stage.width, FlexGlobals.topLevelApplication.stage.height, false);
temporaryBitmap.draw(DisplayObject(FlexGlobals.topLevelApplication.stage)); // this line
Is there a way to get around this error? From research I've done it appears to be related to the RTMP stream permissions. If that's the case then it's ok to not display the stream since I am only trying to find the dimensions of the container. So if there was a way to get a snapshot of the application, and show a black box where the video stream is, that's fine as well.
I'm creating a generic tool for developers so I won't know what content will or will not be available ahead of time. I have to handle both situations. I can imagine I will have to deal with this issue when loading images from different domains as well.

I couldn't find any other solution to tell if it would error or not so I wrapped the call in a try catch block like so:
try {
drawBitmapData(temporaryBitmap, DisplayObject(FlexGlobals.topLevelApplication));
}
catch (e:Error) {
// could not draw so don't draw
}
// continue
I had to put the call to draw into a separate call for it to not to throw the error but rather skip to the next section of code:
public function drawBitmapData(bitmapData:BitmapData, displayObject:DisplayObject, matrix:Matrix = null):void {
bitmapData.draw(displayObject, matrix);
}

Related

Error -999 when loading multiple webViews

The iOS app I am making can have multiple webViews loading the same url at the same time. Resulting in this error:
Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed.
(NSURLErrorDomain error -999.)" UserInfo=0x176b7bc0 {NSErrorFailingURLKey=https://example.com,
NSErrorFailingURLStringKey=https://example.com}
I read this happens when a new request is started before the old request has been completed. How do I prevent this from happening? Thanks
I spent weeks worrying about this error. I was getting it randomly while accessing web pages. In my case I put it down to pages being requested too quickly back to back as the web access was driven by a state machine in code and not by a user.
After much searching, in the end I found a few discussions which could not explain why the error was occuring, but it was felt that it was feature of UIWebView rather than something you should worry about. The guidance was to ignore it. I will see if I can find the article and update this answer later if I can find it.
I updated my code as follows, and so far have seen no ill effects at all since adding it. This would suggest it is almost a notification and anything which causes it seems to get corrected inside UIWebView. Hopefully this is the same in your case.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
VSSLog(#"Entry: error = %#",error);
// Added this based on net advice. Its a bogus error.
if ([error code] == NSURLErrorCancelled) {
return;
}
... Normal error handling code for proper errors.
}
I am not one for out of sight out of mind, but this I believe is one of those cases where it is ok.
Finally if you are using iOS8 only, you could try moving to use the new WKWebView rather than UIWebView.
Use the delegate methods. Determine which view fired the method then fire the next if you're looking to run them sequentially.
- (void) webViewDidFinishLoad:(UIWebView *)webview{
if ( webview == self.wView1 )
{
// stuff
} else if ( webview == self.wView2 ) {
// stuff 2
}
}
UITableViewDelegate Protocol Reference

Windows Phone: playing a recorded sound throws exception

I am using an AudioVideoCaptureDevice to record some sound from the microphone. I want to give the user feedback on what he recorded so I want to be able to play it.
When putting that sound into a Song and playing it via Microsoft.Xna.Framework.Media.MediaPlayer, I am getting an exception:
{System.InvalidOperationException: Song playback failed.
Please verify that the song is not DRM protected.
DRM protected songs are not supported for creator games.
---> System.InvalidOperationException: An unexpected error has occurred.
--- End of inner exception stack trace ---
at Microsoft.Xna.Framework.Media.MediaQueue.Play(Song song)}
Looking into the details of the song in the watch, the IsProtected() seems to cause issues. (If playback works, I get correctly that the song is not protected.) I am using AAC and ACM codecs, both give same results.
I can play the song after closing and opening the view again, but have not found any related initialization that would explain this.
I also tried copying the file, in case some process was still holding a lock on it, still, there is no improvement, same with isolated storage.
After not closing app but re-entering view the song plays without problems.
How can I directly play the recorded audio without any problems?
I used the AudioRecorder sample from http://independentinnovation.net/blogs/independentinnovation/archive/2012/12/11/Windows-Phone-8-Audio-Recording-using-Windows.Phone.Media.Capture.aspx .
(Note: right now this link is not working, domain expired.. anyway, I used someone else's code.)
It turns out, that in the Record() function, a stream is opened, that in the Stop() function is never properly flushed and disposed.
I don't understand how this could have caused the problem, since I did copy the resulting file, just to make sure that all handles are valid.
Still, old handling was definitely wrong and new handling is a lot cleaner, and, btw, works.
public async void StopRecording()
{
try
{
if (dev != null)
{
await dev.StopRecordingAsync();
dev = null;
// these 2 lines I had to add
await recordingStream.FlushAsync();
recordingStream.Dispose();
}
var length = new System.IO.FileInfo(fileName).Length;
System.Diagnostics.Debug.WriteLine("Recorded " + fileName + " with length " + length);
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
edit: to give credits, I found the answer I used here http://www.postseek.com/meta/9fddd403d0cde523a542be42b7a4b057

Confused by W3C geolocation example code — will it loop infinitely if a position can’t be found?

I am a bit concerned about the safety of this code example in the W3C Geolocation spec:
// Forcing the user agent to return a fresh cached position.
// Request a position. We only accept cached positions whose age is not
// greater than 10 minutes. If the user agent does not have a fresh
// enough cached position object, it will immediately invoke the error
// callback.
navigator.geolocation.getCurrentPosition(successCallback,
errorCallback,
{maximumAge:600000, timeout:0});
function successCallback(position) {
// By using the 'maximumAge' option above, the position
// object is guaranteed to be at most 10 minutes old.
// By using a 'timeout' of 0 milliseconds, if there is
// no suitable cached position available, the user agent
// will aynchronously invoke the error callback with code
// TIMEOUT and will not initiate a new position
// acquisition process.
}
function errorCallback(error) {
switch(error.code) {
case error.TIMEOUT:
// Quick fallback when no suitable cached position exists.
doFallback();
// Acquire a new position object.
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
break;
case ... // treat the other error cases.
};
}
function doFallback() {
// No fresh enough cached position available.
// Fallback to a default position.
}
What happens if the browser genuinely can't return a location fix - for whatever reason - and keeps timing out?
Surely the code will end up in an infinite loop with errorCallback being called over and over again.
I see the doFallback() call but that won't stop errorCallback being called repeatedly. Or will it?
error.code will return POSITION_UNAVAILABLE. The code in the spec is an infinite loop because I guess it is exactly what they want. Imagine a scenario where you are in navigation mode, moving through space. Sometimes, you go through a tunnel and you lost the network and are unable to get any position for a certain amount of time that you do not control. Then when out of the tunnel, it is good to be able to get the position automatically. In a scenario, where you need a one time value, the piece of code will not be useful, but then easy to modify to stop with an error message.
A possible error you may be having is that Google Chrome may not have support for Geolocation.
Try testing your webpage on multiple browsers and see how that helps you.

Try Catch With Sound Element into Flash CS5-as3

Hi All—anyone has any idea why this code doesn’t work?
With other load [example image] work perfect with sound...no :(
mySoundURL = new URLRequest(var+".mp3");
mySoundURLDefault = new URLRequest("default.mp3");
try{
sound.load(mySoundURL);
}catch(e:IOErrorEvent){
trace("Can't load sound: "+e);
sound.load(mySoundURLDefault);
}
this is the error I’m getting:
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error
Thanks and good day!
You do not use try/catch with loaders. Here's what you do instead:
sound.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorEvent);
sound.load(mySoundURL);
private function onIOErrorEvent(e:IOErrorEvent):void{
trace(e);
trace(e.message);
// ^ This will show you the file it tried to load and failed at
e.currentTarget.removeEventListener(IOErrorEvent.IOError, onIOErrorEvent);
e.currentTarget.removeEventListener(Event.COMPLETE, onComplete;
// ^ don't forget to clean up your listeners!
}
The reason for this is, as you've seen, the uncaught error does not tell you anything helpful, such as which sound failed to load, or what the URL you tried to contact was.
I think #Alexander Sobolev is on the right track with his first guess, based on the fact that you still get an error. Handling errors from loaders is just different than handling errors from synchronous code, and try/catch will not help you here.
If you want to try playing an alt sound on fail from the first one, you'd do that in the onIOErrorEvent function.
Other times you'd listen for IOErrorEvent are when using Loader to load a swf or image file
loader.contentLoaderInfo.addEventListener(IOErrorEvent....
Or a URLLoader
urlLoader.addEventListener(IOErrorEvent...
It is not much information in the post, i suggest to check following conditions:
1) Is your var+".mp3" a correct URI? Does it point to the existing file? Just try in the browser - should it find the file?
If you are pointing to the file in internet you should use notation like http://site.com/song.mp3
If you are trying to play a file on your hard drive you should use direct file address like C:/audio/song.mp3
2) If its ok with the URI than maybe file is not in mp3 format.
3) You can try to play with SoundLoaderContext like
var mySoundURL = new URLRequest(var+".mp3")
var context:SoundLoaderContext = new SoundLoaderContext(0, false);
snd.load(mySoundURL , context);

How to check for the FLV file existence before playing that using FLVPlayback in Action Script 3?

I'm very new to the Action Scripting, I'm using the FLVPlayback class to play my FLV files.
If I'm trying to play a FLV file which is not existed yet then I am getting a "VideoError: 1000" with message of Unable to make connection to server or to find FLV on server.
I want to check for the FLV file existence using the file URL or path, before playing that FLV by FLVPlayback. Can anybody please suggest a way to do that.
Thanks
The only way to catch the error safely is to listen for the fl.video.VideoEvent.STATE_CHANGE event and act accordingly. Here's a little code snippet on how to do so:
import fl.video.FLVPlayback;
import fl.video.VideoEvent;
import fl.video.VideoState;
var videoPlayer:FLVPlayback;
videoPlayer.addEventListener( VideoEvent.STATE_CHANGE, onVideoStateChange );
/** Bad source **/
videoPlayer.source = "http://www.helpexamples.com/flash/video/caption_video_error.flv";
/** Good source **/
//videoPlayer.source = "http://www.helpexamples.com/flash/video/caption_video.flv";
function onVideoStateChange( evt:VideoEvent ):void
{
var videoPlayer:FLVPlayback = evt.target as FLVPlayback;
switch( evt.state )
{
case VideoState.CONNECTION_ERROR:
trace( 'Connection error' );
/**
* Once you hit this event, you should run some logic to do one or more of the following:
* 1. Show an error message to the user
* 2. Try to load another video
* 3. Hide the FLVPlayback component
*/
break;
default:
trace( 'Player is: ' + evt.state );
}
}
For a full list of possible VideoState constants, visit fl.video.VideoState.
I think you may be able to make use of the stateChange event. One of the possible event types is VideoState.CONNECTION_ERROR and another is VideoState.DISCONNECTED which may also work.
Try giving that a shot.
If those don't work, the only way I can think of would be to either do a HEAD or GET request for the flv before you attempt to load it. Only a successful response would trigger the video loading through the normal method. I don't remember whether Flash supports HEAD requests, but if it does that would certainly be the better option.
If Flash does not support HEAD requests then you may be better off having a simple, server-side script that could verify the existence of the flv before you actually request if. That way you can use a simple GET request without having to retrieve the whole file.
INLINE THINKING
I am just thinking, another possible solution using GET would be to cancel the load as soon as bytesLoaded > 1K (for example), or something like that. As long as you are checking for a size greater than the 404 response you are getting, you should be able to assume the flv is being loaded.