Android 视频作为动态壁纸
我正在尝试将视频作为动态壁纸。 我正在使用媒体播放器。我可以获得 SurfaceHolder,并且可以将该支架提供给媒体播放器。 但它对我不起作用,它会给我以下异常
LogCat 异常详细信息
ERROR/AndroidRuntime(302): java.lang.UnsupportedOperationException: Wallpapers do not support keep screen on
如果我不给它的媒体播放器持有人它可以工作,但我只能听到音频。 我看到一个应用程序 VideoLiveWallpaper ,它将视频设置为动态壁纸,所以这是可能的,可能是我错过了一些东西。我正在粘贴代码,对此的任何帮助将不胜感激。
代码片段
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mp=MediaPlayer.create(getApplicationContext(), R.raw.sample);
mp.setDisplay(holder);
mp.start();
}
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 for me, its giving me following exception
LogCat Exception Detail
ERROR/AndroidRuntime(302): java.lang.UnsupportedOperationException: Wallpapers do not support keep screen on
if i dont give holder to the media player it works, but i can hear only audio.
I saw one application VideoLiveWallpaper , which set video as a live wallpaper, so it can be possible, may be i am missing something . I am pasting the code, any help on this will be appreciated.
Code Snippet
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mp=MediaPlayer.create(getApplicationContext(), R.raw.sample);
mp.setDisplay(holder);
mp.start();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
**mediaPlayer.setSurface(surfaceHolder.getSurface())**
而不是使用**mediaPlayer.setDisplay(surfaceHolder)**
..它不会给出与属性 KeepScreenOn 发生任何类型的冲突。
恩乔伊..:)
Instead of using
**mediaPlayer.setDisplay(surfaceHolder)**
you can use**mediaPlayer.setSurface(surfaceHolder.getSurface())**
..It will not give any kind of conflicts with the attribute KeepScreenOn.
NJOY.. :)
我的猜测是,当前流通的视频动态壁纸使用了完全不同的方法:手动解码媒体并逐帧绘制。我不认为这个问题可以用你的简单方法来解决——否则更多的人已经这样做了。
我假设您有此参考资料,但以防万一: http://forum.xda -developers.com/showthread.php?t=804720
明确提及不同的视频格式让我相信开发人员正在做自己的解码......
祝你好运,
乔治
My guess is that the Video Live Wallpaper currently in circulation is using a totally different approach: decoding the media manually and drawing it frame by frame. I do not think this problem can be tackled using your simple method--otherwise more people would have already done it.
I assume you have this reference, but just in case: http://forum.xda-developers.com/showthread.php?t=804720
The explicit mention of differing video formats leads me to believe the developer is doing his own decoding...
Good luck,
George
发生这种情况的原因是 MediaPlayer 正在调用您传递给它的 SurfaceHolder 的 setKeepScreenOn 方法。您可以通过创建一个自定义 SurfaceHolder 实现类并覆盖 setKeepScreenOn 来解决这个问题,如下所示:
然后,当您只需对上面发布的代码进行微小更改时,即:
您接下来遇到的问题将是您的视频将播放,但您只能听到音频。经过几个小时的痛苦拉扯头发等之后......您会意识到无论出于何种原因 setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) 都无法正常工作。如果你在 onCreate 中调用它,那么它可以工作,但 surfaceCreated 等...永远不会被调用,如果你在 onSurfaceCreated 中调用它,那就太晚了。我自己还没有解决这个问题,但我会及时通知您。
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:
Then when you would only have to make a minor change to the code you posted above, i.e. :
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.
该错误听起来像是您在某个地方设置了属性 KeepScreenOn。它可能位于您的清单中、定义布局的 xml 中或主代码中的某个位置。按照 logcat 输出找到它并尝试将其删除。
The error sounds like somewhere you have set the attribute, KeepScreenOn. It could be in your manifest, the xml defining your layout or somewhere in your main code. Follow the logcat output to find it and try removing it.