1 00:00:05,080 --> 00:00:08,560 In the last video, we got our YouTube app to play a video. 2 00:00:08,560 --> 00:00:13,330 There are a couple of improvements we can make to the app though, three in fact, but we're going to focus on the two 3 00:00:13,330 --> 00:00:15,660 obvious ones to start with. 4 00:00:15,660 --> 00:00:20,440 Now at the moment we have to press the play icon to start the video playing, and it would be neater 5 00:00:20,440 --> 00:00:24,130 if the video started playing automatically when the app starts. 6 00:00:24,130 --> 00:00:29,500 And we're also going to fix the problem that happens when you rotate the device while a video is playing. 7 00:00:29,500 --> 00:00:36,040 So let's start by reviewing the problems. We're going to run the app 8 00:00:36,040 --> 00:00:41,490 Alright so as you can see, the video doesn't autoplay, and basically won't start until I come over here and click 9 00:00:41,490 --> 00:00:42,780 on the play icon. 10 00:00:42,780 --> 00:00:51,400 So there's that, but also when I rotate the device which I'll do now, you can see that the video stops, 11 00:00:51,400 --> 00:00:56,920 and it's actually cued at the right position, you can see here that it has progressed, and when I play it does start 12 00:00:56,920 --> 00:00:59,260 at the same place, it doesn't start at the start of the video, 13 00:00:59,260 --> 00:01:02,560 however it doesn't play automatically and it would be good if we could actually get that to happen, 14 00:01:02,560 --> 00:01:06,050 to get it to automatically play. 15 00:01:06,050 --> 00:01:07,950 So let's have a look at fixing those problems, so 16 00:01:07,950 --> 00:01:11,820 back to the code. In the onInitializationSuccess function, 17 00:01:11,820 --> 00:01:15,500 we're calling this cueVideo function of our YouTubePlayer. 18 00:01:15,500 --> 00:01:19,070 So let's actually see exactly what that does and also what else is available. 19 00:01:19,070 --> 00:01:22,160 So I've still got the documentation open so I'll just go back to a browser, 20 00:01:22,160 --> 00:01:26,000 so go back to there and then click on YouTubePlayer in the left hand menu, 21 00:01:26,000 --> 00:01:32,620 over here. And that gives us a list of the various functions that are available. OK, and 22 00:01:32,620 --> 00:01:35,860 if I scroll down here down to public methods, 23 00:01:35,860 --> 00:01:42,830 and that's the list of the various functions that are actually available to us, and having a look at cueVideo, 24 00:01:42,830 --> 00:01:48,550 this one here, loads the specified video's thumbnail and prepares the player to play the video, but does not download 25 00:01:48,550 --> 00:01:55,660 any of the video stream until the play function is called. So to do that we need to call play, 26 00:01:55,660 --> 00:02:00,580 but I'm not going to do that because it won't work, and this can actually be a source of frustration. 27 00:02:00,580 --> 00:02:04,270 The documentation says you have to do something, called play in this case, 28 00:02:04,270 --> 00:02:07,930 however that doesn't work. It seems to be a timing issue. 29 00:02:07,930 --> 00:02:11,500 So we cue the video in the onInitializationSuccess function, 30 00:02:11,500 --> 00:02:15,880 but if we play too quickly, the video hasn't finished loading. 31 00:02:15,880 --> 00:02:19,930 So there's actually two things I suggest you do if you hit problems like this. First, 32 00:02:19,930 --> 00:02:25,900 add a lot of logging to all the functions, and that includes the callback functions that we haven't used. The logcat 33 00:02:25,900 --> 00:02:30,790 will then show the order that everything's happening which can provide useful information. 34 00:02:30,790 --> 00:02:36,900 Now what you'll find is that we'll be trying to call play, before the onLoaded callback's been received, 35 00:02:36,900 --> 00:02:42,430 the one in the playerStateChangeListener. So obviously the video can't play if it hasn't finished loading. 36 00:02:42,430 --> 00:02:44,140 So that's the first thing. 37 00:02:44,140 --> 00:02:50,090 But secondly, importantly, make sure you read all the documentation. Now if you scroll down all of these methods 38 00:02:50,090 --> 00:02:51,970 down here, you'll eventually get to one, 39 00:02:51,970 --> 00:02:57,700 loadVideo, loads and plays the specified video, which is really what we want to do here. So to fix the 40 00:02:57,700 --> 00:03:03,460 first problem, and to make our video play when the app loads, we really just need to replace the call to cue 41 00:03:03,460 --> 00:03:08,470 Video with the call to loadVideo. So let's actually go back and see if that works. 42 00:03:08,470 --> 00:03:15,760 Go back to android studio, change this cueVideo call to dot loadVideo. 43 00:03:15,760 --> 00:03:21,400 Remember it gets, loads and plays a video for us, and we also need to add an else here as well. Alright 44 00:03:21,400 --> 00:03:22,290 so that should work, 45 00:03:22,290 --> 00:03:25,730 but before we test it we have to think about the second problem. 46 00:03:25,730 --> 00:03:29,850 Now when we rotate the device the player's restored on the screen. 47 00:03:29,850 --> 00:03:35,010 The play position is where we left it as well, but unfortunately it doesn't start playing. 48 00:03:35,010 --> 00:03:39,000 But hang on, if the play is restored on the screen, then wasRestored 49 00:03:39,000 --> 00:03:40,680 probably going to be true, and 50 00:03:40,680 --> 00:03:45,940 we're only doing something when it's false. So perhaps we should call play when the play is restored. 51 00:03:45,940 --> 00:03:46,890 So let's go ahead and do that, 52 00:03:46,890 --> 00:03:56,990 so we'll put an else clause in here and some, and a code block, and we'll try YouTubePlayer questionmark dot play, and 53 00:03:56,990 --> 00:04:01,530 call the play method like that, and that actually should fix both problems, 54 00:04:01,530 --> 00:04:05,610 but let's actually try that and see if it does do that. 55 00:04:05,610 --> 00:04:11,250 And we're going to see if the video starts automatically, number one, and then we'll try rotate again and 56 00:04:11,250 --> 00:04:17,279 see what happens. Alright, so you can see this time perfect, we've got the video playing, 57 00:04:17,279 --> 00:04:24,250 whereas before we had to click on play, and now I'm going to rotate it into landscape and see what happens. 58 00:04:24,250 --> 00:04:29,330 And that's also good because it's actually played from the position, and just to be completely clear about that, 59 00:04:29,330 --> 00:04:41,550 let's advance the play head to about the one minute 17 mark, give it a second to buffer. 60 00:04:41,550 --> 00:04:44,270 Alright so that's working good as well. I didn't actually have to play, 61 00:04:44,270 --> 00:04:47,060 I didn't have to click play, it automatically played, 62 00:04:47,060 --> 00:04:52,760 but if I go back now to portrait mode again, we can see that's also still working, so no matter of when I 63 00:04:52,760 --> 00:04:56,820 rotate the device, in what mode, 64 00:04:56,820 --> 00:05:00,610 the video's playing without me having to actually click on the play button. 65 00:05:00,610 --> 00:05:05,850 So that's awesome, that's what we were trying to achieve here, so I'll just stop that now. 66 00:05:05,850 --> 00:05:09,940 And incidentally don't use Toast messages for diagnosing problems. 67 00:05:09,940 --> 00:05:14,470 Now even with the short duration, the Toast messages hang around for a while, so they actually get 68 00:05:14,470 --> 00:05:16,260 out of sync with what's happening. 69 00:05:16,260 --> 00:05:21,890 Toast messages are really just for you users, and we should be using logging in all those functions ourselves 70 00:05:21,890 --> 00:05:27,220 for debugging purposes. And just to confirm that problem with the Toast messages again, if I start 71 00:05:27,220 --> 00:05:32,200 actually doing that and start running it, and starting and pausing videos and having a play, 72 00:05:32,200 --> 00:05:48,080 you'll see that the messages will get out of sync. So let's just try that, 73 00:05:48,080 --> 00:05:57,060 and I've stopped now, as you can see those messages, the Toast messages are still trying to catch up. 74 00:05:57,060 --> 00:06:04,440 And you can see there it was quite some time after pressing pause, and we're still getting caught up on Toast messages. 75 00:06:04,440 --> 00:06:09,780 So obviously not a good way to do any form of debugging, you really want to use logcat for that. Alright, 76 00:06:09,780 --> 00:06:15,670 so I'm going to stop the video here. In the next one we're going to add to the app, to let it handle playlists as well, 77 00:06:15,670 --> 00:06:20,190 and we're also going to see how we can start new activities to perform different functions, using 78 00:06:20,190 --> 00:06:24,920 a very important Android concept, intents. So see you in the next video.