MediaPlayer.setDataSource causes IOException for valid file

I would like to tell you that MediaPlayer is running and working correctly. Below are all my hypothesis. Don’t take them too seriously.

It is your test.m4a file that is encoded in a format such that MediaPlayer can’t decode, therefore throwing out an error saying that it can’t create the MediaPlayer for you to use. Seems like you encoded it in some FFmpeg codecs you had on your computer. It would be better if you tried creating the file without using advanced codecs such as FFmpeg, as Android can’t decode advanced codecs on the fly.

However, I do not have the answer to why some smartphones can play your M4A file, while others can’t. My suggestion for you is to use a simple OGG format that Audacity already has installed, for all of your Android projects, because the OGG codec what Audacity has, is simple enough for Android to decode on the fly. I’m not claiming that many devs are using mostly OGG, just that OGG doesn’t seem to have a lot of problems with MediaPlayer and SoundPool when using simple codecs. Needs more researching…

Even my book, Beginning Android 4 Game Development, depends on OGG files.

Here’s my proof below. This is the file “test.m4a” I’m using from your link. I made it so that it runs in a endless loop, making it a 100% proof of evidence:

03-18 01:05:22.826: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:22.826: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:22.936: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:22.936: I/MediaPlayer(1289): IOException...
03-18 01:05:22.936: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:22.936: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.037: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.046: I/MediaPlayer(1289): IOException...
03-18 01:05:23.046: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.046: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.146: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.146: I/MediaPlayer(1289): IOException...
03-18 01:05:23.146: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.156: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.256: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.256: I/MediaPlayer(1289): IOException...
03-18 01:05:23.256: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.256: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.366: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.366: I/MediaPlayer(1289): IOException...
03-18 01:05:23.366: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.366: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.476: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.476: I/MediaPlayer(1289): IOException...
03-18 01:05:23.476: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.476: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.586: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.586: I/MediaPlayer(1289): IOException...
03-18 01:05:23.586: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.586: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.696: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.696: I/MediaPlayer(1289): IOException...
03-18 01:05:23.696: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.696: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.806: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.806: I/MediaPlayer(1289): IOException...
03-18 01:05:23.806: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.806: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.917: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.917: I/MediaPlayer(1289): IOException...
03-18 01:05:23.917: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.917: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.026: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.026: I/MediaPlayer(1289): IOException...
03-18 01:05:24.026: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.026: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.136: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.136: I/MediaPlayer(1289): IOException...
03-18 01:05:24.136: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.136: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.246: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.246: I/MediaPlayer(1289): IOException...
03-18 01:05:24.246: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.246: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.356: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.356: I/MediaPlayer(1289): IOException...
03-18 01:05:24.356: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.356: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.466: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.466: I/MediaPlayer(1289): IOException...
03-18 01:05:24.466: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.466: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.856: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.856: I/MediaPlayer(1289): IOException...
03-18 01:05:24.856: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.856: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.017: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.017: I/MediaPlayer(1289): IOException...
03-18 01:05:25.017: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.017: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.146: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.157: I/MediaPlayer(1289): IOException...
03-18 01:05:25.157: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.157: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.266: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.266: I/MediaPlayer(1289): IOException...
03-18 01:05:25.266: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.266: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.376: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.376: I/MediaPlayer(1289): IOException...
03-18 01:05:25.376: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.376: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.486: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.486: I/MediaPlayer(1289): IOException...
03-18 01:05:25.486: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.486: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.586: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.586: I/MediaPlayer(1289): IOException...
03-18 01:05:25.586: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.586: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.696: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.696: I/MediaPlayer(1289): IOException...
03-18 01:05:25.696: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.696: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.806: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.806: I/MediaPlayer(1289): IOException...
03-18 01:05:25.806: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.806: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.916: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.916: I/MediaPlayer(1289): IOException...
03-18 01:05:25.916: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.916: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.026: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.026: I/MediaPlayer(1289): IOException...
03-18 01:05:26.026: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.026: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.136: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.136: I/MediaPlayer(1289): IOException...
03-18 01:05:26.136: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.136: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.246: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.246: I/MediaPlayer(1289): IOException...
03-18 01:05:26.246: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.246: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.356: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.356: I/MediaPlayer(1289): IOException...
03-18 01:05:26.356: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.356: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.466: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.466: I/MediaPlayer(1289): IOException...
03-18 01:05:26.466: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.466: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.577: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.577: I/MediaPlayer(1289): IOException...
03-18 01:05:26.577: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.577: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.697: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.697: I/MediaPlayer(1289): IOException...
03-18 01:05:26.697: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.697: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.807: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.807: I/MediaPlayer(1289): IOException...
03-18 01:05:26.807: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.807: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.917: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.917: I/MediaPlayer(1289): IOException...
03-18 01:05:26.917: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.917: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.027: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.027: I/MediaPlayer(1289): IOException...
03-18 01:05:27.027: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.027: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.137: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.137: I/MediaPlayer(1289): IOException...
03-18 01:05:27.137: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.137: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.236: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.236: I/MediaPlayer(1289): IOException...
03-18 01:05:27.236: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.236: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.347: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.347: I/MediaPlayer(1289): IOException...
03-18 01:05:27.347: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.347: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.457: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.457: I/MediaPlayer(1289): IOException...
03-18 01:05:27.457: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.457: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.567: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.567: I/MediaPlayer(1289): IOException...
03-18 01:05:27.567: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.567: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.677: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.677: I/MediaPlayer(1289): IOException...
03-18 01:05:27.677: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.677: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.787: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.787: I/MediaPlayer(1289): IOException...
03-18 01:05:27.787: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.787: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.907: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.907: I/MediaPlayer(1289): IOException...
03-18 01:05:27.907: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.907: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.007: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.017: I/MediaPlayer(1289): IOException...
03-18 01:05:28.017: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.017: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.127: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.127: I/MediaPlayer(1289): IOException...
03-18 01:05:28.127: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.127: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.237: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.237: I/MediaPlayer(1289): IOException...
03-18 01:05:28.237: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.237: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.347: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.347: I/MediaPlayer(1289): IOException...
03-18 01:05:28.347: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.347: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.457: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.457: I/MediaPlayer(1289): IOException...
03-18 01:05:28.457: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.457: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.567: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.567: I/MediaPlayer(1289): IOException...
03-18 01:05:28.567: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.567: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.677: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.677: I/MediaPlayer(1289): IOException...
03-18 01:05:28.677: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.677: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.787: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.787: I/MediaPlayer(1289): IOException...
03-18 01:05:28.787: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.787: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.907: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.907: I/MediaPlayer(1289): IOException...
03-18 01:05:28.907: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.907: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.017: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:29.017: I/MediaPlayer(1289): IOException...
03-18 01:05:29.017: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:29.017: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.127: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:29.127: I/MediaPlayer(1289): IOException...
03-18 01:05:29.127: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:29.127: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.237: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:29.237: I/MediaPlayer(1289): IOException...
03-18 01:05:29.237: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:29.237: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.327: D/dalvikvm(1289): Calling exit(1)

Now, here’s the code I made, shown below:

package nttu.edu.test;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MediaPlayerTest extends Activity implements OnTouchListener {
    MediaPlayer player = null;
    TextView tv;
    public final String fileName = "sounds/test.m4a";
    AssetManager asset;
    AssetFileDescriptor afd;

    public void onCreate(Bundle b) {
        super.onCreate(b);
        tv = new TextView(this);
        tv.setOnTouchListener(this);
        tv.setText("Touch to start playing the music.");
        Log.d("MediaPlayer", "Creating MediaPlayer.");
        player = new MediaPlayer();

        try {
            asset = this.getAssets();
            afd = asset.openFd(fileName);
        }
        catch (Exception e) {
        }
        while (true) {
            try {
                Log.d("MediaPlayer", "Making sure it is in IDLE state...");
                player.reset();
                Log.d("MediaPlayer", "Setting Data Source...");
                player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                Log.d("MediaPlayer", "Now initialized. Preparing it.");
                player.prepare();
                break;
            }
            catch (IllegalArgumentException e) {
                Log.i("MediaPlayer", "IllegalArgumentException...");
            }
            catch (IllegalStateException e) {
                Log.i("MediaPlayer", "IllegalStateException...");
            }
            catch (IOException e) {
                Log.i("MediaPlayer", "IOException...");
            }
        }
        try {
            afd.close();
        }
        catch (IOException e) {
        }
        setContentView(tv);
        Log.d("MediaPlayer", "Everything is prepared and ready to play the music file.");
    }

    public boolean onTouch(View v, MotionEvent e) {
        if (e.getAction() == MotionEvent.ACTION_UP) {
            Log.d("MediaPlayer", "Playing the audio.");
            if (player.getAudioSessionId() != 0) {
                player.start();
                Log.d("MediaPlayer", "Success!");
            }
        }
        return true;
    }

    protected void onResume() {
        super.onResume();

    }

    protected void onPause() {
        super.onPause();
        if (player.getAudioSessionId() != 0) {
            player.pause();
            if (isFinishing()) {
                player.stop();
                player.release();
            }
        }
    }
}

Notice the final String variable near the top of the code snippet. If I replace that line with this:

public final String fileName = "sounds/test.ogg";

And re-encode your “test.m4a” file to an Ogg Vorbis file, and no other changes in the code, I can play the OGG file just fine. Here are the change logs for the OGG file:

03-18 01:14:21.547: D/gralloc_goldfish(1335): Emulator without GPU emulation detected.
03-18 01:14:23.467: D/dalvikvm(1335): GC_CONCURRENT freed 50K, 4% free 5952K/6151K, paused 7ms+3ms
03-18 01:14:24.647: D/MediaPlayer(1335): Creating MediaPlayer.
03-18 01:14:24.657: D/MediaPlayer(1335): Making sure it is in IDLE state...
03-18 01:14:24.657: D/MediaPlayer(1335): Setting Data Source...
03-18 01:14:24.707: D/MediaPlayer(1335): Now initialized. Preparing it.
03-18 01:14:24.927: D/MediaPlayer(1335): Everything is prepared and ready to play the music file.
03-18 01:14:26.797: D/MediaPlayer(1335): Playing the audio.
03-18 01:14:26.817: D/MediaPlayer(1335): Success!

And then it stops here. There’s no looping going around.

More evidence to support my answer:

Link 1: Android SDK Reference (Do look at the State Diagram, very important!)

And a picture (Image of your “test.m4a” and your file re-encoded into an OGG file, placed side-by-side.):

Image of your "test.m4a" and your file re-encoded into an OGG file, placed side-by-side.

Leave a Comment