1

The strangest thing. When the animation is playing I hear my sound effect clear but when I don't have the animation playing the sound effect breaks. Here is the code:

private void Feedback(boolean success)
{
    Log.d(TAG, "Feedback");
    if(success)
    {
        PlayCreatureSound();
        ShowAnimatedCreature();
    }
    else
    {
        PlayFailedSound();
    }
}

private void PlayCreatureSound()
{
    Log.d(TAG, "PlayCreatureSound");
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      float curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
      float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      float leftVolume = curVolume/maxVolume;
      float rightVolume = curVolume/maxVolume;      
    spCreatureVoice.play(iCreatureVoicesId[lastCreature.ordinal()], leftVolume, rightVolume, 1, 0, 1);
}

private void PlayFailedSound()
{
    Log.d(TAG, "PlayFailedSound");
    spCreatureVoice.stop(iCreatureVoicesId[FeedbackCreature.FC_COUNT.ordinal()]);
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      float curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
      float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      float leftVolume = curVolume/maxVolume;
      float rightVolume = curVolume/maxVolume;      
    spCreatureVoice.play(iCreatureVoicesId[FeedbackCreature.FC_COUNT.ordinal()], leftVolume, rightVolume, 1, 0, 1);
}

private void ShowAnimatedCreature()
{
    Log.d(TAG, "ShowAnimatedCreature");
    // show the creature
    ibtnShapes[lastTargetLocation].setImageBitmap(bmCreatures[lastCreature.ordinal()]); 
    // animate
    ibtnShapes[lastTargetLocation].startAnimation(rotate[0]);
}

I already tried: 1. switching files 2. using MediaPlayer 3. removing "spCreatureVoice.stop(..." 4. changing priority

Redrori
  • 41
  • 5

2 Answers2

0

It may be because of how you are handling the FeedbackCreature structure, or be in the stop method. Are you trying to play two sound effects at the same time?

Because these two methods share so much code, I suggest you combine them like the following. It might help you narrow down the problem.

private void Feedback(boolean success)
{
    Log.d(TAG, "Feedback");
    if(success)
    {
        int creatureOrdinal = lastCreature.ordinal();
        PlaySound(creatureOrdinal );
        ShowAnimatedCreature(creatureOrdinal );
    }
    else
    {
        int creatureOrdinal = FeedbackCreature.FC_COUNT.ordinal();
        spCreatureVoice.stop(iCreatureVoicesId[creatureOrdinal]);
        PlaySound(creatureOrdinal);
    }
}


private void PlaySound(int creatureOrdinal)
{
    Log.d(TAG, "PlayCreatureSound");
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      float curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
      float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      float leftVolume = curVolume/maxVolume;
      float rightVolume = curVolume/maxVolume;      
    spCreatureVoice.play(iCreatureVoicesId[creatureOrdinal], leftVolume, rightVolume, 1, 0, 1);
}

private void ShowAnimatedCreature(int creatureOrdinal)
{
    Log.d(TAG, "ShowAnimatedCreature");
    // show the creature
    ibtnShapes[lastTargetLocation].setImageBitmap(bmCreatures[creatureOrdinal]); 
    // animate
    ibtnShapes[lastTargetLocation].startAnimation(rotate[0]);
}
Noah
  • 1,966
  • 1
  • 14
  • 29
  • Your code is a bit cleaner but my problem remains. I am only playing 1 stream at a time but only when the animation is playing with it the sound is clear. The animation is a simple rotation. – Redrori Oct 27 '11 at 16:18
  • So then the only different is the spCreatureVoice.stop method, or how this method is called. Please post the stop method, and the portion of the code that calls this on a failure case vrs normal case. – Noah Oct 27 '11 at 16:21
  • The stop is part of the SoundPool api and I did try to remove it with no improvement. Feedback(true) works and Feedback(false) sounds broken. The only thing I can think of is that either this is only happening on the emulator or that android is running stuff in the background if I don't hog all the cpu with the animation. – Redrori Oct 27 '11 at 16:26
  • Sounds pool mentions: Note that since streams can be stopped due to resource constraints, the streamID is a reference to a particular instance of a stream. If the stream is stopped to allow a higher priority stream to play, the stream is no longer be valid. However, the application is allowed to call methods on the streamID without error. This may help simplify program logic since the application need not concern itself with the stream lifecycle. http://developer.android.com/reference/android/media/SoundPool.html – Noah Oct 27 '11 at 16:32
  • OK so I have tried to increase the number of streams in the SoundPool constructor and change the stream type with no improvement. From your quote above and common sense one would think that the sound will break when the animation is playing and not the other way around. – Redrori Oct 27 '11 at 16:45
  • I am currently looking at what this person wrote: – Redrori Oct 27 '11 at 17:02
  • [link]http://stackoverflow.com/questions/7437505/how-to-properly-use-soundpool-on-a-game – Redrori Oct 27 '11 at 17:02
  • Trying to play the sound on a different Thread or Service did not solve my problem – Redrori Oct 30 '11 at 11:16
0

Looks like this is an emulator problem. Not hearing any problems on the actual device.

Redrori
  • 41
  • 5
  • It's pretty well known that the emulator for Android is not the best. It's a good tool, but you should try to do most of your testing on a device. (at least until you need to test for devices you can't afford to buy multiples of ;) ) – Codeman Nov 08 '11 at 20:06