Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

passing parameters in crystall ball player

why do we change the name of the parameter in this section of code from player to mp? Could we keep the same name? : MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball); player.start(); player.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.release();

2 Answers

he explains it in the video. For variable scooping reasons.

ok, I know he said that, but it didn't mean anything to me. This explanation and the "shaking" module are too advanced for a beginner. I realize there are varied degrees of experience on here so I'll let it go.

Okay, sorry karen. Can't help you with the shaking module. But an extra variable is created for the scooping reasons. It's better to have a different name so the variables that you use won't shadow other variables names. It just keeps things cleaned and organized. Hope that helps! :)

Thank you JB.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I've realized I didn't talk enough about variable scope in this project. We'll talk about it in more detail in an upcoming project. Anyhow, let's look at the code:

private void playSound() {
        MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball);
        player.start();

        player.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }

So the scope of this playSound() method is everything between the curly braces. Anything declared in here is not available outside of this method. Outside of the method is code that is "out of scope".

The player variable is thus a variable whose scope includes everything inside this method.

This is the most confusing part: When setting the onCompletionListener, we are creating a new OnCompletionListener object, which has its own scope. We do it this way because it's easier to read the code and see right here how it's used, but we could declare the new OnCompletionListener somewhere else and reference it in here.

Anyhow, because it has its own scope, it actually can't reference the player variable without making a change. But a new copy of the MediaPlayer variable is passed into the onCompletion() method. This copy is a new variable named mp. We then use that copy to call the release() method.

Hopefully this confusion doesn't stop you from going on! It's totally natural to be confused and we can hopefully help you here on the Forum. Stuff does start to make sense the more you do it, so keep it up!