在android上绘制SurfaceHolder / MediaPlayer?

时间:2014-09-30 23:15:29

标签: java android video wallpaper surfaceholder

我正在开发动态壁纸应用程序。对于该服务,我使用MediaPlayer类在循环播放视频作为壁纸。我想知道是否有可能像画布一样画出视频,或至少做一些像这样的事情(视频播放和在其上生成的绘制)。在谷歌上搜索并尝试不同的替代方案之后,我一直试图让它发挥作用,但根本没有成功。

我使用的壁纸服务:

public class LiveWallpaperService extends WallpaperService {

@Override
public Engine onCreateEngine() {
    ThemeList.init(getApplicationContext());
    return new LiveWallpaperEngine();
}

private class LiveWallpaperEngine extends Engine {

    private MediaPlayer mp;
    private SurfaceHolder sh;
    private Uri L, P;

    public LiveWallpaperEngine() {
        checkConfig();
    }

    public void onSurfaceCreated(SurfaceHolder holder) {
        sh = new VideoHolder(holder);
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        //Check the current configuration
        checkConfig();
        if (visible) playTheme();
        /* 
        Canvas canvas = null;
        try {

            canvas = sh.lockCanvas();

            if (canvas != null) {
                Log.d("DRAW", "DRAWING!");
                Paint p = new Paint();
                p.setColor(Color.WHITE);
                p.setStrokeWidth(8.0f);
                p.setTextSize(100.0f);
                canvas.drawText("Testing", 150, 250, p);
            } else {
                Log.d("DRAW", "NOT DRAWING!");
            }
        } finally {
            sh.unlockCanvasAndPost(canvas);
        }*/
        super.onVisibilityChanged(visible);
    }

    private void playTheme() {
        //Check the current orientation and select the proper file to load
        mp = orientationCheck();
        //Start the video
        mp.setDisplay(sh);
        mp.setLooping(true);
        mp.start();
    }

    private MediaPlayer orientationCheck() {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            return MediaPlayer.create(getApplicationContext(), L);
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            return MediaPlayer.create(getApplicationContext(), P);
        } else {
            return MediaPlayer.create(getApplicationContext(), P);
        }
    }

    private void checkConfig() {
        //This code generates the L and P URIs based on the configuration set on the app
    }
}
}

评论代码是我尝试过的内容之一,但它提供了java.lang.IllegalArgumentException: canvas object must be the same instance that was previously returned by lockCanvas"

VideoHolder只是一个从SurfaceHolder扩展的类。

如果我没有尝试绘制任何内容,那么视频就可以正常播放,如果视频未初始化,则评论代码可以正常工作。

谢谢!

0 个答案:

没有答案