Image Button sound Slow

+1 Wilford Ice Brimley · October 18, 2014
Hello Everyone!

I Just started a project that allows you to press a couple of image buttons and then your phone plays different wav files. I have the basic functions of this app working but my problem is that when I press multiple buttons fast there is a delay in the sound being played (I want the sound to play the moment I press a button, even if another sound is playing), Does anyone have any suggestions on how to fix this issue?

Thanks!


public class Main extends Activity {

    MediaPlayer upCNote = new MediaPlayer();
    MediaPlayer downCNote = new MediaPlayer();
    MediaPlayer rightCNote = new MediaPlayer();
    MediaPlayer leftCNote = new MediaPlayer();
    MediaPlayer aNote = new MediaPlayer();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton upC = (ImageButton)findViewById(R.id.btnUpC);
        ImageButton downC = (ImageButton)findViewById(R.id.btnDownC);
        ImageButton leftC = (ImageButton)findViewById(R.id.btnLeftC);
        ImageButton rightC = (ImageButton)findViewById(R.id.btnRightC);
        ImageButton aButton = (ImageButton)findViewById(R.id.btnA);
        upCNote = MediaPlayer.create(this, R.raw.upbutton);
        downCNote = MediaPlayer.create(this, R.raw.downbutton);
        rightCNote = MediaPlayer.create(this, R.raw.rightbutton);
        leftCNote = MediaPlayer.create(this, R.raw.leftbutton);
        aNote = MediaPlayer.create(this, R.raw.abutton);

        upC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upCNote.start();

            }
        });
        downC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downCNote.start();

            }
        });
        rightC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightCNote.start();

            }
        });
        leftC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                leftCNote.start();

            }
        });
        aButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aNote.start();

            }
        });

    }

Post a Reply

Replies

Oldest  Newest  Rating
0 Wilford Ice Brimley · October 31, 2014
I moved where the MediaPlayer was initialized. 
0 Alex Sweps · October 21, 2014
How did you fix it?
0 Wilford Ice Brimley · October 21, 2014
Actually, I figured out how to fix it! thank you though!
0 Wilford Ice Brimley · October 21, 2014
here is some of the log:


 E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.IllegalStateException
            at android.media.MediaPlayer._start(Native Method)
            at android.media.MediaPlayer.start(MediaPlayer.java:1304)
            at ocarinasimulator.goldenleafentertainment.com.ocarinasimulator.Main$1.onTouch(Main.java:51)
            at android.view.View.dispatchTouchEvent(View.java:7826)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2131)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2326)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1612)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2494)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2274)
            at android.view.View.dispatchPointerEvent(View.java:8039)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4675)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4563)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4268)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4325)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6317)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6255)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6226)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6397)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:132)
            at android.os.Looper.loop(Looper.java:124)
            at android.app.ActivityThread.main(ActivityThread.java:5493)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)
0 Alex Sweps · October 19, 2014
It might happen if you are not instantiating them in onCreate before releasing them. Whats your logcat say when it crashes on release?
0 Wilford Ice Brimley · October 19, 2014
Thank you! I will have to try that. Also, concerning the .release() method, I have tryed using that and it crashes my app for some reason :(
0 Alex Sweps · October 19, 2014
Also try converting the wav files to ogg. 
0 Alex Sweps · October 19, 2014
I am not sure why it is delaying as the code seems fine. Iv made a few soundboards in Android and they play the sounds without delay when I mash them all at once. The difference though is that they release the sounds before another one plays so they dont play at the same time. My code for creating the sounds and playing them is different to yours so il post it and see if doing it the way I have done it works for you. Kinda sloppy how it creates a fresh object everytime but it does the job.

An awesome feature google have included is the ability to add your listeners in the XML and they handle it all at compile time.  In your XML add this to each button:

android:onClick="onClick1" //onClick1 is whatever name you want just change it for each button.


My buttons and mediaPlayer objects are done like this:

private Button B_0;
private Button B_1;
private Button B_2;
private Button B_3;
private Button B_4;
private Button B_5;

private MediaPlayer M_0;
private MediaPlayer M_1;
private MediaPlayer M_2;
private MediaPlayer M_3;
private MediaPlayer M_4;
private MediaPlayer M_5;

private final MediaPlayer sounds[] = {M_0, M_1, M_2, M_3, M_4, M_5};
private final Button buttons[] = {B_0, B_1, B_2, B_3, B_4, B_5};


private final int soundIDs[] = {R.raw.ansamble_a_gun_army, R.raw.ar_15_gunfire, R.raw.assault_shots, R.raw.barrett_m107_sniper_rifle, R.raw.battlefield_2, R.raw.bullet_whizzing};
private final int btnIDs[] = {R.id.b_ansamble_a_gun_army, R.id.b_ar_15_gunfire, R.id.b_assault_shots, R.id.b_barrett_m107_sniper_rifle, R.id.b_battlefield_2, R.id.b_bullet_whizzing};


//In onCreate
for(int x = 0; x < sounds.length; x++){
sounds[x] = MediaPlayer.create(MainActivity.this, soundIDs[x]);
buttons[x] = (Button) findViewById(btnIDs[x]);
registerForContextMenu(buttons[x]);


public void onClick1(View v){
//This for loop  is to stop the current sound thats playing.
for(int x = 0; x < sounds.length; x++){
sounds[x].release();
}
sounds[1] = MediaPlayer.create(MainActivity.this, soundIDs[1]);  //You can create all of these in a loop in onCreate as you are not releasing them each time. 
sounds[1].start();
  • 1

Java / Android Development

106,939 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator