应用程序关闭播放音频android

时间:2014-09-01 01:52:48

标签: android audio

我有播放两个音频的代码,一个是列表中的随机音频,另一个是按钮点击设置。但每当按下按钮时,它都会随机强行关闭。请告诉我这个问题的解决方案。谢谢!

        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

2 个答案:

答案 0 :(得分:1)

创建后立即启动mp11。所以它会立即开始播放,这几乎是mp1正在播放的瞬间。如果您希望它被延迟,请在您的runnable中触发它。

Additinal:

查看logcat行Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=6

你已经用5个项目设置了你的声音[],但是你正在生成一个0到7之间的随机数,所以你最终得到的数字超出了数组的范围。您应该将随机数生成更改为0到4之间。更灵活的方法是使用数组的长度:

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

答案 1 :(得分:1)

  1-2分钟后,按下按钮,我发现没有了   声音来了。你能告诉我怎么解决这个问题吗?

您对我的第一个答案的评论(顺便提一下,我非常肯定地回答了您的前两个问题),现在实际上是在询问一个全新的问题。这就是我在新答案中对这个新问题做出回应的原因。

首先,MediaPlayers占用系统资源。创建它们的整个负载通常是一个坏主意。特别是如果你在完成它们之后不进行清理(参见Releasing the MediaPlayer)。您描述的问题是,在使用您的应用程序1-2分钟后声音停止播放,这是系统资源耗尽的典型问题。

以下是使用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();

}

顺便说一下,如果这是用于游戏或音板之类的东西,你的声音是短片的集合,你可能要考虑使用SoundPool而不是MediaPlayer。快速google for" android SoundPool教程"会出现像for example this one这样的大量内容。