简体   繁体   English

App Force在播放音频android时关闭

[英]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. 创建后立即启动mp11 So It will start playing immediately, which is pretty much the same instant mp1 is playing. 因此,它将立即开始播放,这与mp1播放的瞬间几乎相同。 If you want it to be delayed, trigger it in your runnable instead. 如果要延迟它,请改为在您的runnable中触发它。

Additinal: 附加的:

see the logcat line Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=6 参见logcat行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. 您已经将sounds []设置为5个项目,但是您正在生成一个介于0到7之间的随机数,因此您最终得到的数字超出了数组的范围。 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: 您应该将生成的随机数更改为0到4之间。更灵活的方法是使用数组的长度:

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. 1-2分钟后,使用按钮进行播放,我发现没有声音了。 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. 首先,MediaPlayers占用系统资源。 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 ). 特别是如果您在使用完它们后仍不清理(请参见释放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. 您描述的问题(使用应用程序1-2分钟后声音停止播放)是系统资源不足的典型问题。

Here is a more resource safe pattern for using a MediaPlayer: 这是使用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. 顺便说一句,如果这是用于游戏或音板之类的东西,其中声音是一小段剪辑,则您可能要考虑使用SoundPool而不是MediaPlayer。 A quick google for "android SoundPool tutorial" will turn up a load of tuts like for example this one . 快速谷歌为“机器人的Soundpool教程”将变成像TUTS的负载例如这一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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