Wednesday, June 27, 2012

Android: Play video on Top of GLSurfaceView



i modified the GLSurfaceViewActivity from ApiDemo to check to see where can play Video on top of GLSurfaceView.



public class GLSurfaceViewActivity extends Activity implements OnClickListener, OnCompletionListener, OnTouchListener {

@SuppressWarnings("unused")
private static final String TAG = "com.example.android.apis.graphics.GLSurfaceViewActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create our Preview view and set it as the content of our
        // Activity
        mGLSurfaceView = new GLSurfaceView(this);
        mGLSurfaceView.setOnTouchListener(this);
        mGLSurfaceView.setRenderer(new CubeRenderer(false));
        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.addView(mGLSurfaceView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.FILL_PARENT));
       
        mVideoView = new VideoView(this);
        mVideoView.setVisibility(View.INVISIBLE);
        mVideoView.setOnCompletionListener(this);
frameLayout.addView(mVideoView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.FILL_PARENT));
       
        mCheckBox = new CheckBox(this);
        mCheckBox.setText("Load Video");
        mCheckBox.setOnClickListener(this);
frameLayout.addView(mCheckBox, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.CENTER));
        setContentView(frameLayout);
    }

    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        mGLSurfaceView.onPause();
    }

    private GLSurfaceView mGLSurfaceView;
private VideoView mVideoView;
private CheckBox mCheckBox;

@Override
public void onClick(View arg0) {
if(mCheckBox.isChecked()){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
startActivityForResult(Intent.createChooser(intent, "Chose a Video"), 10);
}else{
mVideoView.setVisibility(View.VISIBLE);
mVideoView.start();
mCheckBox.setChecked(true);
}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data!=null && data.getData()!=null){
mCheckBox.setText("Play Video");
mVideoView.setVideoURI(data.getData());
mVideoView.setVisibility(View.VISIBLE);
mVideoView.start();
}else{
mCheckBox.setChecked(false);
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onCompletion(MediaPlayer arg0) {
mVideoView.setVisibility(View.INVISIBLE);

}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mVideoView.setTranslationX(motionEvent.getX());
mVideoView.setTranslationY(motionEvent.getY());
return true;
}
}



3 comments:

NoneyaDBiznazz said...

Does this work?

Could this technique be used to play video in Andengine?

Sudar Nimalan said...

I didn't try in Andengine but it worked in normal android phone.

srinivas reddy said...

update the CubeRenderer class