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?
Activity
in the example you linked has thesurfaceCreated()
method because itimplements SurfaceHolder.Callback
. You can do the same with yourFragment
class. – AmendatoryFragment
to implementSurfaceHolder.Callback
. – Severable