视频动态壁纸更新视频源不起作用

时间:2019-01-16 12:00:41

标签: java android kotlin live-wallpaper

我将视频设置为动态壁纸,并且效果很好。但是当我通过替换路径更改视频时。在“预览”中,它显示了已更改的视频,但是单击“ SET WALLPAPER”后,之前的视频未更改。我的动态壁纸在手机上运行时,如何用新视频更改当前视频。我的墙纸的源代码如下:

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
}

}

当我优先更改链接时,我会通过以下代码调用动态壁纸:

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

以上代码打开动态壁纸的预览模式。在此模式下,墙纸已更改,但是在此预览中单击“设置墙纸”按钮后,无法更改该先前的视频。请帮助我。

0 个答案:

没有答案