I'm working on a live wallpaper application. For the service, I'm playing a video as a wallpaper on loop using the MediaPlayer
class. I would like to know if it's possible to draw over the video like it was a canvas, or at least a work around for do something like that (video playing and a drawn generated on top of it). I've been trying to make it work but no success at all after googling a lot and trying different alternatives.
The wallpaper service I'm using:
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
}
}
}
The commented code is one of the things I tried to do it, but it gives java.lang.IllegalArgumentException: canvas object must be the same instance that was previously returned by lockCanvas"
The VideoHolder
is just a class that extends from SurfaceHolder.
If I don't try to draw anything the video just plays fine, and the commented code works if the video is not initialized.
Thanks!