‫Welcome back.
‫Till now, we succeeded in getting the ID of the clicked button and playing the correct sound.
‫Every time we need to click to check for the click the button ID, we need to create a media player
‫instance.
‫And this is not a good approach.
‫We need to create one instance of the media player and start it with the correct sound.
‫So for that we need to create a new function called play sounds.
‫And I need to pass the ID and I'll move this code to here.
‫It's very simple now.
‫I'll call play sounds method and instead of passing, here are the row dot read.
‫I'll pass id remember media player dot create function needs two parameters.
‫The first one is the context, which is no problem and it's provided by this and end resource ID.
‫If I pass this ID as an argument for the play sounds, it will play it directly.
‫Here.
‫I need to pass r.id or r dot row dot read.
‫Let me run test it and it plays the red sound.
‫Congratulations guys.
‫We succeeded in getting this media player and playing the sounds correctly with no problem and without
‫making it boilerplate code.
‫Else if.
‫But an idea that I didn't.
‫So I'll make the same for the other buttons.
‫And here we go.
‫We're covering all the cases, all five buttons and calling the method play sounds when passing the
‫correct row resource run our application.
‫Now let's play the sounds.
‫River, Violette.
‫Rouge, June.
‫And congratulations, guys.
‫This is how we use the media player in Android studio to play the sounds that are placed in the row
‫folder.