// Initialize the MediaPlayer MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.audio_file); // Start playing the audio file mediaPlayer.start(); // Pause the audio file mediaPlayer.pause(); // Stop the audio file mediaPlayer.stop(); // Release the MediaPlayer mediaPlayer.release();
// Initialize the MediaPlayer MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.audio_file); mediaPlayer.setLooping(true); // Start playing the audio file mediaPlayer.start();In these examples, the android.media package library is included and the MediaPlayer class is used to play audio files. The create() method is used to initialize the MediaPlayer with an audio file from the res/raw folder. The start(), pause(), and stop() methods are used to control the audio playback. In the second example, the setLooping() method is used to loop the audio file continuously.