How to use MediaPlayer in an Android app?

The MediaPlayer class in Android is used to play audio and video files. It supports various media formats and provides a simple interface for media playback. Here’s how to use MediaPlayer in your Android app to play an audio file.

MediaPlayer, Android MediaPlayer, play audio Android, audio playback Android, Android media playback example

This example demonstrates how to use the MediaPlayer class in an Android application for audio playback. It includes initializing the MediaPlayer, playing, pausing, and releasing resources.

<![CDATA[ // Step 1: Initialize MediaPlayer MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sample_audio); // Step 2: Start Playback mediaPlayer.start(); // Step 3: Pause Playback if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); } // Step 4: Resume Playback mediaPlayer.start(); // Step 5: Stop Playback mediaPlayer.stop(); // Step 6: Release Resources mediaPlayer.release(); ]]>

MediaPlayer Android MediaPlayer play audio Android audio playback Android Android media playback example