Flexo
Flexo

Reputation: 2556

Andengine sound only works sometimes

I have a Sound loaded in the onCreateResources method.

try {
    shield = MusicFactory.createMusicFromAsset(this.getMusicManager(), this, "shield.ogg");
} catch (IOException e) {
    e.printStackTrace();
}

shield.setVolume(1.0f);
shield.setLooping(true);

When i play the sound the first time it works fine, however, the following times i play it is seems random if it works or not.

i use shield.play(); to play the sound and shield.stop(); to stop it again. When it doesnt work i get the following in the log.

AudioFlinger could not create track, status: -12

Error creating AudioTrack

Upvotes: 1

Views: 929

Answers (2)

Chris Ham Join
Chris Ham Join

Reputation: 23

Using Music when you play background music, it will be control long play, repeat.. Using Sound when you play a short audio, example when press button, or shoot..etc Here the my same code using both of them:

public static void controlBgAudio(boolean play, boolean repeat) {
    Music ms = ResourceManager.getInstance().audioBgMusic;
    if (play) {
        if (ms.isReleased()) {
            ms.resume();
        } else {
            ms.play();
        }
    } else {
        if (ms.isPlaying()) {
            ms.pause();
        } else {
            ms.stop();
        }
    }

    if (repeat) {
        ms.setLooping(true);
    } else {
        ms.setLooping(false);
    }
}

public static void controlShootAudio(boolean play, boolean repeat) {
    Sound ms = ResourceManager.getInstance().audioShoot;
    if (play) {
        if (ms.isReleased()) {
            ms.resume();
        } else {
            ms.play();
        }
    } else {
        if (ms.isLoaded()) {
            ms.pause();
        } else {
            ms.stop();
        }
    }

    if (repeat) {
        ms.setLooping(true);
    } else {
        ms.setLooping(false);
    }
}

Upvotes: 2

a_schimpf
a_schimpf

Reputation: 765

You don't use the MusicFactory for sound effects. It is strictly for music.
You are supposed to use the SoundFactory.

Here is an example:

Sound sound = SoundFactory.createSoundFromAsset(getSoundManager(), this, "sound.wav");

Upvotes: 0

Related Questions