VideoView in a live wallpaper?

元气小坏坏 提交于 2019-12-08 15:28:39

问题


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?


回答1:


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.

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();
                    }
        }

}



回答2:


Short answer is yes. Long answer is http://ikaruga2.wordpress.com/2011/06/15/video-live-wallpaper-part-1/




回答3:


Just to think outside the box, is it possible to take a working video player and re-parent it under a java window in Android? I have not done this in Linux or Android, but under Windows it is possible to get the window handle of a running application and make it a child of a Java frame, with the result that the other application's window looks like its part of your Java application.




回答4:


I have tried the Justin Buser solution and it does not work (tested on a API 16 device), have also found a similar code on https://github.com/thorikawa/AndroidExample/tree/master/MovieLiveWallpaper/; it does not work either. The only solution seems to be to use FFMPEG with NDK. eg : https://github.com/frankandrobot



来源:https://stackoverflow.com/questions/6129593/videoview-in-a-live-wallpaper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!