"The surface has been released" error

AndroidOptimist picture AndroidOptimist · Aug 31, 2013 · Viewed 12.3k times · Source

I am creating a media player for online videos, but when I try to run it, I get this error: "The surface has been released". Here is my code:

public class VideoSample1 extends Activity implements  Callback, 
    OnPreparedListener, OnCompletionListener, 
    OnClickListener, OnSeekCompleteListener, android.view.SurfaceHolder.Callback
    {
        public String video_path = "My video URL";
        private SurfaceView surfaceViewFrame;
        private MediaPlayer player;
        private SurfaceHolder holder;
        private Bundle extras;
        private static final String TAG = "log_tag";
        private boolean b =false;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.customvideoview);
            extras = getIntent().getExtras();

            surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
            surfaceViewFrame.setOnClickListener(this);
            surfaceViewFrame.setClickable(false);

            holder = surfaceViewFrame.getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            player = new MediaPlayer();
            player.setOnPreparedListener(this);
            player.setOnCompletionListener(this);

            player.setOnSeekCompleteListener(this);
            player.setScreenOnWhilePlaying(true);
            player.setDisplay(holder);
        }
        @Override
        protected void onDestroy()
        {
            super.onDestroy();
            player.stop();
            player.release();
            player = null;
            Toast.makeText(VideoSample1.this, "back",Toast.LENGTH_SHORT).show();
            finish();
        }
        private void playVideo()
        {

            new Thread(new Runnable() 
            {
                public void run() 
                {
                    try
                    {

                        player.setDataSource(VideoSample1.this, Uri.parse(extras.getString("Video URL")));
                        player.prepareAsync();
                    }
                    catch (IllegalArgumentException e) 
                    {
                        Log.d("admin","Error while playing video");
                        e.printStackTrace();
                        Log.i(TAG,"tag"+ e.getMessage());
                    } 
                    catch (IllegalStateException e) 
                    {
                        Log.d("admin","Error1 while playing video");
                        e.printStackTrace();
                        Log.i(TAG, "tag"+e.getMessage());
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                        Log.d("admin","Error while playing video.Please, check your network connection");
                        Log.i(TAG, "tag"+e.getLocalizedMessage());
                    }
                }
            }).start();
        }


        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
        {

        }

        public void surfaceCreated(SurfaceHolder holder) 
        {
            playVideo();
        }

        public void surfaceDestroyed(SurfaceHolder holder) 
        {

        }

        public void onPrepared(MediaPlayer mp)
        {
            if (!player.isPlaying())
            {
                b = true;
                player.start();
            }

        }

        public void onCompletion(MediaPlayer mp)
        {
            mp.stop();

            finish();
        }


        public void onSeekComplete(MediaPlayer mp)
        {

        }
        @Override
        public void invalidateDrawable(Drawable who) 
        {

        }

        @Override
        public void scheduleDrawable(Drawable who, Runnable what, long when)
        {
    // TODO Auto-generated method stub

        }

        @Override
        public void unscheduleDrawable(Drawable who, Runnable what) 
        {
    // TODO Auto-generated method stub

        }


        @Override
        public void onClick(View v)
        {
    // TODO Auto-generated method stub

        }

}

I referred this , and other stack overflow sites but nothing worked with me :-(. Error is near setdisplay(holder). And here is my Logcat messages:

08-31 10:18:01.512: E/AndroidRuntime(1162): FATAL EXCEPTION: main
08-31 10:18:01.512: E/AndroidRuntime(1162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.msense.msenseplayer/com.msense.msenseplayer.VideoSample1}: java.lang.IllegalArgumentException: The surface has been released
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.os.Looper.loop(Looper.java:137)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.main(ActivityThread.java:4429)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at java.lang.reflect.Method.invoke(Method.java:511)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at dalvik.system.NativeStart.main(Native Method)
08-31 10:18:01.512: E/AndroidRuntime(1162): Caused by: java.lang.IllegalArgumentException: The surface has been released
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.media.MediaPlayer._setVideoSurface(Native Method)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.media.MediaPlayer.setDisplay(MediaPlayer.java:641)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at com.msense.msenseplayer.VideoSample1.onCreate(VideoSample1.java:53)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.Activity.performCreate(Activity.java:4578)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-31 10:18:01.512: E/AndroidRuntime(1162):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-31 10:18:01.512: E/AndroidRuntime(1162):     ... 11 more

Answer

AndroidOptimist picture AndroidOptimist · Sep 3, 2013

Hi finally i resolved my error. Its simple that i added player.setdisplay under surfacecreated method like

public void surfaceCreated(SurfaceHolder holder) 
{ 
playVideo(); 
player.setDisplay(holder); 
} 

and this solved my issue.