Try Catch With Sound Element into Flash CS5-as3 - actionscript-3

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);

Related

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

Alternative to bitmapdata.draw for error

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);
}

as3 error says "Stream Error", but when loaded manually it's there

I've looked in several other questions, but it just doesn't seem to explain my error.
The error i'm getting is "Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http://plustg.com/v2/_inc/_swf/getSavedSelfy.php
at snapshot_fla::MainTimeline/frame1()[snapshot_fla.MainTimeline::frame1:20]"
When going to that URL, it does load. But somehow flash throws a stream error.
For reference, this is all there is on line 20: var selfyXMLLoader:URLLoader = new URLLoader();
And this is the rest of the code:
var selfyXMLLoader:URLLoader = new URLLoader();
var selfyXMLRequest:URLRequest = new URLRequest('http://plustg.com/v2/_inc/_swf/getSavedSelfy.php');
selfyXMLLoader.load(selfyXMLRequest);
selfyXMLLoader.addEventListener(Event.COMPLETE, loadSelfy);
Strange thing here is that when changing it to any other url on ANOTHER website, it will work.
Help would be great.
I think some authentication is needed to get the content of this page. I tried to access it and I was prompted with a login/password box and the message: “You need to be a beta tester to view this page.”
Maybe you've already authenticaded in your browser, that would justify the fact that you can access, but not the Flash.

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.

AS3 Error #2044. Once dissmissed the app works fine

I'm working in as3 and my code is producing an error: "Error #2044: Unhandled IOErrorEvent:. text=Error #2036: Load Never Completed."
Other then the fact that the app is producing this error and interrupting users who have a the debug version of the adobe flash player the app works just fine, I "Dismiss All" errors and
I've been debugging the program, stepping through one line at a time, trying to see exactly what it's failing to load and I can figure it out. From what research I've done it seems like the error is caused by urls being wrong or resources not being present, but as I've said, the app works exactly as expected.
here is a link to the app if you have debugger version of flash you'll see when it happens:
http://www.playmatics.com/parents_connect/
Assuming you are loading files off a server, I would suggest looking for server errors on file requests as this could be the source of your problem.
However, you might still want to properly handle the event thrown by the player in order to avoid the error from being displayed:
var loader:Loader = new Loader();
loader.addEventListener(IOErrorEvent.IOError, handleIOErrorEvent);
var urlReq:URLRequest = new URLRequest('srcURL');
loader.load(urlReq);
protected function handleIOErrorEvent(event:IOErrorEvent):void {
trace('handleIOErrorEvent: ' + event);
}
Hope this helps.
UPDATE
Not related to your problem... two files appear as not found – 404 errors – on your server:
http://www.playmatics.com/parents_connect/history/history.js
http://www.playmatics.com/parents_connect/history/history.css