Before doing this exercise, copy all the sound resources to the assets
folder. You may do this by copying all the mp3 files to the assets
folder through the File Manager
, as shown in the following screenshot:
Add code for btnsound
in the onClick
method. Add the following lines so that when the btnplay
button is clicked, it will execute the method named playSound()
. This method will receive a string argument as the value of the sound file name to be played.
The soundfile
array variable is the variable that stores the list of the mp3 filenames, while the screennumber
indicates the current number on the screen.
//when btnplay is clicked else if(arg0.getId()==R.id.btnsound){ //call the method playSound playSound(soundfile[screennumber].toString()); }//end btnsound clicked
The next important method is the playsound
method. This method will play an mp3 sound file. The soundname
parameter is the string that contains the sound...