2

I'm trying to make an app to teach someone how to count music. The media player is initialize to a 30 second sound clip of a persistent A note. I am using a countdown timer to tell my media player when to pause and play. The code below causes the first and last second of audio to stutter.

MediaPlayer myMediaPlayer = MediaPlayer.create(MusicCounting.this, R.raw.a_note);
CountDownTimer time = new CountDownTimer(4000,500) {
    @Override
    public void onTick(long millisUntilFinished) {
        if(myMediaPlayer.isPlaying()) {
            myMediaPlayer.pause();
        } else {
            myMediaPlayer.start();
        }
    }

    @Override
    public void onFinish() {
        myMediaPlayer.pause();
        myMediaPlayer.seekTo(0);
    }
};

time.start();

This code is for quarter notes and my eighth notes code looks identical except the second parameter for the countdown timer is 250. Any suggestions would be greatly appreciated. Thanks :)

mpkuth
  • 6,994
  • 2
  • 27
  • 44

1 Answers1

0

There are lots of reports of Android MediaPlayer stutter with various ways to try to fix it. This seems to be the best related question:

However, AudioTrack might be better for your particular situation where you just want to play specific notes. See:

Community
  • 1
  • 1
mpkuth
  • 6,994
  • 2
  • 27
  • 44
  • Thanks, I'll look into this – user2028508 Jul 02 '15 at 04:01
  • So the MediaPlayer doesn't stutter if I take out the CountDownTimer. Also, I implemented the arbitrary tone and I get a consistent note, but it's duration is an integer so I cannot play the note for less than a second. Any ideas? – user2028508 Jul 02 '15 at 06:45
  • I'm not at my computer right now but I believe that `duration` is only used in conjunction with `sampleRate` to determine `numSamples` so you should be able to tweak that calculation fairly easily. Maybe just make `duration` a float and then cast `numSamples` back to an int? – mpkuth Jul 02 '15 at 07:05
  • You're right with some fiddling I reduced its duration. Thank you for your help. – user2028508 Jul 02 '15 at 18:07