0

I made a small app to practice audio playing in android using MediaPlayer, the app works great but there is a small delay of 1 second after clicking the play button and it's noticeable, I noticed this happens only when starting the audio file, when paused it resumes immediately with no delay,I googled around and seen people suggests using SoundPool instead of MediaPlayer but SoundPool is recommended for short audio clips while my app is playing a full song, what's the cause of this delay? is there a fix or work around this issue?

Here's my code:

private MediaPlayer mMediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMediaPlayer = MediaPlayer.create(this,R.raw.this_is_america);

    Button btnPlay = findViewById(R.id.btnPlay);
    Button btnPause = findViewById(R.id.btnPause);
    Button btnStop = findViewById(R.id.btnStop);
    btnPlay.setOnClickListener(this);
    btnPause.setOnClickListener(this);
    btnStop.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btnPlay:

            Toast.makeText(getApplicationContext(), "Playing song",
                    Toast.LENGTH_SHORT).show();


           mMediaPlayer.start();

            break;

        case R.id.btnPause:

            Toast.makeText(getApplicationContext(), "Pausing song",
                    Toast.LENGTH_SHORT).show();

            mMediaPlayer.pause();
            break;

        case R.id.btnStop:

            Toast.makeText(getApplicationContext(), "Song stopped",
                    Toast.LENGTH_SHORT).show();

          mMediaPlayer.reset();
          mMediaPlayer = MediaPlayer.create(this,R.raw.this_is_america);
            break;
    }

}

Update: Turned out to be the mp3 audio file had silent pause at the beginning , tried another song and it works fine no noticeable delays , thank you greeble31 for suggesting to check that.

Abdelaziz Faki
  • 27
  • 1
  • 10

2 Answers2

1

Your song has a silent pause at the beginning ;)

As @Lucefer mentioned, the Android platform has some small unavoidable latency due to the implementation of the audio stack. Or, at least it did a few years back, not sure what the current state of affairs is. At any rate, this delay is generally far too small (~10ms) to notice at the beginning of an audio file; it has more to do with response times for apps that simulate musical instruments and the like.

greeble31
  • 4,894
  • 2
  • 16
  • 30
  • Hahaha yeah that was embarrassingly stupid of me , however thank you for pointing that out and for sharing this information , 10ms is not noticeable at all, in the future I'll provide more user feedback so the user don't feel like there's a delay of there's nothing playing. – Abdelaziz Faki Nov 04 '18 at 23:26
0

There is an audio latency issue in the Android operating system. There is a few milliseconds delay while audio recording and audio playing. You can learn more about this by navigating below URLs. And this latency is related to the device types.

https://developer.android.com/ndk/guides/audio/audio-latency https://source.android.com/devices/audio/latency/measurements

You can use a native toolkit or native program (C, C++ ndk base) to minimize this latency. But you cannot do reduce it to 0Seconds. only minimize the latency.

There is https://superpowered.com/superpowered-android-media-server . You can get support from this but as I know you need to pay for it. I didn't try it. Therefore i don't know how much it reduced the latency.


If you want to remove silent part from your mp3. you can use ffmpeg wrapper for it. Go to https://github.com/WritingMinds/ffmpeg-android-java link there is a working ffmpeg wrapper for Android. you can easily use it.

using FFMPEG with silencedetect to remove audio silence you can find relevant FFmpeg command for it by navigating to this.

in the wrapper you don't need ffmpeg part. You need to replace it from -y. You can get those details when navigate to that wrapper

PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
  • Thank you Lucefer for your answer, it's useful to know this. – Abdelaziz Faki Nov 03 '18 at 19:39
  • @AZOOZFaki what the exact thing u want to know. I also created audio recording and playing related application and i added manul synchronization for it. What the exact thing you want ? and please upvote this, if it's useful – PushpikaWan Nov 04 '18 at 02:41
  • You shared enough information about android audio latency , the issue was from my audio file had a silent pause from the beginning and I didn't notice that till i tried other audio file (ops :P) , and sorry my reputation is below 15 so I can't vote – Abdelaziz Faki Nov 04 '18 at 23:16