如何使用SurfaceView在MediaPlayer上应用过滤器?

时间:2012-09-29 16:35:23

标签: android android-ndk android-mediaplayer

我需要对Android上播放的一个视频(MediaPlayer)执行过滤,这个过滤器只会将背景变为透明,但我如何读取要处理的字节?因为android似乎没有运行alpha通道电影:(

我需要将视频放在全屏覆盖相机预览(AR)。这就是为什么我需要电影背景透明。

我的想法是将MediaPlayer帧数据(位图数据)发送到NDK进程,然后将处理后的位图返回到surfaceview。

PS:Android API8,目标2.2 +

关注我的代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceView;

public class MovieSurfaceView extends SurfaceView {

    public MovieSurfaceView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
    }

}

/** The main activity for the ImageTargets sample. */
public class ImageTargets extends Activity  implements SurfaceHolder.Callback
{
    private MovieSurfaceView mPreview;

    private MediaPlayer mMediaPlayer;

    private SurfaceHolder holder;

    private String videoPath = "/LAST.3g2";

    protected void onCreate(Bundle savedInstanceState)
    {
        DebugLog.LOGD("ImageTargets::onCreate");
        super.onCreate(savedInstanceState);
        initApplicationAR();
    }

    private void initApplicationAR()
    {        

        mPreview = new MovieSurfaceView(this);

        addContentView(mPreview, new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));

        //        holder = mGlView.getHolder();
        holder = mPreview.getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(this);
    }


    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

    }


    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        mMediaPlayer = new MediaPlayer();

        mMediaPlayer.setDisplay(holder);

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

        mMediaPlayer.setOnPreparedListener(new OnPreparedListener() 
        {
            public void onPrepared(MediaPlayer mp) 
            {
                mp.start();
            }
        }
        );

        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() 
        {

            @Override
                public void onCompletion(MediaPlayer mp) 
            {
                mp.reset();
            }
        }
        );

        iniPlayer();
    }


    @Override
        public void surfaceDestroyed(SurfaceHolder arg0) 
    {
        // TODO Auto-generated method stub

    }  


}

2 个答案:

答案 0 :(得分:1)

我建议将电影帧和相机帧发送到OpenGL。您没有解释如何在电影帧上选择Alpha滤镜,但使用OpenGL,您可以在GPU上使用着色器实现许多滤镜。

答案 1 :(得分:1)

如果你只需要一个使用NDK + OpenGL的玩家(当你编译播放器时设置rgb565 = 0,这将启用alpha通道)

http://code.google.com/p/dolphin-player/

现在解决我的问题的方法如下:

http://code.google.com/p/javacv/

import java.io.File;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FFmpegFrameGrabber;

public class TestCV{

    public static void main(String[] args) throws Exception {

        File f = new File("input.mp4");
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(f);

        final CanvasFrame canvas = new CanvasFrame("My Image");

        canvas.showImage(grabber.grab());

    }
}

只需锁定画布并使用字节,就像我们可以在CameraPreview画布上一样,

感谢所有帮助过的人,