Is there a reliable way to learn if a video file is valid? VideoPlayer does not detect empty/small size files as invalid ones - actionscript-3

I need to detect if video file is valid(and delete it if it is not). It is an air application.
For large files it can be accomplished by processing MediaPlayerState.PLAYBACK_ERROR state. No problems.
However, when there's an empty file like empty.mp4, it does not dispatch any PLAYBACK_ERROR.
Then I created a file singleChar.mp4 (with x letter inside), the behavior was exactly the same.
It goes through READY state without any problems. playing is true at that time (it is said in the documentation that it is true when it is playing or attempting to play, so this is also not reliable).
Then I suggested that it treats those files as valid ones with duration=0... No! Duration is not set, durationChange is not dispatched.
The best approach for now is to play() it, setTimeout for about 50ms and check duration when this timeout expires. At least it works in 50% cases. However, this is completely unreliable. Reliability may be improved by extending the delay, but I'd prefer to detect it quickly and handle silently before user will notice any problems.
Other possible approach is to check if the file is less than some size(maybe,100KB?), however, this is also another stupid unreliable approach.
So, is there a reliable way to detect if a file is a valid video file?

Related

Html5 web API AudioBufferSourceNode sound syncing

I have 4 wav files all designed to be played at the same time. I have created the 4 sound buffers and play them with the start function. All 4 sounds are played successfully but they are slightly out of sync with each other. I guess each call to start takes long enough for the subsequent call to be out of sync. Is there anyway I can force them to sync?
Thanks
Without seeing you code, these are the spontaneous ideas I have. Make sure to check the following:
Give the exact same start time to the start method (maybe save context.currentTime + a little bit in a var and pass it to the start method of each source node, don't pass context.currentTime to each source in a for loop or similar (the context time might update while that's executing, thus the time passed to each source is different)).
Make sure that the audio files are cut exactly the same in the beginning of the file. If you have 10 ms extra silence in one of the files, that file will always be 10ms off (unless you compensate for it in the code).
If none of that works/applies, please add some code to your question explaining what you're doing right now.

AS3 Starling custom shader crash "Error #3600: No valid program set" when computer is locked or put to sleep

I'm working on a Flash game using Starling, and I have started coming across an issue where my render crashes immediately after I put my PC to sleep/lock it and then turn it back on. The error I'm getting is:
Error #3600: No valid program set
I was not able to find any advice online on how to prevent this/re-initialize everything appropriately. I'm assuming this is part of a wider issue with how Flash/Starling handles the computer being put to sleep/locked.
Things I have tried so far:
Catching the error and re-uploading the shader programs.
Setting Starling.handleLostContext to true
Has anyone come across this issue before? Any help/pointers would be greatly appreciated.
It sounds like your game is losing the GPU context.
On context loss, all GPU data is lost. You have to restore textures, vertex buffers, etc. Starling handles some of it for you when you set handleLostContext to true, but you still have to handle the textures.
If you use the AssetManager, it will handle re-uploading textures for you, which is clearly the easiest way to go. It automatically creates a texture.root.onRestore callback function which Starling calls after it re-creates the context, and it will attempt to re-load your textures from wherever you first loaded them from, whether from disk, an embedded asset, or a URL.
You can also manually define the function yourself, though that is more complicated and as the article mentions, some gotchas are involved.
More information: http://wiki.starling-framework.org/manual/context_loss

AS3 Error #1502

AS3
Error: Error #1502: A script has executed for longer than the default timeout period of 15 seconds.
Is there a way to temporarily suppress this on a specific block of code?
I am creating a HUGE dynamic 3d array of objects, 1000x1000x1000 and need the build to actually finish the initializing.
Your best bet would be to try and refactor your code. Perhaps you can make use of this tutorial which deals with the exact problem you are having.
http://www.senocular.com/flash/tutorials/asyncoperations/
Increasing the timeout is one option, however I would also suggest considering an approach that would build your arrays over multiple frames, that is splitting the work up into separate jobs. As long as you give control back to the Flash Player every once in a while, you will not get this exception.
I'm not certain of the specifics of your problem, however you will need to find a way to parallelize or just simply segment your calculations. If your algorithm centers around one major loop, then consider creating a function that takes all of the arguments necessary to record the context of a single iteration. Then, create a simple control loop that will call this function and determine when to wait until the next frame and when not to. Leveraging AS3 closures can also help with this.
Look for the script execution time limit in the "Publish Settings" (Flash). If you're using Flex, maybe this one can be useful: http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_14.html (check default-script-limits, max-recursion-depth, max-execution-time). Oh! It seems there's apparently no way to make it behave in a different way on a specific piece of code (it is a global setting).
I do not approve the increse timeout option. Because for all this time your appllication is just hangs the whole Flash player. And normaly user thinks it is down, and forses it to quit.
check this one out: How to show the current progressBar value of process within a loop in flex-as3?
And then you can even show the progress which would be really more confident for you and for user.

Problems loading a NetStream Video

I have been creating a custom video player for the web. On some machines that I run this on it will start loading the .flv file then no progress will be made for 30 seconds to one minute then show that the video is completely loaded. I am checking how much has been loaded using a bytesLoaded / bytesTotal in an Event.ENTER_FRAME. When traced separately what seems to be happening when it shows fully loaded the bytesTotal value changes to the current bytesLoaded value causing my video player to register that my load percentage to be 1. I have traced out the NetStatus event.code value and there is no update to show that there has been any sort of error. All I get are a NetStream.Play.Start NetStream.Buffer.Full and then it will wait and reset the bytes total value.
So what I am asking is if there a way to handle this problem?
There doesn't seem to be a specific answer to this problem. I have since made another attempt at the problem and there are two lessons that I have learned.
1) You can code around most shortcomings in the netstream class by not allowing it to attempt to seek past the loaded point using the bytesLoaded, bytesTotal and the bufferTime properties. This can allow you to make sure that you never allow the seek to be attempted to a time that could cause a problem.
2) Always allow the previous seek attempt to finish and handle it properly before attempting to send another.
Are you playing an mp4 whose moov atom (e.g. metadata) is at the end of the file? If so, Flash cannot play the file until the entire file loads and is able to read the metadata.
This tool should fix your video file:
http://renaun.com/blog/code/qtindexswapper/

Garbage collection - manually wiping EVERYTHING!

I have been building a game for a while (nearly done) - But the game needs a replay button and its a big task.
I know the GC is dreadful in flash, but I wanted to know if there is a way to wipe EVERYTHING as if the flash app has just begun. Clearing memory, game data - I haven't to worry about game loading as its not really heavy on data.
Everything pretty much lives in a DataModel - but I fear if I just clear the all variables, I'll have pockets of orphaned memory.
Any forwarding idea would be great.
cheers guys.
I would do this:
make a class that encapsulates your entire game, called GameContainer or whatever.
Do a search on all your source code, and make sure that in every call to addEventListener, you are passing true for the "use weak references" argument.
In your document class (or frame script), make a single instance of GameContainer and add it to the stage, and do nothing else.
Now when you want to entirely clear your game from memory, remove GameContainer from the stage and null the reference. Memory will not immediately be released, but everything in your game will now be eligible for release. If Flash thinks it needs more memory it will trigger a GC, and the large orphaned GameContainer will be nuked. (Step 2 above will keep your event listeners from counting as references to your objects, and make sure that all self-contained objects are eligible for disposal.
Not sure what you mean about Flash's GC being dreadful though. I can't recall having heard of any bugs in it. It won't nuke your objects unless you are careful with your references, but that's true of all garbage collection.
Not short of refreshing the page. There might be some hack you could do by loading a separate swf and then unloading it, but that would be just as error prone as doing it the proper way.
My advice would be to just buckle down and write your reset function then get something to monitor memory and make sure it works by reiniting/resetting a bunch of times.
The problem with Flash's garbage collector is that as far as I know you cannot FORCE it to "collect". I had this issue with a program in which I would load in various external SWF's, and occasionally Flash would simply not load them. What you may need to do is make a function to set every major variable in your code to null at the end of a game, if you want a true reset.
There is still no guarantee, but unfortunately I'm fairly certain there is no shortcut to force a memory release.
(For me this meant also making sure my event handlers were cleared properly, and my loaders would "unload()" after passing their content off, in the case of my external SWF loader.)