Best Tutorial for Android Jetpack Compose

Android App Development

Stay ahead with the latest tools, trends, and best practices in Android development

SoundPool vs MediaPlayer in Jetpack Compose

SoundPool vs MediaPlayer in Jetpack Compose - Coding Bihar
 
SoundPool vs MediaPlayer in Jetpack Compose

SoundPool vs MediaPlayer in Jetpack Compose: Which One Should You Use?

When building Android apps with Jetpack Compose, audio often plays an important role. Whether it’s a short notification sound, button click effect, or background music track, choosing the right audio playback method is crucial for both performance and user experience.

In Android development, two popular options exist: SoundPool (often misspelled as Soundproof) and MediaPlayer. Both can play audio, but they serve very different purposes. In this article, we’ll explore the differences, use cases, advantages, limitations, and also show you practical Jetpack Compose examples.

🔊 What is SoundPool?

SoundPool is a lightweight audio API designed for short and fast sounds. If your app needs to play a button click sound, game effect, or notification tone, SoundPool is the best choice.

  • Pros of SoundPool

✅ Very low latency (plays almost instantly).

✅ Can play multiple sounds at the same time.

✅ Uses little memory since sounds are preloaded.

  • Cons of SoundPool

❌ Not suitable for long music tracks.

❌ Only provides basic playback controls (play, stop, volume).

👉 Best Use Case: UI click sounds, game sound effects, and short audio clips.

Join Monetag

🎶 What is MediaPlayer?

MediaPlayer is a more powerful Android API built for long audio files such as songs, podcasts, or background music. Unlike SoundPool, it can handle large files and even stream audio from the internet.

  • Pros of MediaPlayer

✅ Perfect for long music or background tracks.

✅ Supports streaming from a URL.

✅ Provides pause, resume, seek, and loop options.

  • Cons of MediaPlayer

❌ Higher latency (not instant like SoundPool).

❌ Can’t handle many sounds at the same time efficiently.

👉 Best Use Case: Music player apps, podcasts, background music in apps.

⚖️ SoundPool vs MediaPlayer: Key Differences

Feature SoundPool (Sound Effects) MediaPlayer (Music)
Latency Very low (instant play) Higher (some delay)
Best For Short sounds, effects Songs, background audio
Simultaneous Play Multiple sounds at once Limited
Controls Basic (play/stop/volume) Advanced (pause, seek, loop)
File Size Small clips (KBs) Large files (MBs)
Streaming ❌ Not supported ✅ Supported

🧑‍💻 Jetpack Compose Examples

Let’s see how to implement both in Jetpack Compose.

1. Playing Short Sound with SoundPool

val context = LocalContext.current
val soundPool = remember { SoundPool.Builder().setMaxStreams(5).build() }
val soundId = remember { soundPool.load(context, R.raw.click_sound, 1) }

Button(onClick = {
    soundPool.play(soundId, 1f, 1f, 1, 0, 1f)
}) {
    Text("Play Click Sound")
}

2. Playing Music with MediaPlayer

val context = LocalContext.current
var mediaPlayer: MediaPlayer? by remember { mutableStateOf(null) }

Column {
    Button(onClick = {
        mediaPlayer = MediaPlayer.create(context, R.raw.sample_music)
        mediaPlayer?.start()
    }) {
        Text("Play Music")
    }

    Button(onClick = { mediaPlayer?.pause() }) {
        Text("Pause Music")
    }

    Button(onClick = {
        mediaPlayer?.stop()
        mediaPlayer?.release()
        mediaPlayer = null
    }) {
        Text("Stop Music")
    }
}

🚀 Which One Should You Use?

  • If you are building a game, quiz app, or utility app where you just need quick sound effects, use SoundPool.
  • If you are creating a music app, podcast app, or anything with long playback, use MediaPlayer (or even better, ExoPlayer).

🌟 Bonus: Why ExoPlayer is Recommended Today

While MediaPlayer still works, Google now recommends using ExoPlayer for most audio/video use cases because it’s:
  • More powerful and customizable.
  • Handles streaming and buffering better.
  • Actively maintained and updated.
If you’re planning to build a modern music player in Jetpack Compose, ExoPlayer should be your first choice.

How to build a QR Scanner Android App using Jetpack Compose?

Full Source Code

@Composable
fun AudioDemoScreen() {
    // Gradient background
    Box(
        modifier = Modifier
            .systemBarsPadding()
            .background(
                Brush.verticalGradient(
                    listOf(Color(0xFF4A90E2),
                        Color(0xFF50E3C2))
                )
            )
            .padding(16.dp),
        contentAlignment = Alignment.TopCenter
    ) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.spacedBy(20.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "🎵 Jetpack Compose Audio Demo",
                style = MaterialTheme.typography.headlineSmall,
                color = Color.White
            )

            // SoundPool Card
            Card(
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(20.dp),
                colors = CardDefaults.cardColors(
                    containerColor = Color.White.copy(alpha = 0.9f)),
                elevation = CardDefaults.cardElevation(
                    defaultElevation = 8.dp)
            ) {
                Column(
                    modifier = Modifier.padding(20.dp),
                    verticalArrangement = Arrangement.spacedBy(12.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text("🔊 Sound Effects (SoundPool)",
                        style = MaterialTheme.typography.titleMedium)
                    SoundPoolButton()
                }
            }

            // MediaPlayer Card
            Card(
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(20.dp),
                colors = CardDefaults.cardColors(
                    containerColor = Color.White.copy(alpha = 0.9f)),
                elevation = CardDefaults.cardElevation(
                    defaultElevation = 8.dp)
            ) {
                Column(
                    modifier = Modifier.padding(20.dp),
                    verticalArrangement = Arrangement.spacedBy(12.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text("🎶 Music Playback (MediaPlayer)",
                        style = MaterialTheme.typography.titleMedium)
                    MediaPlayerControls()
                }
            }
        }
    }
}

@Composable
fun SoundPoolButton() {
    val context = LocalContext.current
    val soundPool = remember { SoundPool.Builder().setMaxStreams(5).build() }
    val soundId = remember { soundPool.load(context, R.raw.mouse_clicks, 1) }

    DisposableEffect(Unit) {
        onDispose {
            soundPool.release()
        }
    }

    Button(onClick = {
        soundPool.play(soundId, 1f, 1f, 1, 0, 1f)
    }) {
        Text("Play Click Sound")
    }
}

@Composable
fun MediaPlayerControls() {
    val context = LocalContext.current
    var mediaPlayer: MediaPlayer? by remember { mutableStateOf(null) }

    DisposableEffect(Unit) {
        onDispose {
            mediaPlayer?.release()
            mediaPlayer = null
        }
    }

    Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
        Button(onClick = {
            mediaPlayer?.release()
            mediaPlayer = MediaPlayer.create(context, R.raw.mouse_clicks)
            mediaPlayer?.start()
        }) {
            Text("▶ Play Music")
        }

        Button(onClick = { mediaPlayer?.pause() }) {
            Text("⏸ Pause Music")
        }

        Button(onClick = {
            mediaPlayer?.stop()
            mediaPlayer?.release()
            mediaPlayer = null
        }) {
            Text("⏹ Stop Music")
        }
    }
}

Output:

Sound Pool vs Media Player in Jetpack Compose App Screenshot

📌 Our Thoughts

Choosing between SoundPool and MediaPlayer depends entirely on your app’s needs.
  • For short, quick, multiple sounds → go with SoundPool.
  • For long, continuous playback → go with MediaPlayer (or ExoPlayer).
By selecting the right tool, you’ll make your app more efficient and deliver a smoother user experience.

Special Message

Welcome to Coding