1

I set video as live wallpaper and it work fine. But when i change that video by replacing the path. In Preview it show the video changed but after click on "SET WALLPAPER" the previous one is not changed. How can i change the current video with new one while my live wallpaper is in running on phone. source code for my wallpaper is below:

class VideoLiveWallpaperService : WallpaperService() {

val PREFS_FILENAME = "com.billionaire.wallpaper.prefs"
val wall = "wall"
var prefs: SharedPreferences? = null

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 )")

        // get pref for wallpapers

        prefs = baseContext.getSharedPreferences(PREFS_FILENAME, 0)
        val wall_path = prefs!!.getString(wall, "null")

        val f = File(Environment.getExternalStorageDirectory().toString() + "/"+wall_path)
        // System.out.println("Path >>>>>>>>>????? " + Environment.getExternalStorageDirectory().absolutePath+"ibad.mov");
        if (wall_path.equals("null") && !f.isDirectory) {
            mediaPlayer = MediaPlayer.create(baseContext,R.raw.test)
            mediaPlayer.isLooping = true
        } else{
        val uri = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/"+wall_path)
            mediaPlayer = MediaPlayer()

            mediaPlayer.setDataSource(baseContext, uri)
            mediaPlayer.prepare()
            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
}

}

When i change the link in preference then i call live wallpaper by following code:

try
                    {
                        val intent = Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                                ComponentName(this@AnimattedViewpagerActivity, VideoLiveWallpaperService::class.java))
                        startActivity(intent)
                    }
                    catch (e:Exception) {
                        val intent = Intent()
                        intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER)
                        startActivity(intent)
                    }

the above code open Preview mode of live wallpaper. in this mode the wallpaper changed but after i click on "Set Wallpaper" button in this Preview, it can't change that previous video. Please help me.

ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40

0 Answers0