---
title: Decode MP3s and other Audio formats the easy way on Android
canonical: https://0110.be/posts/Decode_MP3s_and_other_Audio_formats_the_easy_way_on_Android
markdown_url: https://0110.be/posts/Decode_MP3s_and_other_Audio_formats_the_easy_way_on_Android.md
id: 438
published_at: '2015-07-14T00:00:00Z'
updated_at: '2025-11-29T19:48:15Z'
author: Joren
tags:
- name: TarsosDSP
  markdown_url: https://0110.be/tags/TarsosDSP.md
- name: UGent
  markdown_url: https://0110.be/tags/UGent.md
---

# Decode MP3s and other Audio formats the easy way on Android

This post describes how to decode MP3's using an already compiled ffmpeg binary on android. Using ffmpeg to decode audio on Android has advantages:

-   It supports about every audio format known to man. Three channel flac, vorbis with 32 bit samples, ... do not pose a problem.
-   Extracting audio from video container formats is supported. Accessing the first audio stream from `mkv`, `avi`, `mov`,... just works.
-   Decoding audio frames is more efficient using native code than often buggy Java decoders.
-   Resampling and downmixing is supported. If you want to resample incoming audio to e.g. 44.1kHz and only want single channel audio this is easily achievable.

The main disadvantage is that you need an ffmpeg build for your Android device. Luckily some [poor soul](https://github.com/hiteshsondhi88/ffmpeg-android) already managed to compile ffmeg for Android for several architectures. The [precompiled ffmpeg binaries for Android](https://github.com/hiteshsondhi88/ffmpeg-android/releases/download/v0.3.3/prebuilt-binaries.zip) are available for download and are mirrored [here](https://0110.be/releases/TarsosDSP/TarsosDSP-static-ffmpeg/Android/) as well.

To bridge the ffmpeg binary and the java world TarsosDSP contains some glue code. The `AndroidFFMPEGLocator` is responsible to find and extract the correct binary for your Android device. It expects [these ffmpeg binaries](https://0110.be/releases/TarsosDSP/TarsosDSP-static-ffmpeg/Android/) in the assets folder of your Android application. When the correct ffmpeg binary has been extracted and made executable the `PipeDecoder` is able to call it. The `PipeDecoder` calls ffmpeg so that decoded, downmixed and resampled PCM samples are streamed into the Java application via a pipe, which explains its name.

With the TarsosDSP Android library the following code plays an MP3 from external storage:

````java
new AndroidFFMPEGLocator(this);
new Thread(new Runnable() {
    @Override
    public void run() {
        File externalStorage = Environment.getExternalStorageDirectory();
        File mp3 = new File(externalStorage.getAbsolutePath(), "/audio.mp3");
        AudioDispatcher adp;
        adp = AudioDispatcherFactory.fromPipe(mp3.getAbsolutePath(), 44100, 5000, 2500);
        adp.addAudioProcessor(new AndroidAudioPlayer(adp.getFormat(), 5000, AudioManager.STREAM_MUSIC));
        adp.run();
    }
}).start();
````

This code *just works* if the application has the `READ_EXTERNAL_STORAGE` permission, includes a recent TarsosDSP-Android.jar, is ran on one of the supported ffmpeg architectures and has these binaries available in the assets folder.

