-
Book Overview & Buying
-
Table Of Contents
Android Programming for Beginners - Fourth Edition
By :
In the next demo app, we will explore Android sounds for the first time. We will use the MediaPlayer class.
The MediaPlayer class is an Android class that can load an audio file and prepare it for playback. Once set up, an instance of MediaPlayer can play, pause, and stop audio with a simple function call.
We can think about a MediaPlayer instance as a remote control for sound.
Of course, we will need some sound in a standard format, such as .mp3 or .wav. We also need to place the sound file into the project in the res/raw folder.
Once we have done this, Android Studio creates a resource for us based on the sound file's name. Suppose we have a sound file called sound.wav. When we place it in the res/raw folder, we can then refer to the sound file in our code with the resource ID R.raw.sound. Behind the scenes, the resource ID is an Int, but we refer to it by the ID. Look at this code.
val context = LocalContext.current
val mediaPlayer = MediaPlayer.create(context...