In the last video, we got our YouTube app to play a video.
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
obvious ones to start with.
Now at the moment we have to press the play icon to start the video playing, and it would be neater
if the video started playing automatically when the app starts.
And we're also going to fix the problem that happens when you rotate the device while a video is playing.
So let's start by reviewing the problems. We're going to run the app
Alright so as you can see, the video doesn't autoplay, and basically won't start until I come over here and click
on the play icon.
So there's that, but also when I rotate the device which I'll do now, you can see that the video stops,
and it's actually cued at the right position, you can see here that it has progressed, and when I play it does start
at the same place, it doesn't start at the start of the video,
however it doesn't play automatically and it would be good if we could actually get that to happen,
to get it to automatically play.
So let's have a look at fixing those problems, so
back to the code. In the onInitializationSuccess function,
we're calling this cueVideo function of our YouTubePlayer.
So let's actually see exactly what that does and also what else is available.
So I've still got the documentation open so I'll just go back to a browser,
so go back to there and then click on YouTubePlayer in the left hand menu,
over here. And that gives us a list of the various functions that are available. OK, and
if I scroll down here down to public methods,
and that's the list of the various functions that are actually available to us, and having a look at cueVideo,
this one here, loads the specified video's thumbnail and prepares the player to play the video, but does not download
any of the video stream until the play function is called. So to do that we need to call play,
but I'm not going to do that because it won't work, and this can actually be a source of frustration.
The documentation says you have to do something, called play in this case,
however that doesn't work. It seems to be a timing issue.
So we cue the video in the onInitializationSuccess function,
but if we play too quickly, the video hasn't finished loading.
So there's actually two things I suggest you do if you hit problems like this. First,
add a lot of logging to all the functions, and that includes the callback functions that we haven't used. The logcat
will then show the order that everything's happening which can provide useful information.
Now what you'll find is that we'll be trying to call play, before the onLoaded callback's been received,
the one in the playerStateChangeListener. So obviously the video can't play if it hasn't finished loading.
So that's the first thing.
But secondly, importantly, make sure you read all the documentation. Now if you scroll down all of these methods
down here, you'll eventually get to one,
loadVideo, loads and plays the specified video, which is really what we want to do here. So to fix the
first problem, and to make our video play when the app loads, we really just need to replace the call to cue
Video with the call to loadVideo. So let's actually go back and see if that works.
Go back to android studio, change this cueVideo call to dot loadVideo.
Remember it gets, loads and plays a video for us, and we also need to add an else here as well. Alright
so that should work,
but before we test it we have to think about the second problem.
Now when we rotate the device the player's restored on the screen.
The play position is where we left it as well, but unfortunately it doesn't start playing.
But hang on, if the play is restored on the screen, then wasRestored
probably going to be true, and
we're only doing something when it's false. So perhaps we should call play when the play is restored.
So let's go ahead and do that,
so we'll put an else clause in here and some, and a code block, and we'll try YouTubePlayer questionmark dot play, and
call the play method like that, and that actually should fix both problems,
but let's actually try that and see if it does do that.
And we're going to see if the video starts automatically, number one, and then we'll try rotate again and
see what happens. Alright, so you can see this time perfect, we've got the video playing,
whereas before we had to click on play, and now I'm going to rotate it into landscape and see what happens.
And that's also good because it's actually played from the position, and just to be completely clear about that,
let's advance the play head to about the one minute 17 mark, give it a second to buffer.
Alright so that's working good as well. I didn't actually have to play,
I didn't have to click play, it automatically played,
but if I go back now to portrait mode again, we can see that's also still working, so no matter of when I
rotate the device, in what mode,
the video's playing without me having to actually click on the play button.
So that's awesome, that's what we were trying to achieve here, so I'll just stop that now.
And incidentally don't use Toast messages for diagnosing problems.
Now even with the short duration, the Toast messages hang around for a while, so they actually get
out of sync with what's happening.
Toast messages are really just for you users, and we should be using logging in all those functions ourselves
for debugging purposes. And just to confirm that problem with the Toast messages again, if I start
actually doing that and start running it, and starting and pausing videos and having a play,
you'll see that the messages will get out of sync. So let's just try that,
and I've stopped now, as you can see those messages, the Toast messages are still trying to catch up.
And you can see there it was quite some time after pressing pause, and we're still getting caught up on Toast messages.
So obviously not a good way to do any form of debugging, you really want to use logcat for that. Alright,
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,
and we're also going to see how we can start new activities to perform different functions, using
a very important Android concept, intents. So see you in the next video.