Issue
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.
Solution
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>
Answered By - fancyyou
Answer Checked By - Willingham (JavaFixing Volunteer)