VideoView一个动态壁纸? [英] VideoView in a live wallpaper?
问题描述
根据其他问题<一href="http://stackoverflow.com/questions/4825913/android-video-as-a-live-wallpaper">android-video-as-a-live-wallpaper,是在一个动态壁纸播放视频的唯一方法就是自己去code呢?
As per other questions android-video-as-a-live-wallpaper, is the only way to play a video in a live wallpaper is to decode it yourself?
推荐答案
刚刚使用,而不是VideoView的MediaPlayer,并使用MediaPlayer.setSurface而不是MediaPlayer.setDisplay。如果您使用setDisplay在MediaPlayer改掉告诉SurfaceHolder保持上这是不允许的LiveWallpapers屏幕,将抛出一个错误。
Just use MediaPlayer instead of VideoView and use MediaPlayer.setSurface instead of MediaPlayer.setDisplay. If you use setDisplay the MediaPlayer trys to tell the SurfaceHolder to keep the screen on which isn't allowed for LiveWallpapers and will throw an error.
我用的WebM / vpx8视频但这应该工作与任何MediaPlayer的支持(只是把水库视频文件/ RAW)
I use WebM/vpx8 video but this should work with whatever MediaPlayer supports (just put the video file in res/raw)
package com.justinbuser.nativecore;
import android.media.MediaPlayer;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;
import com.justinbuser.android.Log;
public class VideoWallpaperService extends WallpaperService
{
protected static int playheadTime = 0;
@Override
public Engine onCreateEngine()
{
return new VideoEngine();
}
class VideoEngine extends Engine
{
private final String TAG = getClass().getSimpleName();
private final MediaPlayer mediaPlayer;
public VideoEngine()
{
super();
Log.i( TAG, "( VideoEngine )");
mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.wallpapervideo);
mediaPlayer.setLooping(true);
}
@Override
public void onSurfaceCreated( SurfaceHolder holder )
{
Log.i( TAG, "onSurfaceCreated" );
mediaPlayer.setSurface(holder.getSurface());
mediaPlayer.start();
}
@Override
public void onSurfaceDestroyed( SurfaceHolder holder )
{
Log.i( TAG, "( INativeWallpaperEngine ): onSurfaceDestroyed" );
playheadTime = mediaPlayer.getCurrentPosition();
mediaPlayer.reset();
mediaPlayer.release();
}
}
}
这篇关于VideoView一个动态壁纸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!