Android 视频作为动态壁纸

发布于 2024-10-14 20:54:51 字数 696 浏览 9 评论 0原文

我正在尝试将视频作为动态壁纸。 我正在使用媒体播放器。我可以获得 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骄傲 2024-10-21 20:54:51

您可以使用 **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.. :)

最美的太阳 2024-10-21 20:54:51

我的猜测是,当前流通的视频动态壁纸使用了完全不同的方法:手动解码媒体并逐帧绘制。我不认为这个问题可以用你的简单方法来解决——否则更多的人已经这样做了。

我假设您有此参考资料,但以防万一: 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

无人问我粥可暖 2024-10-21 20:54:51

发生这种情况的原因是 MediaPlayer 正在调用您传递给它的 SurfaceHolder 的 setKeepScreenOn 方法。您可以通过创建一个自定义 SurfaceHolder 实现类并覆盖 setKeepScreenOn 来解决这个问题,如下所示:

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

然后,当您只需对上面发布的代码进行微小更改时,即:

mp.setDisplay(new VideoSurfaceHolder(holder));

您接下来遇到的问题将是您的视频将播放,但您只能听到音频。经过几个小时的痛苦拉扯头发等之后......您会意识到无论出于何种原因 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:

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.

一城柳絮吹成雪 2024-10-21 20:54:51

该错误听起来像是您在某个地方设置了属性 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文