Android MediaPlayer java.io.IOException:准备失败.:status = 0x1 [英] Android MediaPlayer java.io.IOException: Prepare failed.: status=0x1
问题描述
我正在使用Android的MediaPlayer在我的应用程序中设置URL流.我尝试了几种不同的方法来处理退出代码和错误:(1,-2147483648).
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).
我尝试了几种不同的流,但是我似乎无法使MediaPlayer正常工作.我曾考虑过要使用Google的ExoPlayer,但它要复杂一些,如果我遗漏了某些东西,我不想跳船.
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:
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();
}
}
我还包括了android权限:
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);
所以我改用了.setAudioAttributes(...)
我试图只运行prepare()
而不是prepareAsync()
,这给出了问题的标题,但是我仍然会导致相同的错误.我已经仔细调查了实际的错误定义( Android MediaPlayer错误(1,-2147483648) ).我认为这不是源问题,因为我尝试了其他多个流.如果我跳过了可能导致我出错的关键问题,请告诉我.
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.
修改
如果有帮助,我一直在调查呼叫,发现MediaPlayer从未呼叫onPrepared(...)
.我检查了我正在测试的所有媒体的Content-Type,它们都是音频/MPEG标头.所以我不明白为什么MediaPlay无法访问onPrepared.
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.
推荐答案
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呼叫可以在几秒钟内完成.
onPrepared calling in seconds.
在android 9中,选中此https://developer.android.com/training/articles/security-config
In android 9, check this https://developer.android.com/training/articles/security-config
AndroidManifest.xml添加networkSecurityConfig属性
AndroidManifest.xml add networkSecurityConfig attributes
...
<application
android:networkSecurityConfig="@xml/network_security_config"
...>
...
在src/res/xml中添加network_security_config.xml文件
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>
这篇关于Android MediaPlayer java.io.IOException:准备失败.:status = 0x1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!