Android 媒体播放器返回 IllegalStateException

发布于 2024-11-30 00:16:41 字数 800 浏览 0 评论 0 原文

我有以下代码来播放小音频文件

private void playVoice() {

     if (mPlayVoice != null) {
         if (mPlayVoice.isPlaying()) {
             mPlayVoice.release();
             mPlayVoice = null;
         }
     }
     mPlayVoice = 
         MediaPlayer.create(BirdsActivity.this, mSoundIds[getCurrentIndex()]);
     mPlayVoice.start();
 }

它在三星 Galaxy 选项卡中工作正常,但在小型设备中给出以下错误(我在索尼 xperia mini pro 中检查了我的项目)

08-17 12:45:45.232: ERROR/AndroidRuntime(6639): java.lang.IllegalStateException
    08-17 12:45:45.232: ERROR/AndroidRuntime(6639):     at   android.media.MediaPlayer.isPlaying(Native Method)
    08-17 12:45:45.232: ERROR/AndroidRuntime(6639):     at           com.android.mds.kidsapps.alphakids.BirdsActivity.playVoice(BirdsActivity.java:146)

I have following code to play small audio files

private void playVoice() {

     if (mPlayVoice != null) {
         if (mPlayVoice.isPlaying()) {
             mPlayVoice.release();
             mPlayVoice = null;
         }
     }
     mPlayVoice = 
         MediaPlayer.create(BirdsActivity.this, mSoundIds[getCurrentIndex()]);
     mPlayVoice.start();
 }

It works fine in Samsung galaxy tab but gives below error in small device(I Checked in Sony xperia mini pro my project)

08-17 12:45:45.232: ERROR/AndroidRuntime(6639): java.lang.IllegalStateException
    08-17 12:45:45.232: ERROR/AndroidRuntime(6639):     at   android.media.MediaPlayer.isPlaying(Native Method)
    08-17 12:45:45.232: ERROR/AndroidRuntime(6639):     at           com.android.mds.kidsapps.alphakids.BirdsActivity.playVoice(BirdsActivity.java:146)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名的好友 2024-12-07 00:16:41

你正在这样做:

 PlayVoice.release(); 

你不是说

 mPlayVoice.release(); 

如果你有其他问题,这是最好的参考文档:

Android MediaPlayer

编辑

好的,如果您在这里:isPlaying() 无效状态 它表明您正在尝试在播放器处于错误状态时调用 isPlaying()。因此,您需要弄清楚为什么它已经处于错误状态。

一般情况下,某些播放控制操作可能会因各种原因而失败,例如音视频格式不支持、音视频交错效果不佳、分辨率太高、串流超时等。

看看添加错误侦听器:

You're doing this:

 PlayVoice.release(); 

Do you not mean

 mPlayVoice.release(); 

If you have other issues this is the best document to consult:

Android MediaPlayer

EDIT

Ok if you are here: isPlaying() Invalid States it show's you're trying to call isPlaying() while the player is in the error state. So you need to work out why it is already in the error state.

In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like.

Have a look at adding an error listener: setOnErrorListener()

寂寞清仓 2024-12-07 00:16:41

使用以下代码,因为我遇到了相同的异常。

try {
    if(mPlayVoice!=null && mPlayVoice.isPlaying()) {
        Log.d("TAG------->", "player is running");
        mPlayVoice.stop();
        Log.d("Tag------->", "player is stopped");
        mPlayVoice.release();
        Log.d("TAG------->", "player is released");
    }
} catch(Exception e){
}

在这里写下你想做的任何事情。实际上,像 isPlaying() 这样的条件检查或检查 null 会生成 IllegalStateException......

Use the following code as i was facing the same exception.

try {
    if(mPlayVoice!=null && mPlayVoice.isPlaying()) {
        Log.d("TAG------->", "player is running");
        mPlayVoice.stop();
        Log.d("Tag------->", "player is stopped");
        mPlayVoice.release();
        Log.d("TAG------->", "player is released");
    }
} catch(Exception e){
}

Here write whatever you want to do. Actually the condition checking like isPlaying() or checking for null generates the IllegalStateException.....

心清如水 2024-12-07 00:16:41

您可能需要清除与audioStream连接的audioGroup。我的工作使用以下代码:

public static void audioPlayCaptureStop()
        {

            try 
            {
                 if(audioStream.isBusy()) 
                 {
                     audioGroup.clear();
                     audioStream.release();
                     System.out.println("audioStream released");
                 }

            } catch (Exception e) {
                System.out.println("audioStream release exception: "+e.toString());
            }
        }

You may have to clear the audioGroup joined with audioStream. Mine worked with the following code:

public static void audioPlayCaptureStop()
        {

            try 
            {
                 if(audioStream.isBusy()) 
                 {
                     audioGroup.clear();
                     audioStream.release();
                     System.out.println("audioStream released");
                 }

            } catch (Exception e) {
                System.out.println("audioStream release exception: "+e.toString());
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文