How do use a MediaPlayer to a SurfaceView in a Fragment
Asked Answered
N

1

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?

Nobell answered 4/5, 2018 at 18:58 Comment(2)
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.Amendatory
As suggested by @MikeM., make your Fragment to implement SurfaceHolder.Callback.Severable
C
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);
            }
Corpsman answered 9/5, 2018 at 9:44 Comment(3)
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?Nobell
no , you can implement SurfaceHolder.Callback in Fragment to override surfaceCreatedCorpsman
try to implement SurfaceHolder.Callback in fragment , then you can override SurfaceCreatedCorpsman

© 2022 - 2024 — McMap. All rights reserved.