Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5992455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:32:03+00:00 2026-05-22T23:32:03+00:00

I’m having a problem with Android’s MediaPlayer in that it is too slow when

  • 0

I’m having a problem with Android’s MediaPlayer in that it is too slow when calling the prepare method. I’ve tried to simply keep a Vector of the few MediaPlayer objects (with their preloaded data sources) but calling .start() multiple times results in weird issues.

The first issue is it will skip every other play, and sometimes the play will be half (or less) as loud.

The tones played are very very short but need to be played as quickly as possible. My source code is posted below.

Any help is greatly appreciated.

Kevin

package com.atClass.lemon;

import java.util.Vector;

import com.atClass.cardShoe.SettingTools.SETTING_PREF;
import com.atClass.cardShoe.SettingTools.SETTING_STUB;
import com.atClass.cardShoe.SettingTools.SETTING_VALUE;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.util.Config;
import android.util.Log;

public class MediaHandler {
    public static int cRepeat;
    public static float cVolume = Integer.valueOf(Prefs.cPrefsGet.getString(SETTING_PREF.annunciator_volume.name()+SETTING_STUB._int.name(), PrefDefaults.getDefault(SETTING_PREF.annunciator_volume,SETTING_STUB._int)));
    public static boolean cVolumeEnabled = !(Prefs.cPrefsGet.getString(SETTING_PREF.annunciator_volume.name()+SETTING_STUB._value.name(),PrefDefaults.getDefault(SETTING_PREF.annunciator_volume)).equals(SETTING_VALUE.disabled.name()));

    static Vector <MediaPlayer> cQuickMediaPlayerList = new Vector<MediaPlayer>();

    public static enum AUDIO_CLIP {
        app_boot_sound(R.raw.windows_hardware_insert),
        app_results_sound(R.raw.windows_exclamation),
        app_warning_sound(R.raw.windows_hardware_fail),
        app_card_draw_sound(R.raw.fs_beep5),
        app_lid_open_sound(R.raw.windows_hardware_fail),
        app_top_tigger_overdraw_sound(R.raw.fs_beep6),
        test(R.raw.fs_beep4);

        private int enumResourceId;
        AUDIO_CLIP(int input){ enumResourceId = input;}
        int getItem(){return enumResourceId;}
    }

    public static int getAudioClipIndex(AUDIO_CLIP iAudioClip){
        for (int i=0; i<AUDIO_CLIP.values().length; i++){
            if (AUDIO_CLIP.values()[i] == iAudioClip){
                return i;
            }
        }

        return 0;
    }


    public static void setupQuickMediaPlayer(){
        cQuickMediaPlayerList.clear();
        for (int i=0; i<AUDIO_CLIP.values().length; i++){

            MediaPlayer lMediaPlayer = new MediaPlayer();
            final AssetFileDescriptor afd = Global.gContext.getResources().openRawResourceFd(AUDIO_CLIP.values()[i].getItem());
            try{
                lMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                afd.close();
                lMediaPlayer.prepare();
            }catch(Exception e){}
            lMediaPlayer.setVolume(cVolume,cVolume);
            lMediaPlayer.setLooping(false);
            lMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
                @Override
                public void onCompletion(MediaPlayer lMediaPlayer) {
                    lMediaPlayer.release();
                    try{lMediaPlayer.prepare();}catch(Exception e){e.printStackTrace();}
                }});
            cQuickMediaPlayerList.add(lMediaPlayer);
        }
    }

    public static void playAudio(AUDIO_CLIP iAudioClip){
        float volume = cVolume;

        volume++;
        volume /= 10;

        playAudio(iAudioClip,volume);
    }

    public static void playAudio(final AUDIO_CLIP iAudioClip, final float iVolume){

        Thread lThread = new Thread(new Runnable(){
            public void run() {
                //int resourceId = iAudioClip.getItem();                
                Log.d(Global.TAG,"--> Playing audio clip: " + iAudioClip.name() + "," + iAudioClip.getItem() + "," + getAudioClipIndex(iAudioClip));

                if (cVolumeEnabled == true){

                    //Log.d(Global.TAG,"--> Supplying volume: " + iVolume);
                    //Works but is too slow
//                  try {
//                      final MediaPlayer lMediaPlayer = new MediaPlayer();
//                      AssetFileDescriptor afd = Global.gContext.getResources().openRawResourceFd(iAudioClip.getItem());
//                      lMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
//                      afd.close();
//                      lMediaPlayer.prepare();
//                      lMediaPlayer.setVolume(iVolume,iVolume);
//                      lMediaPlayer.setLooping(false);
//                      lMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
//                          @Override
//                          public void onCompletion(MediaPlayer arg0) {
//                              lMediaPlayer.release();
//                          }});
//                      lMediaPlayer.start();
//                  }catch(Exception e){}

                    try{
                        //Works half the time
                        cQuickMediaPlayerList.get(getAudioClipIndex(iAudioClip)).start();
                    }catch(Exception e){}
                }
            }
        });
        lThread.setPriority(Thread.MAX_PRIORITY);
        lThread.start();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:32:03+00:00Added an answer on May 22, 2026 at 11:32 pm

    You should use SoundPool instead: http://developer.android.com/reference/android/media/SoundPool.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.