Search code examples
androidsurfaceviewaidl

How to pass a surfaceview between activity and service through AIDL?


I want to pass the SurfaceView from activity to service using AIDL. In Service I will be rendering the video.


Solution

  • Found the solution, Instead of passing surface view application can pass the Surface which actually implements parcelable interface and instance of Surface class can be passed to MediaPlayer.setSurface() to render the Video.

    In .aidl file

    import android.view.Surface;
    
    oneway interface{
    void startRender( in Surface surface)
    }
    

    in Service

        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.setSurface(surfaceViewId);
    

    In application

     serviceInstance.startRender((((SurfaceView)findViewById(R.id.surfaceView)).getHolder()).getSurface());