02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Dealing with HTML5 Media Errors

What happens when a video can't load? When the server is down or when you try to load a video or sound track that the browser doesn't support. In this video we find out how to deal with this type of error.

Video Transcript

00:00Welcome back. So until now we worked with the sound and videos that always work. We
00:05assumed they always work. We did only check once to make sure that we were sending the
00:10browser the right configuration. But what happens if a browser does support mp3 for
00:15example but our file just doesn't work? If it's because our mp3 isn't a new codec
00:21or there's some sort of media error network like network aborted the file or there's
00:27a partway network issue or there's a corrupt a corruption or unsupported
00:31feature. How do we deal with those type of scenarios? And that's exactly what this
00:36core this video is all about. Now before we see the actual interface inside of
00:41JavaScript how do we get the error code? I wanted just to make sure that you guys
00:45know how to do it in both ways both in HTML and also without HTML. As you can
00:50see currently in our error document we don't have any object embedded into the
00:55HTML. All our JavaScript is in this error.js file. We've never added the new
01:00audio into the document. So I'm going to go to the audio formats from sample 0 to
01:05and just add an extra on error event. And the way we add the event is like with
01:10every other event is we just type on error and then we type in the
01:14JavaScript code we want inside of the the value that comes in between. Now we
01:19could put the on error as well to specific sources and what that means if
01:23we put it on the audio object it will capture all errors that are happening on
01:27the level of the audio. Meaning that if there's an error for example in the mp3
01:32format or in the og format it will bubble up into the audio object and be
01:37captured in the on error. Now if we only wanted to worry for example maybe
01:42you're building an application that's going to be working for different Apple
01:46devices and you only care about mp3 because you only care about Safari and
01:49you don't care about other elements and you don't want the
01:54capturing of the errors to happen in other scenarios you could actually move
01:57your on error message and just put it specifically only to specific file
02:01formats. So for example in this case only if we'll run the mp3 file then we'll
02:06listen to the errors and then we could process them in that specific way.
02:09All right but we're not going to worry about that and we're not going to worry
02:13about the HTML and instead of that I'm just going to close that down and we're
02:16going to go right into our new source file that continues on from sample 005
02:22into 006 and I want you to see how we could do this basically completely
02:26dynamically. Now the way we add the event is very similar to the way we added the
02:31update time. We'll just type our sound object and add a new event listener and
02:35we'll just call the error event. Whenever an error happens we will
02:41call an on error function, an on error callback and in this on error
02:47callback we'll accept an event and this event will give us the information of the
02:52error. Now before we continue any further I want us to review basically the error
02:56message that is coming back console. Now currently and this information might
03:02change over time and really because we're living in dynamic times right now all
03:05I'm doing is is outputting the log of this event. Now I want you to see
03:10what happens if we run it currently in Safari and we click on refresh. Well we're
03:17not getting any error. Well the reason why we're not getting any error is because
03:20our music is working. We're not having any network issues. So let's
03:24create an issue. Let's go back into our source files and let's maybe change the
03:28file path. So maybe instead of sounds let's just change it to songs. So
03:35let's change it to songs folder which doesn't exist. Now if we go back into
03:38our file and refresh again we should get our error message and here we go we
03:42got our event. Now if we open up our event we'll notice something strange. Our
03:46target element is coming back as undefined meaning that we don't have a
03:50sound object or it's not connected to a sound object. Now the reason is because
03:55in Safari at least currently if we don't add our sound object to the
03:59stage it doesn't recognize it. It doesn't accept it once we're working
04:03with errors. Well if we test this out on a browser such as Safari such as
04:08Firefox currently and click on refresh we'll see that our error object does
04:13contain our information and not only that it does also contain our target
04:17which is our audio object. So although we we want to avoid this type of
04:22inconsistency so what we could do to solve this problem is literally just go
04:26back into our file making sure we actually add our object and add a sound
04:31object into our document. So I'm just going to figure out where I'm going to
04:35put it. I'm going to put it inside directly inside of our controls because
04:38it's invisible anyways. So I'm going to hit my document dot get element by ID and
04:45I'm going to get my control. I think it's controls right? Controls perfect. Once
04:51we're in the controls I'm just going to append a child and I'm going to add
04:57our sound object. Once I save this and I've added our object we could go back
05:01into Safari and this time around when we click on refresh and notice before
05:05we refresh the element is returning as null. It's not finding our
05:09object. While I click on refresh and our error happens now that it is on
05:14the stage you'll see that we actually do have a target which is our audio
05:17element and notice how Safari has to have this sound object on the stage
05:23for it to be able to interact with it currently. We don't know what will
05:26happen in the future but this is how you troubleshoot to figure out when bugs
05:30are happening. It's really important to test it out in various browsers. Alright so
05:35now that we did get our audio element let's start talking about the error
05:38object itself. So our element itself our sound object actually contains and
05:43we're going to get to our sound object by clicking on the target. Now if you
05:47didn't want to add the element onto the stage you could have still just
05:51approached your sound object directly. So if we wouldn't add this element
05:55onto the stage I would have to type SND and then go into our element. But
06:01because we want to keep consistent and I kind of like keeping our code
06:04anonymous once we're inside of events and the reason is is because so we could
06:08use the same events in the future for other objects. So when we're in our
06:13target I want to approach our error object and this error object will be
06:17empty if there is never an error it would return null but if there was an
06:20error then it's going to fill up with an object containing both the codes the
06:25base codes of the media error types which are going to be static codes
06:28and O2 contain a code a code property that will tell us which specific code
06:33error is in our is our problem. Right so let's just look at that object for a
06:38second I'm going to save it and go back into our browser and I'm going to click
06:42on refresh and this time around I'm going to get that media error directly
06:46and if we look into the error we'll see that we were got we're getting the
06:49code for which is our actual error. If we try to dig into our prototype we'll
06:53see the different types of media formats that are supported and you can
06:57see our error is error number four which is the media error source not
07:01supported or in other words in our scenario our source file is invalid well
07:06there are three other variations of three other codes so let's go through
07:10those codes really quickly get an understanding of how they work and what
07:14they are so I'm just going to create a switch controller a switch statement and
07:20in that switch I'm just going to put in our object so e.target.code
07:25oh.error.code and we don't need this anymore and in our switch itself I'm
07:32just going to create the different cases that exist which are basically case e.target.error
07:40and by the way as this is getting really long what we could do is create
07:45a new variable er and just create a shortcut just to make it less code on
07:52our stage so I could now type er instead of this is long paragraph and then we
07:57could more easily access the information so I'm just going to type and I already
08:01pre-copied the file name so I don't have to type them each time so the first one
08:06is media error aborted which its code itself is one now this happens
08:11whenever there was some sort of abortion or aborted the operation that
08:16for some reason has been up aborted or by the user or by the server midway
08:21while the file was being running suddenly it stops working the next case scenario
08:27is our er media error network which basically is similar but different this
08:34time around there's a server wait network issue maybe the internet
08:37connection that disconnected while we were still connected well this one is
08:42an abrupt disconnection usually user-defined this one is a
08:46disconnection such as maybe your network got disconnected or maybe the
08:50server is down of the of the source where you're running the file so this is
08:55some sort of a partway network issue case number four in case number three on
09:01the other hand and by the way I'll just put here the numbers just so they're
09:04very clear so this is aborted partway network issue is to let's move on to
09:09the next error and the next error code is media error decode now media error
09:15decode is is a interesting one and an important one as well and this one
09:19means is that a corruption was found in the file or the server just doesn't
09:23support the features so this happens when there's a career corruption
09:28problem or not supported feature right so there is a support if the feature is not
09:39supported that would happen now fourth case which is our case lucky for us case
09:44number four is when we are basically when the server just can't find the
09:48file or it's just an or there's some sort of major error issue before the
09:52network issue before the file even starts loading now because in our case we're
09:57not really we don't really care what type of error happens we just want to
10:01make sure that whenever an error happens that we capture it and let the user know
10:04that their media doesn't work although what we might do in a real-world
10:08application we might send ourselves maybe an email letting us know that there's
10:12some sort of network issue we couldn't we could log it some somewhere in our
10:16server making sure that we can make sure that if this is happening constantly
10:20that we could take care of it or maybe we just want to load maybe a backup video
10:24that says we're currently having issues please come back later alright so case
10:29number four was not supported completely or network issue alright so we
10:36said we have one two three four in our scenario we really aren't planning on
10:40doing anything specific to make it work but what we could do is maybe create
10:45also a default default state and in our default state we might want to whenever
10:52there's an error that we don't know which type it is if it's a future code
10:55net number that hasn't been defined yet or some error that came back without a
10:59number so in that case we might want to do another item so what I want to do
11:04now is how about we output the error itself to the user itself so I'm just
11:08going to go right ahead underneath our switch code and I'm just going to
11:11create the code for that so first of all I'm going to go right ahead into
11:14my controls but this time around I'm just going to swap the content of our
11:17controls because our controls have no meaning anymore and I'm just going to
11:21I'm going to swap the code to error obviously this copy doesn't make sense
11:27so how about we add maybe another variable here error message error let's
11:33do a message and by default we won't put anything in it and each error code that
11:38comes inside we could maybe a add that value that we're just talking about so
11:42obviously this is not user-friendly but we will use it anyway so I'm just going
11:46to go quickly with that breakout because we don't want our messages to continue
11:51once we have one message we're good with it and we want out of our switch so in
11:57this case we're going to grab this one as well and let me just quietly run
12:00through all them all right we're almost there and last but not least is our
12:07last default element and in our default we will put an error message how about
12:11we put an error message saying an unknown error occurred and I'm just
12:15going to paste that message because I have that saved on the side an unknown
12:19error occurred and there we go so we saved all the four different error
12:23formats that could happen all that's left for us is to test it out what we're
12:27expecting is to see this copy instead of our player once we click on refresh
12:30because there was some sort of network issue so basically our controllers would
12:34disappear so I'm going to click on refresh and here we go we're having an
12:37error obviously the reason why we're not getting the full text is because I
12:40forgot to actually output the actual text instead of outputting the just copy
12:46of error over so if we paste that right there we'll click on save we go back
12:51into our browser into Safari we click on refresh support in that network issue
12:56obviously we would want to put here a copy that's a little bit more
12:59descriptive if we just the same in Safari this time around we'll see that
13:03Ozone Safari if the error occurs we'll see that there's an error and then we
13:08could take over obviously we'd want to put a more user-friendly message there
13:11we could even maybe load a different file format maybe we could default back
13:15into flash it or another format that would probably be supported so if your
13:21HTML5 implementation doesn't work then you could backtrack back into flash or
13:25other format which would which would need a plugin all right so there we go
13:30that's our conversation about errors I hope you enjoyed it and learned a lot
13:33from it and we'll see you in the next video when we learn something new

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!