AndEngine SoundManager中出现空指针异常

发布于 2024-12-16 01:38:00 字数 457 浏览 2 评论 0原文

我刚刚编写了从 AndExample 复制的以下几行代码。

try {
        this.collision_sound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "touch.ogg");
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("Error","in creation "+e);
    }

this.collision_sound.play();

现在的问题是,当我播放声音时,它给了我一个空指针异常。任何人都可以帮助我解决这个问题吗?

I just coded the following lines copied from AndExample.

try {
        this.collision_sound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "touch.ogg");
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("Error","in creation "+e);
    }

this.collision_sound.play();

Now The problem is that when i play the sound it gives me a null pointer exception. Any one can help me with this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不必在意 2024-12-23 01:38:00

最后我明白了问题所在。实际上,上面的代码是正确的,我所缺少的是,当我创建引擎时,我缺少为加载引擎中的声音设置选项。

new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera).setNeedsSound(true));

这解决了我缺少 .setNeedsSound(true) 选项的问题。

Finally I got the point where the problem was. Actually the above code is right what I was missing is that when I was creating the engine I was missing to set it options for sounds in on load engine like.

new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera).setNeedsSound(true));

this solved my problem I was missing the .setNeedsSound(true) option.

心凉怎暖 2024-12-23 01:38:00

你的代码似乎没问题。尝试将createsound函数放在AndEngine的onLoad()方法中,并在其他地方调用play()方法。我想你没有给引擎时间来设置声音变量。

Your code seems to be fine. Try to put the createsound function in the onLoad() method of AndEngine and call the play() method in another place. I suppose you're not giving the time to the engine to set up the sound variable.

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