Saturday, March 24, 2012

Amazon Kindle Fire Sound Delay

A few weeks ago a customer reported that they couldn't download Kindergarten Math from the amazon market on their kindle fire. I did not have a kindle, so, I did not know what to tell the customer. The next day, I went to Fred Meyer and got a Kindle fire.

Fortunately, there was no problem downloading and running Kindergarten Math. But, there was a peculiar sound cut off on the Kindle. I use multiple sound files that are run one after the other for the complete instruction. The number values come from multiple sound files. This way, I don't need a separate sound file for each of the 50 exercises. But, on the kindle, the sound gets cut off at the wrong point, making it sound jerky.

For the first exercise the sounds are
  •  move
  • five
  • balls into the box, using your finger. Press the number
  • five
  • when done

The end part of each sound was getting cut off. It would sound like
  • mo
  • fi
  • balls into the box using your finger. Press the numb
  • fi
  • when done

There was a delay in the sound being played, but not in the reset() being called on the MediaPlayer. My solution is to use 2 MediaPlayer objects. The first media player plays the 1st, 3rd, and 5th sound. So, the reset on the 1st sound is only called after the 2nd sound is complete. The second media player plays the 2nd and 4rth sound. This removes the cut-off, but leads to a little bit of overlap. Still, it sounds better than what it does currently.

Here is some code for reference, if anyone else is having the same problem, or can suggest a better option - 

public class MediaManager implements OnCompletionListener {

static MediaPlayer mp0 = new MediaPlayer();
static MediaPlayer mp1 = new MediaPlayer();

int[] sounds;

int current = -1;

Context context;
public MediaManager (Context context) {
mp0.setOnCompletionListener(this);
mp1.setOnCompletionListener(this);
this.context = context;
}
public void play(int[] sounds) {
this.sounds = sounds;
this.current = 0; //start from 0
mp0.reset();
mp1.reset();
playAudio(mp0);
}
public void stop() {
if(mp0!=null && mp0.isPlaying())
mp0.stop();
if(mp1!=null && mp1.isPlaying())
mp1.stop();
}

@Override
public void onCompletion(MediaPlayer mp) {
if(current>=0 && current<sounds.length) {
current ++;
if(mp==mp0) {
mp1.reset();
playAudio(mp1);
}
else { //mp==mp1
mp0.reset();
playAudio(mp0);
}
}
}
private void playAudio(MediaPlayer mp) {
try {
if(current>=0 && current<sounds.length) {
mp.setDataSource(context, Uri.parse("android.resource://..."));
mp.prepare();
mp.start();
}
else {
current = -1;
}
} catch(Exception e) {
Log.e("Media Player","Media player exception", e);
}
}
}

No comments:

Post a Comment