Why is my MediaPlayer audio distorted/clipping?

Viewed 1391

When I play a sound in my app it comes off as clippy and distorted. Here is a recording: recording.

Here is the sound file as it was uploaded to android studio: success sound cue

Here is the function that calls the sound from the .raw

 public void playCorrectAnswerSound() {
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.correct);
        mp.start();
    }

Heres how I call it:

Thread t = new Thread(){
            public void run(){
                playCorrectAnswerSound();
            }
        };
        t.start()

This is my first time debugging a sound related issue. I don't know what else to include in this post, so if you need more info please say so.

EDIT: I was asked to record more of the distortion. Here it is. Also I should say that after more testing, my physical device lacks the sound distortion while the sound distortion is present on 3 different emulators.

1 Answers

I'm going to say this is stuttering due to underrun (starvation) of the MediaPlayer's internal playback buffer. That is, the MediaPlayer can't supply data fast enough to keep up with the sound hardware (which suggests a severe lack of processing power). If the buffer starves, it'll start to play back old data (because it's a circular buffer). This causes a sharp phase transition, which sounds like a "click". Presumably the MediaPlayer recovers quickly enough that the "correct" sound resumes playing shortly thereafter.

Here is a picture of the spectrum from Audacity. 0-4KHz. The first row is the clean .mp3; the next four rows are the distorted recordings (in no particular order). All rows have been aligned in time, and are roughly the same amplitude. The large vertical stripes in the last four rows represent the distortion/clicks that you hear.

5 rows of sound spectrum - Audacity screenshot

Related