web-dev-qa-db-fra.com

java.lang.IllegalStateException qu'est-ce que cela signifie?

Je développe une application vidéo. Après la première lecture vidéo terminée, dans le "OnCopletion", j'essaie d'en démarrer un nouveau. Mais ça s'arrête (pas de plantages) et ne fait rien. Dans le journal:

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)

qu'est-ce que ça veut dire? Comment puis-je le réparer? il y a mon 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();
                    }



    }

Aidez-moi, s'il vous plaît.

24
SentineL

Si vous voyez ce diagramme d'état (tiré de https://developer.Android.com/reference/Android/media/MediaPlayer.html#StateDiagram ): enter image description here

Vous vous rendrez compte que vous devez appeler reset() pour le ramener à l'état inactif. Ce n'est qu'alors que vous pouvez appeler setDataSource()

115
Reno