java.lang.IllegalStateException 是什么意思?

发布于 2024-12-10 13:32:08 字数 4696 浏览 0 评论 0原文

我正在开发一个视频应用程序。第一个视频播放完成后,在“OnCopletion”中,我尝试开始一个新的视频。但它只是停止(而不是崩溃)并且什么也不做。日志中:

10-19 09:44:49.056: ERROR/MediaPlayer(4654): setDataSource called in state 128
10-19 09:44:49.056: WARN/System.err(4654): java.lang.IllegalStateException
10-19 09:44:49.056: WARN/System.err(4654):     at android.media.MediaPlayer.setDataSource(Native Method)
10-19 09:44:49.056: WARN/System.err(4654):     at ru.osiris.BusAdvertising.BusAdvertisingActivity.onCompletion(BusAdvertisingActivity.java:1255)
10-19 09:44:49.056: WARN/System.err(4654):     at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1304)
10-19 09:44:49.056: WARN/System.err(4654):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 09:44:49.056: WARN/System.err(4654):     at android.os.Looper.loop(Looper.java:123)
10-19 09:44:49.056: WARN/System.err(4654):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-19 09:44:49.056: WARN/System.err(4654):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 09:44:49.056: WARN/System.err(4654):     at java.lang.reflect.Method.invoke(Method.java:521)
10-19 09:44:49.056: WARN/System.err(4654):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-19 09:44:49.056: WARN/System.err(4654):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-19 09:44:49.056: WARN/System.err(4654):     at dalvik.system.NativeStart.main(Native Method)

这是什么意思?我该如何修复它? 这是我的代码:

public class BusAdvertisingActivity extends Activity implements LocationListener, OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
...
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    private String path;
    private Bundle extras;

     @Override
    public void onCreate(Bundle icicle) {
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        extras = getIntent().getExtras();
    }
 public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playVideo();
}
private void playVideo(Integer Media) {
        Log.d(TAG, "playVideo called");
        doCleanUp();
        try {
           File clip=new File(Environment.getExternalStorageDirectory(),
                            playList[FLcurrentVideo].substring(2,            playList[FLcurrentVideo].length()-1)+".mp4");
           path = clip.getAbsolutePath();
           mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }


public void onCompletion(MediaPlayer arg0) {

        Log.d(TAG, "onCompletion called");
        FLcurrentVideo++;

        File clip=new File(Environment.getExternalStorageDirectory(),
                playList[FLcurrentVideo].substring(2, playList[FLcurrentVideo].length()-1)+".mp4");
                path = clip.getAbsolutePath();

                    try {
                        Log.d ("111", path);
                        arg0.setDataSource(clip.getAbsolutePath());   //I got exception here
                        arg0.setDisplay(holder);
                        arg0.prepare();
                        arg0.setOnBufferingUpdateListener(this);
                        arg0.setOnCompletionListener(this);
                        arg0.setOnPreparedListener(this);
                        arg0.setOnVideoSizeChangedListener(this);
                        arg0.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



    }

请帮助我。

I'm developing a video application. After 1st video playback done, in the "OnCopletion" I'm trying to start a new one. But it just stops (not crashes) and do nothing. In the log:

10-19 09:44:49.056: ERROR/MediaPlayer(4654): setDataSource called in state 128
10-19 09:44:49.056: WARN/System.err(4654): java.lang.IllegalStateException
10-19 09:44:49.056: WARN/System.err(4654):     at android.media.MediaPlayer.setDataSource(Native Method)
10-19 09:44:49.056: WARN/System.err(4654):     at ru.osiris.BusAdvertising.BusAdvertisingActivity.onCompletion(BusAdvertisingActivity.java:1255)
10-19 09:44:49.056: WARN/System.err(4654):     at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1304)
10-19 09:44:49.056: WARN/System.err(4654):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 09:44:49.056: WARN/System.err(4654):     at android.os.Looper.loop(Looper.java:123)
10-19 09:44:49.056: WARN/System.err(4654):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-19 09:44:49.056: WARN/System.err(4654):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 09:44:49.056: WARN/System.err(4654):     at java.lang.reflect.Method.invoke(Method.java:521)
10-19 09:44:49.056: WARN/System.err(4654):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-19 09:44:49.056: WARN/System.err(4654):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-19 09:44:49.056: WARN/System.err(4654):     at dalvik.system.NativeStart.main(Native Method)

what does it mean? How can I fix it?
there is my code:

public class BusAdvertisingActivity extends Activity implements LocationListener, OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
...
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    private String path;
    private Bundle extras;

     @Override
    public void onCreate(Bundle icicle) {
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        extras = getIntent().getExtras();
    }
 public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playVideo();
}
private void playVideo(Integer Media) {
        Log.d(TAG, "playVideo called");
        doCleanUp();
        try {
           File clip=new File(Environment.getExternalStorageDirectory(),
                            playList[FLcurrentVideo].substring(2,            playList[FLcurrentVideo].length()-1)+".mp4");
           path = clip.getAbsolutePath();
           mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }


public void onCompletion(MediaPlayer arg0) {

        Log.d(TAG, "onCompletion called");
        FLcurrentVideo++;

        File clip=new File(Environment.getExternalStorageDirectory(),
                playList[FLcurrentVideo].substring(2, playList[FLcurrentVideo].length()-1)+".mp4");
                path = clip.getAbsolutePath();

                    try {
                        Log.d ("111", path);
                        arg0.setDataSource(clip.getAbsolutePath());   //I got exception here
                        arg0.setDisplay(holder);
                        arg0.prepare();
                        arg0.setOnBufferingUpdateListener(this);
                        arg0.setOnCompletionListener(this);
                        arg0.setOnPreparedListener(this);
                        arg0.setOnVideoSizeChangedListener(this);
                        arg0.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



    }

Please, help me.

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

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

发布评论

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

评论(2

和影子一齐双人舞 2024-12-17 13:32:08

如果您看到此状态图(取自 https://developer.android. com/reference/android/media/MediaPlayer.html#StateDiagram): 在此处输入图像描述

你会意识到你应该打电话给reset() 将其恢复到空闲状态。只有这样你才能调用setDataSource()

If you see this state diagram (taken from https://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram): enter image description here

You will realize that you should call reset() to get it back to the idle state. Only then can you call setDataSource()

月竹挽风 2024-12-17 13:32:08

Android 文档关于 IllegalStateException 的说法:

当虚拟机不在虚拟机中时尝试执行操作时引发
正确的状态。

Android docs say about IllegalStateException:

Thrown when an action is attempted at a time when the VM is not in the
correct state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文