6

Because I want to be able to pass the MediaPlayer that plays the video around, I want to use a SurfaceView instead of a VideoView inside my fragment that plays the video.

I looked at an earlier question about how to attach the MediaPlayer to the SurfaceView. The answer to the question tells me to create two functions:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    mPreview = (SurfaceView)findViewById(R.id.surfaceView);
    holder = mPreview.getHolder();
    holder.setFixedSize(800, 480);
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mp = new MediaPlayer();
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mp.setDisplay(holder);
    play();
}

Unfortunately, I can't overwrite surfaceCreated via a Fragment because a Fragment doesn't have the method. Can I still attach MediaPlayer to a SurfaceView in a Fragment?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • 1
    The `Activity` in the example you linked has the `surfaceCreated()` method because it `implements SurfaceHolder.Callback`. You can do the same with your `Fragment` class. – Mike M. May 07 '18 at 01:48
  • As suggested by @MikeM., make your `Fragment` to implement `SurfaceHolder.Callback`. – azizbekian May 07 '18 at 07:41

1 Answers1

1

Pass Surface View From Fragment to parent Activity using Callback Listener and then attach Media Player from activity to surface View then update Fragment Surface View, demo code , IN Fragment

      SurfaceView mPreview ;

  private MyListener mylistener=null;
  public interface MyListener extends EventListener{
     void onPassSurface(SurfaceView surfaceview);
    }

   public void setListener (MyListener listener)
     {
        mylistener = listener;
      }

     public void updateSurfaceView(SurfaceView surfaceview)
      {
                  mPreview=surfaceview;

                 }

     mPreview =  (SurfaceView)findViewById(R.id.surfaceView);

     mylistener.onPassSurface(mPreview);

IN ACTIVITY

    MyFragment  myfragment=new MyFragmment();
    myfragment.setListener(new MyFragment.MyListener)
     {
       @Override
       void onPassSurface(SurfaceView mPreview)
         {
            //attach Media Player Here or Write Method to attach
            //media player with Surface View and Call from Here

            //after attaching media player call this method
              myfragment.updateSurfaceView(mPreview);
            }
Muhammad Hassaan
  • 874
  • 6
  • 18
  • From the perspective of clean code, it would be preferable if the Activity wouldn't need to have anything to do with the MediaPlayer in the Fragment. Do you believe that it's not possible to do this without adding code to the activity? – Christian May 09 '18 at 09:47
  • no , you can implement SurfaceHolder.Callback in Fragment to override surfaceCreated – Muhammad Hassaan May 09 '18 at 09:54
  • try to implement SurfaceHolder.Callback in fragment , then you can override SurfaceCreated – Muhammad Hassaan May 09 '18 at 09:55