1. Check out our companion app, Forums for Android! Download from Google Play

MediaPlayer cuting off in Froyo?

Discussion in 'Application Development' started by Criminosis, Jul 14, 2010.

  1. Criminosis

    Criminosis New Member
    Thread Starter

    Jul 14, 2010
    I'm experiencing this when using MediaPlayer on a 2.2 device (my MotoDroid) and using a 2.2 emulator. I'm making a simple sound board as my first app into Android thinking it would press me into learning a gradual curve of basic processes. And so far it has. Though recently I thought I was using bad sound files, but it appears that my app running in 2.2 causes the ending bit of the sound file to be tapered off. The sound files are at most 1 or 2 seconds long.

    Code (Text):
    1. mp = MediaPlayer.create(this, audioToPlay);
    2.         makeToast(mp);
    3.         mp.start();
    To compare I ran a 2.1 and a 2.2. emulation and installed my app. The 2.1 played the audio files fine, but the 2.2 continued to taper off the endings. I came across another site mentioning duration errors but it was for a different context of us of MediaPlayer. So I created a Toast to show me the duration after clicking.

    Code (Text):
    1. double length = mp.getDuration();
    2.         length = length / 1000;
    3.         String audioLength = new String();
    4.         audioLength = "Audio file is: " + length + "long";
    5.         new Toast(this);
    6.         Toast toast = Toast.makeText(this, audioLength, Toast.LENGTH_SHORT);
    7.         toast.show();
    Interestingly when playing the same file, here are the outputs :

    2.2 : Audio file is 1.635long
    2.1 : Audio file is 1.388long

    To confirm one of the numbers I ran the app in a 1.6 emulator. Like 2.1 it did not cut off the audio playback.

    1.6 : Audio file is 1.388long

    So it appears there is a change that affects 2.2 specifically causing it to not playback properly at least for audio files of this tiny length. Any thoughts or workarounds?


  2. DominicDroid

    DominicDroid New Member

    Aug 24, 2010
    I recently discovered that I had this problem in one of my apps as well. Thanks for the information about it cutting off at the end. At first I thought it was just sounding weird and wasn't quite sure what was happening.

    My solution (not the most elegant, but seems to get the job done)
    1. Open up the file with audacity (freeware program) and add about .15 seconds of silence to the end of the file.
    2. If for some reason you need the sound to repeat quickly, add some logic that can tell if the media player is still playing and pause it.
    3. If you aren't familiar with audacity, when you export an mp3, it likes to add a little bit of silence to the beginning of the file (not sure why). Therefore, I use the media player to "seek ahead" slightly.

    Listed below is part of the code I am using.

    Code (Text):
    1.             if(player.isPlaying())
    2.                 player.pause();
    4.             player.seekTo(50);
    5.             player.start();
  3. alex42

    alex42 New Member

    Jul 25, 2010
    I tried out your solution, but my audio files were so short they needed more than 0.15 extra seconds. My experience was that files below the cutoff (somewhere between 0.275 and 0.28 seconds) play for what seems like the right amount of time, but without producing any sound, while longer files seem to play fine (although there could easily be problems I'm not hearing, since those files are silence after the first 0.05 seconds)

Share This Page