简体   繁体   中英

App force closes on playing audio android

i have code for playing two audios, one is a random audio from list and other one is set on button click. but whenever i press the button it keeps force closing randomly. please tell me resolution for this problem. Thanks!

        package com.example.btn;

    import java.net.SocketException;
    import java.util.Random;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;

        public class MainActivity extends Activity {
            Handler mHandler; // global instance
            Runnable your_runnable; // global instance

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

            public void yolo(final View view) {

                if (view == view) {
                    view.setBackgroundResource(R.drawable.btn1);// Change to this when
                                                                // clicked
                    final MediaPlayer mp11 = MediaPlayer.create(getBaseContext(),R.raw.animals009);// get mp3 dir
                    mp11.start(); // play mp3
                    mHandler = new Handler();
                    your_runnable = new Runnable() {

                        @Override
                        public void run() {
                            view.setBackgroundResource(R.drawable.btn2);// Revert back
                                                                        // to this after
                                                                        // timer
                            int[] sounds={R.raw.animals010, R.raw.animals012, R.raw.animals013,R.raw.animals019,R.raw.animals114};
                            Random r = new Random();
                             int Low = 0;
                             int High = 7;
                             int rndm = r.nextInt(High-Low) + Low; 
                             MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
                             mp1.start();




                        }

                    };

                    mHandler.postDelayed(your_runnable, 3000L);// 3sec timer

                }


            }
        }

log cat

09-01 07:38:41.673: V/MediaPlayer(16923): isPlaying: 1
09-01 07:38:41.673: V/MediaPlayer-JNI(16923): isPlaying: 1
09-01 07:38:42.658: D/AndroidRuntime(16923): Shutting down VM
09-01 07:38:42.658: W/dalvikvm(16923): threadid=1: thread exiting with uncaught     exception (group=0x4170fc08)
09-01 07:38:42.663: E/AndroidRuntime(16923): FATAL EXCEPTION: main
09-01 07:38:42.663: E/AndroidRuntime(16923): Process: com.example.btn, PID: 16923
09-01 07:38:42.663: E/AndroidRuntime(16923): java.lang.IllegalStateException: Could not execute method of the activity
09-01 07:38:42.663: E/AndroidRuntime(16923):    at  android.view.View$1.onClick(View.java:3969)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at android.view.View.performClick(View.java:4630)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at     android.view.View$PerformClick.run(View.java:19339)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at     android.os.Handler.handleCallback(Handler.java:733)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at  android.os.Handler.dispatchMessage(Handler.java:95)
 09-01 07:38:42.663: E/AndroidRuntime(16923):   at   android.os.Looper.loop(Looper.java:157)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at android.app.ActivityThread.main(ActivityThread.java:5335)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at java.lang.reflect.Method.invokeNative(Native Method)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at java.lang.reflect.Method.invoke(Method.java:515)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at dalvik.system.NativeStart.main(Native Method)
09-01 07:38:42.663: E/AndroidRuntime(16923): Caused by: java.lang.reflect.InvocationTargetException
09-01 07:38:42.663: E/AndroidRuntime(16923):    at java.lang.reflect.Method.invokeNative(Native Method)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at java.lang.reflect.Method.invoke(Method.java:515)
09-01 07:38:42.663: E/AndroidRuntime(16923):    at android.view.View$1.onClick(View.java:3964)
09-01 07:38:42.663: E/AndroidRuntime(16923):    ... 11 more
09-01 07:38:42.663: E/AndroidRuntime(16923): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=6
09-01 07:38:42.663: E/AndroidRuntime(16923):    at  com.example.btn.MainActivity.yolo(MainActivity.java:28)
09-01 07:38:42.663: E/AndroidRuntime(16923):    ... 14 more
09-01 07:38:43.783: V/MediaPlayer(16923): message received msg=2, ext1=0, ext2=0
09-01 07:38:43.783: V/MediaPlayer(16923): playback complete

You start mp11 immediately after you create it. So It will start playing immediately, which is pretty much the same instant mp1 is playing. If you want it to be delayed, trigger it in your runnable instead.

Additinal:

see the logcat line Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=6 Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=6

You have set up your sounds[] with 5 items, but you are generating a random number between 0 and 7, so your ending up with numbers outside the bounds of the array. You should change your random number generation to between 0 and 4. A more flexible way would be to use the length of the array:

Random r = new Random();
int index = r.nextInt(sounds.length - 1);

after 1-2 minutes, playing with the button, i find there is no more sound coming. can you tell me how to resolve this?

Your comment on my first answer (which I'm pretty sure answered your first two questions by the way), is now actually asking about a completely new problem. That is why I'm putting my response to this new question in a new Answer.

Firstly, MediaPlayers take up system resources. Creating a whole load of them is usually a bad idea. Particularly if you don't clean up after you're done with them (see Releasing the MediaPlayer ). The problem you describe, where the sounds stop playing after 1-2 minutes of using your app, is typical of running out of system resources.

Here is a more resource safe pattern for using a MediaPlayer:

MediaPlayer mediaPlayer;
...
public void btnOnClick(View view) {

    // Setup the MediaPlayer with a resource
    mediaPlayer = MediaPlayer.create(getContext(), R.raw.sound);

    // Set a callback to listen for when the sound finishes playing
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // The sound has finished playing so release the resources being used by the MediaPlayer
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    // Start playback
    mediaPlayer.start();

}

By the way, if this is for something like a game or soundboard, where your sounds are a collection of short clips, you may want to consider using SoundPool instead of MediaPlayer. A quick google for "android SoundPool tutorial" will turn up a load of tuts like for example this one .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM