在Android中将视频设置为SET WALLPAPER?

时间:2019-01-01 11:27:46

标签: java android kotlin

我想将视频设置为墙纸,就像ZEDGE墙纸和其他墙纸应用程序在play store中所做的一样。以下是ZEDGE Wallpaper应用程序的屏幕截图,该应用程序在android中将视频设置为墙纸。 enter image description here

我将以下代码用作墙纸:

  val intentt = Intent(Intent.ACTION_ATTACH_DATA)
                    .setDataAndType(uri, "image/*")
                    .putExtra("mimeType", "image/*")
                    .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)


            startActivityForResult(Intent.createChooser(intentt, getString(R.string.setas)), 1)

如何将视频设置为墙纸?

1 个答案:

答案 0 :(得分:1)

您好,我找到了解决问题的方法:

在Kotlin Android中:

VideoLiveWallpaperService类:WallpaperService(){

override fun onCreateEngine(): WallpaperService.Engine {
    return VideoEngine()
}

internal inner class VideoEngine : WallpaperService.Engine() {

    private val TAG = javaClass.simpleName
    private val mediaPlayer: MediaPlayer

    init {
        Log.i(TAG, "( VideoEngine )")
        mediaPlayer = MediaPlayer.create(baseContext, R.raw.video1)
        mediaPlayer.isLooping = true
    }

    override fun onSurfaceCreated(holder: SurfaceHolder) {
        Log.i(TAG, "onSurfaceCreated")
        mediaPlayer.setSurface(holder.surface)
        mediaPlayer.start()
    }

    override fun onSurfaceDestroyed(holder: SurfaceHolder) {
        Log.i(TAG, "( INativeWallpaperEngine ): onSurfaceDestroyed")
        playheadTime = mediaPlayer.currentPosition
        mediaPlayer.reset()
        mediaPlayer.release()
    }
}

companion object {
    protected var playheadTime = 0
}

}

在Java中:

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

}

在清单中:<service android:name=".VideoLiveWallpaperService" android:label="Video live wallpaper" android:permission="android.permission.BIND_WALLPAPER"> <intent-filter> <action android:name="android.service.wallpaper.WallpaperService" /> </intent-filter> <meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" /> </service>

在res / xml / wallpaper.xml中:

<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" />