Filter on playing video on GLSurfaceView at runtim

2019-08-02 04:17发布

问题:

I used https://github.com/krazykira/VidEffects

to apply filter on playing video. But I want to change the filter on click of button at runtime without any glitch on playing video.

According to Applying Effects on Video being Played

I should use

mVideoView.init(mMediaPlayer,new filter)

whenever I want to change filter.But there is no effect on playing video

Can someone help me... I am not experienced in using GLSurfaceView.

Here is my java class

public class MainActivity extends Activity {

private static final String TAG = "MediaPlayerSurfaceStubActivity";

protected Resources mResources;
ShaderInterface d = null;
private  VideoSurfaceView mVideoView = null;
private MediaPlayer mMediaPlayer = null;
private Button button;
LinearLayout linear;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mVideoView = (VideoSurfaceView) findViewById(R.id.mVideoSurfaceView);
    linear = (LinearLayout) findViewById(R.id.linear);
    button = (Button) findViewById(R.id.effects);


//HERE , on click of following button I want the effect to be applied at runtime.


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.gc();

            mVideoView.init(mMediaPlayer, new VignetteEffect(2));

        }
    });
    mResources = getResources();
    mMediaPlayer = new MediaPlayer();

    try {

        AssetFileDescriptor afd = getAssets().openFd("sample.mp4");
        mMediaPlayer.setDataSource(afd.getFileDescriptor(),
                afd.getStartOffset(), afd.getLength());
    } catch (Exception e) {
        e.printStackTrace();
    }

    mVideoView.init(mMediaPlayer,
            new SepiaEffect());

}


@Override
protected void onResume() {
    super.onResume();
    mVideoView.onResume();
}
}

My xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linear"
tools:context="videoeditor.xcs.com.videoeffect.MainActivity">

<com.sherazkhilji.videffect.view.VideoSurfaceView
    android:id="@+id/mVideoSurfaceView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="effects"
    android:id="@+id/effects"/>

</LinearLayout>

In this Sepia Effect is already set but I want to change that to vignette on button click

回答1:

Just add this line in onDrawFrame() and you will get the desired result

        mProgram = createProgram(mVertexShader,
                effect.getShader(mSurfaceView));

Your onDrawFrame() should look something like this:

 @Override
    public void onDrawFrame(GL10 glUnused) {
        synchronized (this) {
            if (updateSurface) {
                mSurface.updateTexImage();
                mSurface.getTransformMatrix(mSTMatrix);
                updateSurface = false;
            }
        }

        mProgram = createProgram(mVertexShader,
                effect.getShader(mSurfaceView));
        GLES20.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT
                | GLES20.GL_COLOR_BUFFER_BIT);

        GLES20.glUseProgram(mProgram);
        checkGlError("glUseProgram");

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID[0]);

        mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
        GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT,
                false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
                mTriangleVertices);
        checkGlError("glVertexAttribPointer maPosition");
        GLES20.glEnableVertexAttribArray(maPositionHandle);
        checkGlError("glEnableVertexAttribArray maPositionHandle");

        mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
        GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT,
                false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
                mTriangleVertices);
        checkGlError("glVertexAttribPointer maTextureHandle");
        GLES20.glEnableVertexAttribArray(maTextureHandle);
        checkGlError("glEnableVertexAttribArray maTextureHandle");

        Matrix.setIdentityM(mMVPMatrix, 0);
        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix,
                0);
        GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        checkGlError("glDrawArrays");
        GLES20.glFinish();

    }