android - AndEngine背景音乐错误

标签 android background andengine playback

我正在 AndEngine 中练习。 我制作了背景、 Sprite ,并添加了运动和重力。

所以我决定练习一下背景音乐和声音,但我遇到了问题。现在,当我启动该应用程序时,出现黑屏, Sprite 画面失真,当然音乐也听不到。

你能帮忙吗?这是代码:

public class MainActivity extends BaseGameActivity {
    Scene scene;
    private Music music;
    protected static final int CAMERA_WIDTH = 850;
    protected static final int CAMERA_HEIGHT = 480;
    BitmapTextureAtlas playerTexture;
    ITextureRegion playerTexureRegion;
    PhysicsWorld physicsWorld;



    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub

         Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        EngineOptions options = new EngineOptions(true,
                ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
        return options;
    }

    @Override
    public void onCreateResources(
            OnCreateResourcesCallback pOnCreateResourcesCallback)
            throws Exception {
        // TODO Auto-generated method stub


         loadGfx();
         loadSounds();

        // resource
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }
    private void loadSounds() {
        // TODO Auto-generated method stub
        try
        {
            music = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), this,"mfx/oldf.ogg");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    private void loadGfx() {
        // TODO Auto-generated method stub
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        // width and height power of 2^x
        playerTexture = new BitmapTextureAtlas(getTextureManager(), 64, 64);
        playerTexureRegion = BitmapTextureAtlasTextureRegionFactory
                .createFromAsset(playerTexture, this, "player.png", 0, 0);
        playerTexture.load();

    }
    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {
        this.scene = new Scene();
        this.scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
        this.music.play();
        physicsWorld = new PhysicsWorld(new com.badlogic.gdx.math.Vector2(0, SensorManager.GRAVITY_EARTH), false);
        this.scene.registerUpdateHandler(physicsWorld);
        createWalls();

        pOnCreateSceneCallback.onCreateSceneFinished(this.scene);

    }

    private void createWalls() {
        // TODO Auto-generated method stub
        FixtureDef WALL_FIX = PhysicsFactory.createFixtureDef(0.0f, 1.0f, 0.0f);
        Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 15, CAMERA_WIDTH,
                15, this.mEngine.getVertexBufferObjectManager());
        ground.setColor(new Color(15, 50, 0));
        PhysicsFactory.createBoxBody(physicsWorld, ground, BodyType.StaticBody,
                WALL_FIX);
        this.scene.attachChild(ground);
    }


    @Override
    public void onPopulateScene(Scene pScene,
            OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
        // TODO Auto-generated method stub

        Sprite sPlayer = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2,
                playerTexureRegion, this.mEngine.getVertexBufferObjectManager());
        sPlayer.setRotation(45.0f);
        final FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(10.0f,
                1.0f, 0.0f);
        Body body = PhysicsFactory.createCircleBody(physicsWorld, sPlayer,
                BodyType.DynamicBody, PLAYER_FIX);
        this.scene.attachChild(sPlayer);
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(sPlayer,
                body, true, false));

        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

谢谢!


更新

日志:

10-09 21:07:46.635: E/AndEngine(5305): MainActivity.onCreateGame failed. @(Thread: 'GLThread 4852')
10-09 21:07:46.635: E/AndEngine(5305): java.lang.IllegalStateException: To enable the MusicManager, check the EngineOptions!
10-09 21:07:46.635: E/AndEngine(5305):  at org.andengine.engine.Engine.getMusicManager(Engine.java:294)
10-09 21:07:46.635: E/AndEngine(5305):  at com.example.prueba2.MainActivity.loadmusic(MainActivity.java:73)
10-09 21:07:46.635: E/AndEngine(5305):  at com.example.prueba2.MainActivity.onCreateResources(MainActivity.java:62)
10-09 21:07:46.635: E/AndEngine(5305):  at org.andengine.ui.activity.BaseGameActivity.onCreateGame(BaseGameActivity.java:181)
10-09 21:07:46.635: E/AndEngine(5305):  at org.andengine.ui.activity.BaseGameActivity.onSurfaceCreated(BaseGameActivity.java:110)
10-09 21:07:46.635: E/AndEngine(5305):  at org.andengine.opengl.view.EngineRenderer.onSurfaceCreated(EngineRenderer.java:80)
10-09 21:07:46.635: E/AndEngine(5305):  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1471)
10-09 21:07:46.635: E/AndEngine(5305):  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1241)

最佳答案

OOOoooo 我现在知道你的问题了,把它添加到你的 EngineOptions 类

   options.getAudioOptions().setNeedsMusic(true);
   options.getAudioOptions().setNeedsSound(true);

启动EngineOptions 时,您必须指明您的场景需要声音。只是您对 EngineOptions 方法稍作疏忽。

关于android - AndEngine背景音乐错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285690/

相关文章:

android - 选择选项卡更改 ListView 项目重复

android - 在 eglSwapBuffers 之后保留后台缓冲区内容

java - JTree - 如何将背景设置为节点

jquery - 页面上有多个弹性 slider 的奇怪闪烁背景

android - AndEngine AnimatedSprite 不显示

android - 可以在对话框而不是 Activity 中显示和引擎场景

Android Instrumented 测试在全部一起运行时失败

android - 如何处理 sleep 模式进入android?

python - 如何从另一个 python 脚本运行 python 脚本而不让辅助脚本中断第一个脚本?

ANDROID ANDENGINE -- 改变 Action 时的 Sprite