Android video as a live wallpaper

前端 未结 4 895
野趣味
野趣味 2020-12-13 23:00

i am trying to put video as a live wallpaper. I am using media player for that. i can get SurfaceHolder and i can give that holder to the media player. But its not working f

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 23:40

    The reason this is happening is that MediaPlayer is calling the setKeepScreenOn method of the SurfaceHolder you are passing to it. You can get around this by creating a custom SurfaceHolder implementing Class and override setKeepScreenOn like this:

    package com.justinbuser.videolivewallpapers;
    
    import android.graphics.Canvas;
    import android.graphics.Rect;
    import android.view.Surface;
    import android.view.SurfaceHolder;
    
    public class VideoSurfaceHolder implements SurfaceHolder {
    
        private SurfaceHolder surfaceHolder;
    
        public VideoSurfaceHolder(SurfaceHolder holder) {
            surfaceHolder = holder;
        }
    
        @Override
        public void addCallback(Callback callback) {
            surfaceHolder.addCallback(callback);
        }
    
        @Override
        public Surface getSurface() {
            return surfaceHolder.getSurface();
        }
    
        @Override
        public Rect getSurfaceFrame() {
            return surfaceHolder.getSurfaceFrame();
        }
    
        @Override
        public boolean isCreating() {
            return surfaceHolder.isCreating();
        }
    
        @Override
        public Canvas lockCanvas() {
            return surfaceHolder.lockCanvas();
        }
    
        @Override
        public Canvas lockCanvas(Rect dirty) {
            return surfaceHolder.lockCanvas(dirty);
        }
    
        @Override
        public void removeCallback(Callback callback) {
            surfaceHolder.removeCallback(callback);
        }
    
        @Override
        public void setFixedSize(int width, int height) {
            surfaceHolder.getSurface().setSize(width, height);
            surfaceHolder.setSizeFromLayout();
        }
    
        @Override
        public void setFormat(int format) {
            surfaceHolder.setFormat(format);
        }
    
        @Override
        public void setSizeFromLayout() {
            surfaceHolder.setSizeFromLayout();
        }
    
        @Override
        public void setType(int type) {
            surfaceHolder.setType(SURFACE_TYPE_PUSH_BUFFERS);
        }
    
        @Override
        public void setKeepScreenOn(boolean bool){
            //do nothing
        }
    
        @Override
        public void unlockCanvasAndPost(Canvas canvas) {
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
    

    Then when you would only have to make a minor change to the code you posted above, i.e. :

    mp.setDisplay(new VideoSurfaceHolder(holder));
    

    The problem you are going to have next is going to be that your Video will play but you will only hear audio. After several hours of tormented hair pulling etc... you would have realized that for whatever reason setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) won't work properly. If you call it in onCreate then it works but surfaceCreated etc... never get called, if you call it in onSurfaceCreated then it's too late. Haven't solved that one myself yet but I'll keep you posted.

提交回复
热议问题