Android MediaPlayer java.io.IOException:准备失败。:status = 0x1

时间:2019-04-02 16:31:16

标签: android android-mediaplayer urlstream

我正在使用Android的MediaPlayer在我的应用程序中设置URL流。我尝试了几种不同的方法来处理退出代码和错误:(1,-2147483648)。

我尝试了几种不同的流,但是似乎无法使MediaPlayer正常工作。我曾考虑过要使用Google的ExoPlayer,但它要复杂一些,如果我遗漏了某些东西,我不想跳船。

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权限:

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

我尝试使用原始流类型(但是它抛出了不建议使用的警告):

player.setAudioStreamType(AudioManager.STREAM_MUSIC);

所以我改用了.setAudioAttributes(...) 我试图只运行prepare()而不是prepareAsync()来给出问题的标题,但仍然会导致相同的错误。我研究了实际的错误定义,也没有运气(Android MediaPlayer error (1, -2147483648))。我认为这不是源问题,因为我尝试了其他多个流。请让我知道我是否跳过了可能导致我的错误的关键操作。

修改 如果有帮助,我一直在调查呼叫,发现MediaPlayer从未呼叫onPrepared(...)。我检查了我正在测试的所有媒体的Content-Type,它们都是音频/ MPEG标头。所以我不明白为什么MediaPlay无法访问onPrepared。

3 个答案:

答案 0 :(得分:2)

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呼叫可以在几秒钟内完成。

在android 9中,选中此https://developer.android.com/training/articles/security-config

AndroidManifest.xml添加networkSecurityConfig属性

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

在src / res / xml中添加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>

答案 1 :(得分:2)

尝试使用此代码 经过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();
        }
    }

确保您在清单文件中定义了权限

Manifest.xml

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

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

答案 2 :(得分:0)

就我而言,我使用此代码解决了该错误,因为我在华为型号中收到错误。

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();
        }
    }

}