The media player object also allows us to stream music from somewhere on the Internet.
So what I did actually is I put these watch me and Bathory instead of our my build apps with Pollo,
that com a folder.
So anytime you want to use this, you can just like, follow this link here.
I'll have this link available for you.
But for us to be able to do that, obviously at this point, we are just calling the raw folder the
raw directory here and get the watch me Empey three.
So this works fine.
But he noticed that we have to actually put inside of our folder here inside of a project.
And there are times, like I said, we want to stream or get the sound or the music from somewhere else.
How do we do that?
Well, to do that, we just have to set the data source.
So in the top here where we are saying media player and get it from our Rob, I'm gonna go ahead and
common this out.
And I'm going to say media player, dot set data source like that and I'm going to pass this string.
So the string is going to be the you órale.
Guess here.
I'm going to just copy this
copy and paste it all in there.
But notice said we're going to have a little complaint here.
This is because we are dealing with streaming something from a remote place from somewhere on the Internet.
So obviously that can cause some issues depending on connectivity or maybe the file is unavailable and
so forth.
So what we can do here is just put inside of a tried catch like that.
Okay.
But before we go forward, remember, this requires us to have Internet connection so we can go to our
manifest here and make sure that we pass the permissions or users permission.
So Internet like that.
OK, good.
And then go back to Main there we have our media player, their source.
It's pointing to our Watch Me MP three.
In fact, if I can take this media player and go onto a browser and just place it all in, there are
laws that we are able to play.
OK.
Very good.
So there it is.
So we know that this link actually works.
I just want to show you that.
So next, what we're gonna do is we can also call media player.
I'm going to say that set on prepared listener in here is going to pass the on, prepared me to play
listener.
And it will go ahead and give us these on prepared method, these unprepared method here.
Notice that we have this media player object, which is what it's actually playing at this point.
So so whenever we want to access it, we can do so by using our media player inside here.
So what do we want to do at this point here?
Well, at this point here, all we want to do is to start arming the player.
This means now every time we are setting up our data source for the player, we go make sure that we
call the set unprepared listener, which then we pass this unprepared listener interface and implement
this method unprepared.
So once it's prepared, then we're going to be able to play it.
So when I say Empey, because this player, again, it is this object that we've seen been setting up
and is just being passed there.
And I'm going to say dot start.
Next thing we need to do here is called the async Sulmona state media player DOT.
Prepare async.
So these will allow to a synchronously prepare.
All the assets are needed to start playing our sound or our music.
So we need to call the preparer's sync.
Get this point here.
Let's give a quick try.
Notice now when we play, we're not going to be playing using our RA.
Watch me.
It's going to be coming from our set datasource here, which we added earlier.
Go ahead and give it run.
So you notice that even though it's splaying, fine, but the problem is it goes ahead and starts right
away.
Right.
So if you click here, it's Bozz but then says play.
And they're now spoors and everything goes back to normal.
But something's not quite right here.
So we've got to restructure our code here to make things a little bit simpler for us.
So we understand that the media player on prepared listener who goes ahead as soon as it's prepared,
we are starting our media player, which is fine, but it's a little bit faulty.
So let's see if we can fix this so that it's actually that way it actually behaves the way you want
it to behave.
So the first thing I want to do, I know the display button here.
We've got to move it and put at the top here.
Doesn't really matter.
But because the things we're gonna be doing now, I think it's better to have it there.
And what I'm going to do for our unprepared listener here.
I'm gonna actually take all of this code and put inside of a variable.
So in this case here, this variable is going to be.
I'm of prepared.
Listener like that.
Go ahead and create this method or variable, I should say.
That's going to make it local like this.
All right, so we have it here and I'm going to say is equal to all of that.
OK, so now we can just use this, as you see here and call it their.
Very good.
And our play button here, so I'm gonna get this play button.
All of this code, I'm going gonna cut it all and put it all inside here.
OK, there we go.
So we have that code and we're gonna restructure things here because we know that I'm prepared.
Like I said, it's gonna go ahead and play as soon as the unprepared is ready.
So what I'm gonna do actually is this MP three start.
I'm going to take it out of there.
And when I put inside of LS, because that's where our play music is.
In fact, we need to make this final because we can set off another class here.
That's fine.
Very good.
So this point here, I'm going to get the code of MP three.
All of this code here is just copy all of this.
And delete it all.
I'm gonna guess.
Make it MP three or MP that we're passing here starts.
In fact, it's all the same code here.
I'm just restructuring things and then I'm gonna make sure the play button here is to pause, OK?
And for pause music, literally the same thing.
Copy all of this.
We could have left and called his methods, but I don't want to do that anymore, so I'm just gonna
do like that again.
MP.
There we go.
MP, that's very gets so delete all of this.
And this is going to be MP.
OK.
Looks like.
It's always true.
Well, because inside here, apparently, we don't need to check.
And then just change all of that.
And when I'm writing the statement like that.
OK.
All right.
So at this point here, we should be good.
Let me go ahead and go through this code again.
So what we did here, we just change a few things around our prepared list.
Now, we put that in a variable so that we can call it later.
As you can see here, just pass it instead of creating all inside here.
That's not a way to do this.
And what we did, because we know that on inside of and prepared here we have our media player, which
right away went ahead and started playing.
But we don't want to that we wanted to want to give the user, as in this case, the power of starting
our media player.
So I put the play button inside there to seven click listener.
So the moment we come in, it's going to go ahead and check if it's playing right.
We're going to use the MP, which is our media player here, being passed around.
And if it's playing, if they click, it's going to go in a pause, which will make this button text
title to say play else is going to go ahead and start.
OK.
And then again, to make it the button to say pause.
So essentially the same thing we had before.
And this point here, we can go ahead and comment this code out because we don't quite needed at this
point.
Let's save this and hope for the best.
Give it a quick run there.
All right, let's give a second.
Nothing is Splaine that's good.
Let's play nice.
All right.
So it's working just like was working before, but at least we know that we have the power.
Let's pause.
It's pausing.
Let's play again.
Pause play again.
Nice.
OK.
Now it's working, so you have two ways to do this, right, a previous way where we actually didn't
need to use the RA.
Watch me, the MP three in this case, at this point here, we are able to play our music from a different
data source, which is a remote link.
This one here.
OK.
Very cool.
And remember, we are also making sure that on destroy.
So when we're getting rid of our activity, we're going to be getting rid of our media player.
A section.