This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we add music playback methods to our Service and setup our Activity with a shiny new Button!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've just created our service, added
our song, and set up our media player.
0:00
Now let's add some functionality.
0:05
We'll keep it pretty simple by
just handling the most basic
0:08
features of a music player,
play and pause.
0:10
But first let's add some logging
to our services lifecycle methods
0:15
to help us see what's going on.
0:19
At the top of onCreate,
let's type Log.d(TAG in all caps and
0:21
then "onCreate") for the message.
0:28
And then use Alt+Enter to create our tag.
0:34
PlayerService.class.getSimpleName().
0:39
Next let's copy and paste this log
statement into the top of onBind and
0:46
change the message to say onBind.
0:54
Then below onBind, let's override
the onUnbind method Using Ctrl+O.
0:59
And add a log message to
the top that says onUnbind.
1:09
Lastly, let's override onDestroy and
1:17
add one final log message
to say onDestroy.
1:21
Also in onDestroy,
let's add a call to mPlayer.release().
1:33
This frees up system resources for
other apps and is a best practice for
1:41
when we're finished with our media player.
1:45
Awesome, now let's add a comment
at the bottom of the class
1:47
to let us know the following methods
are meant to be called by a client.
1:51
Typically a client is an activity
that is bound to our service.
1:56
Let's type // Client Methods.
2:01
And then let's add our methods for
play and pause,
2:06
public void play, and
if it suggests setPlayer hit Escape,
2:11
otherwise you'll autocomplete
to the wrong method.
2:16
Add parentheses and
brackets, and then inside
2:21
our new play method type mPlayer.start().
2:26
Add a couple lines below this method, and
2:31
we'll create our pause method,
public void pause() {}.
2:35
And inside let's type mPlayer.pause.
2:42
Notice that each of
these methods is public.
2:48
If we created them as private,
our clients wouldn't be able to use them.
2:52
Okay, now that we have
the ability to play and
2:56
pause, let's add a button
to control music playback.
2:59
This will be the play pause button.
3:04
It will start off labeled as play, but
3:06
once music is playing it will
switch to be labeled pause.
3:09
Let's switch over to our layout file and
add these new buttons.
3:13
On the text tab let's
highlight the code for
3:19
our download button and use Cmd+D or
Ctrl+D to duplicate it.
3:23
Then let's fix the spacing, And
3:31
update the second button to
be our new play pause button.
3:35
Let's change the text to be Play,
And the id to be playButton.
3:40
And instead of aligning to
the bottom of our relative layout,
3:50
we should have it align
above our download button.
3:53
So instead of using
the layout_alignParentBottom attribute,
3:58
let's change that to
the layout_above attribute,
4:04
and set it to @id/downloadButton.
4:10
Now if we look over at the design
tab we can see our buttons.
4:14
Great work!
4:19
Back in main activity, you guessed it,
we need to create our button and
4:20
set up an on click listener.
4:25
Let's create an initializerButton just
like we did with our downloadButton.
4:29
Let's use Cmd+D or
Ctrl+D to duplicate this declaration and
4:34
then change the variable
name to mPlayButton.
4:39
Then let's use Cmd+D or
Ctrl+D down here as well and
4:45
replace the name of
the variable with mPlayButton.
4:51
And let's use the correct id too.
4:57
Finally, at the bottom of onCreate,
let's set up the onClickListener,
5:01
mPlayButton.setOnClickListener(new
OnClickListener().
5:07
All right,
now that we've got our endpoints,
5:16
let's see how we can connect them.
5:18
But before we get to the code,
let's take a look at the big picture.
5:21
When a user taps the button,
we'll want two things to happen.
5:26
One, the play() method and
PlayerService should be called.
5:30
And two,
the button should change to say pause.
5:34
And if they tap the button while
it says pause, the pause()
5:39
method in PlayerService should be called
and the button should change to say play.
5:42
We've got our button and we've got
the methods we need to play our music.
5:48
In the next video we'll connect
our activity to our service
5:52
through the magic of binding.
5:55
Okay, fine, it's not magic.
5:58
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up