媒体播放器在状态 0 中调用,错误 (-38,0) [英] Media Player called in state 0, error (-38,0)

查看:22
本文介绍了媒体播放器在状态 0 中调用,错误 (-38,0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试设计一个简单的应用程序来播放网络广播电台.我有电台的 URL,正在设置媒体播放器,如

I am currently trying to design a simple app that streams an internet radio station. I have the URL for the station and am setting up the Media Player like

    MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(URL);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();

程序在模拟时没有崩溃,但没有任何内容在播放,我收到以下错误:

The program isn't crashing when emulated, but nothing is playing and I am get the following error:

start called in state 0

它的正下方是

Error (-38,0)

有人知道这是什么意思吗?

Does anyone know what this means?

我已经阅读了一些关于这些状态错误的内容,但找不到任何适用于我的项目的内容.

I've read a little about these state errors, but couldn't find anything that applies to my project.

推荐答案

您需要使用侦听器在 onPrepared 方法中调用 mediaPlayer.start().您收到此错误是因为您在 mediaPlayer.start() 到达准备状态之前调用了它.

You need to call mediaPlayer.start() in the onPrepared method by using a listener. You are getting this error because you are calling mediaPlayer.start() before it has reached the prepared state.

你可以这样做:

mp.setDataSource(url); 
mp.setOnPreparedListener(this);
mp.prepareAsync();

public void onPrepared(MediaPlayer player) {
    player.start();
}

这篇关于媒体播放器在状态 0 中调用,错误 (-38,0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆