Android MediaPlayer java.io.IOException: Prepare failed.: status=0x1

17.8k views Asked by At

I am using Android's MediaPlayer to set up a URL stream in my application. I have tried several different posts to deal with the exit code and error: (1, -2147483648).

I have attempted several different streams, but I can't seem to get the MediaPlayer to work. I have thought about moving over the Google's ExoPlayer but it is a little more complex and I don't want to jump ship in case I am missing something.

MediaPlayer:

private MediaPlayer player;
String url = "http://199.180.75.118:80/stream";     //temp stream
private void initializeMediaPlayer() {
    player = new MediaPlayer();
    player.setAudioAttributes( new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        .build());

    try { 
        player.setDataSource(url);
        player.prepareAsync();
        player.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 
}

I have also included the android permission:

<uses-permission android:name="android.permission.INTERNET" /> 

I have attempted to use the original stream type (but it throws a deprecated warning):

player.setAudioStreamType(AudioManager.STREAM_MUSIC);

So instead I used the .setAudioAttributes(...) I have attempted to just run prepare() instead of prepareAsync() which gave the title of the problem, but I still result in the same error. I have looked into the actual error definition with no luck either (Android MediaPlayer error (1, -2147483648)). I don't think it is a source issue since I have tried multiple other streams. Please let me know if I am skipping over something crucial that might be causing my error.

Edit If it helps at all, I have been looking into my calls and I found out that MediaPlayer is never calling onPrepared(...). I checked the Content-Types of all of the media I have been testing and they have all been audio/MPEG headers. So I don't understand why the MediaPlay isn't accessing the onPrepared.

6

There are 6 answers

3
fancyyou On BEST ANSWER
private void initializeMediaPlayer() {
        player = new MediaPlayer();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            player.setAudioAttributes(new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                    .build());
        } else {
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
        try {
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
            player.setDataSource(url);
            player.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

onPrepared calling in seconds.

In android 9, check this https://developer.android.com/training/articles/security-config

AndroidManifest.xml add networkSecurityConfig attributes

...
<application
android:networkSecurityConfig="@xml/network_security_config"
...>
...

in src/res/xml add network_security_config.xml file

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
            <certificates src="user" />
        </trust-anchors>
    </base-config>
</network-security-config>
0
Justice Eli On

I solved this issue by changing my file to another name,initial filename of my audio file was "john:doe.wav",Mediaplayer can't play a file with this ":" special character and many other characters so changing file name to "johndoe.wav" solved the issue.

0
mohammad reza sarsarabi On

I had a local music file in my raw folder and this error kept happening on specific devices. Turned out that the problem was with the file format. Changed it from .m4a to .mp3 and the problem was solved.

I guess that this is a device specific issue related to device supported codecs.

0
Ashvin solanki On

try with this code Tested with real device Vivo V7+ Android 8.1.0

    private MediaPlayer player;
    String url = "http://199.180.75.118:80/stream";     //temp stream
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        player.setAudioAttributes( new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());

        try {
            //change with setDataSource(Context,Uri);
            player.setDataSource(this, Uri.parse(url));
            player.prepareAsync();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                public void onPrepared(MediaPlayer mp) {
                    //mp.start();
                    player.start();
                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Make sure u defined the permission in manifest file

Manifest.xml

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<Application
  android:usesCleartextTraffic="true"
  ....
  >
  //.....
  </Application>

Ref :: https://developer.android.com/training/articles/security-config

Network security configuration

The Network Security Configuration feature lets apps customize their network security settings in a safe, declarative configuration file without modifying app code. These settings can be configured for specific domains and for a specific app. The key capabilities of this feature are as follows:

  • Custom trust anchors: Customize which Certificate Authorities (CA) are trusted for an app's secure connections. For example, trusting particular self-signed certificates or restricting the set of public CAs that the app trusts.
  • Debug-only overrides: Safely debug secure connections in an app without added risk to the installed base.
  • Cleartext traffic opt-out: Protect apps from accidental usage of cleartext traffic.
  • Certificate pinning: Restrict an app's secure connection to particular certificates.

Add a Network Security Configuration file

The Network Security Configuration feature uses an XML file where you specify the settings for your app. You must include an entry in the manifest of your app to point to this file. The following code excerpt from a manifest demonstrates how to create this entry:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config"
                    ... >
        ...
    </application>
</manifest>

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
            <certificates src="user" />
        </trust-anchors>
    </base-config>
</network-security-config>
0
Erick Martinez On

In my case i solved that error with this code, because i was receiving error in Huawei models.

private void setUpMediaRecorder() {
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mMediaRecorder.setOutputFile(mVideoFileName);
    mMediaRecorder.setVideoEncodingBitRate(10000000);
    mMediaRecorder.setVideoFrameRate(30);
    mMediaRecorder.setVideoSize(320, 240);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    switch (mSensorOrientation) {
        case SENSOR_ORIENTATION_DEFAULT_DEGREES:
            mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
            break;
        case SENSOR_ORIENTATION_INVERSE_DEGREES:
            mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
            break;
    }
    try {
        mMediaRecorder.prepare();
    } catch (Exception e) {
        e.printStackTrace();
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setOutputFile(mVideoFileName);
        mMediaRecorder.setVideoEncodingBitRate(10000000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(320, 240);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        switch (mSensorOrientation) {
            case SENSOR_ORIENTATION_DEFAULT_DEGREES:
                mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
                break;
            case SENSOR_ORIENTATION_INVERSE_DEGREES:
                mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
                break;
        }
        try {
            mMediaRecorder.prepare();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
0
Peter On

My problem probably had a different cause than yours, but since I came here with the same error message, here is how I fixed it:

In my case I was trying to read a video that I had created on my computer with opencv and copied to the phone.

The error that I had encoded it as MJPG when I should have used H264. This answer pointed me to the solution.