加载soundPool文件48次后应用程序崩溃

时间:2013-10-19 23:46:01

标签: android soundpool

我想知道为什么我的应用程序在加载和播放声音文件正好482次后崩溃了? 在482:e点击(listview)加载并播放mp3声音文件,发生以下情况:

 10-20 01:28:38.454: V/MediaPlayer(14307): decode(1018, 58494525, 47481)
 10-20 01:28:40.424: I/System.out(14307): groupos i metoden getGroupId: 71
 10-20 01:28:40.489: I/System.out(14307): groupos i metoden getGroupId: 71
 10-20 01:28:40.489: V/SoundPoolThread(14307): Got message m=2, mData=480
 10-20 01:28:40.489: V/MediaPlayer(14307): decode(1020, 58494525, 47481)
 10-20 01:28:42.359: I/System.out(14307): groupos i metoden getGroupId: 71
 10-20 01:28:42.424: I/System.out(14307): groupos i metoden getGroupId: 71
 10-20 01:28:42.424: V/SoundPoolThread(14307): Got message m=2, mData=481
 10-20 01:28:42.424: V/MediaPlayer(14307): decode(1022, 58494525, 47481)
 10-20 01:28:44.244: I/System.out(14307): groupos i metoden getGroupId: 71
 10-20 01:28:44.314: I/System.out(14307): groupos i metoden getGroupId: 71
 10-20 01:28:44.314: V/SoundPoolThread(14307): Got message m=2, mData=482
 10-20 01:28:44.314: V/MediaPlayer(14307): decode(-1, 58494525, 47481)
 10-20 01:28:44.314: E/SoundPool(14307): Unable to load sample: (null)
 10-20 01:28:44.324: W/SoundPool(14307): Sample was not loaded. Waiting for 30ms.
 10-20 01:28:44.354: W/SoundPool(14307): Sample was not loaded. Waiting for 30ms.
 10-20 01:28:44.384: W/SoundPool(14307): Sample was not loaded. Waiting for 30ms.
 10-20 01:28:44.414: W/SoundPool(14307): Sample was not loaded. Waiting for 30ms.
 10-20 01:28:44.444: W/SoundPool(14307): Sample was not loaded. Waiting for 30ms.
 10-20 01:28:44.474: E/SoundPool(14307):   sample 482 not READY

和堆栈跟踪

  10-20 01:40:03.639: D/AndroidRuntime(22802): Shutting down VM
  10-20 01:40:03.639: W/dalvikvm(22802): threadid=1: thread exiting with uncaught      exception (group=0x40eb22a0)
  10-20 01:40:03.664: E/AndroidRuntime(22802): FATAL EXCEPTION: main
  10-20 01:40:03.664: E/AndroidRuntime(22802):       android.content.res.Resources$NotFoundException: File res/drawable-ldpi/sam.mp3 from  drawable resource ID #0x7f0203af
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at       android.content.res.Resources.openRawResourceFd(Resources.java:994)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.media.SoundPool.load(SoundPool.java:191)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at  com.example.thai.PhraseActivity.loadSound(PhraseActivity.java:255)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at  com.example.thai.PhraseActivity.access$3(PhraseActivity.java:252)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at com.example.thai.PhraseActivity$2.onChildClick(PhraseActivity.java:130)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:590)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3063)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.widget.AbsListView$1.run(AbsListView.java:3949)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.os.Handler.handleCallback(Handler.java:615)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.os.Handler.dispatchMessage(Handler.java:92)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.os.Looper.loop(Looper.java:137)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.app.ActivityThread.main(ActivityThread.java:4898)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at java.lang.reflect.Method.invokeNative(Native Method)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at java.lang.reflect.Method.invoke(Method.java:511)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at dalvik.system.NativeStart.main(Native Method)
  10-20 01:40:03.664: E/AndroidRuntime(22802): Caused by:   java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is  probably compressed
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.content.res.AssetManager.openNonAssetFdNative(Native Method)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:428)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  at android.content.res.Resources.openRawResourceFd(Resources.java:991)
  10-20 01:40:03.664: E/AndroidRuntime(22802):  ... 17 more

源代码:

  weakExpandableList.get().setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Object e = (Object)adapter.getChild(groupPosition, childPosition);
            int nmbr_childs = adapter.getChildrenCount(groupPosition);
            final int group_position = groupPosition;

            // villkor för childitem. 
            switch (childPosition) {
                case 0:
                    // tester huruvida ett eller två childitems. returvärdet fyller ingen funktion.
                    int x = (nmbr_childs > 1) ? loadAnimation_singleView(v, childPosition) 
                        : loadAnimation_coupleView(v);

                    loadSound(0, group_position);  // Här uppstår krasch ibland!
                break;
                case 1:
                    loadSound(1, group_position);
                    loadAnimation_singleView(v, childPosition); 
                break;              
        }

        return true;
    }
    });  




    private void loadSound(int position, int group_position) {
    switch (position) {
        case 0:
            myVoice = soundPool.load(PhraseActivity.wr.get(), sound[group_position][0], 2);  // Här uppstår en crash ibland - resource not found exeption
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
                    soundPool.play(myVoice, 1, 1, 1, 0, 1.0f);
                } 
            });
        break;
        case 1:
            myVoice = soundPool.load(PhraseActivity.wr.get(), sound[group_position][1], 2);
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
                    soundPool.play(myVoice, 1, 1, 1, 0, 1.0f);
                } 
            });
        break;
    }
}

1 个答案:

答案 0 :(得分:1)

我最好的猜测是你没有正确使用SoundPool。这是你每次加载的MP3吗?从Soundpool javadoc可以看出,SoundPool允许您从磁盘加载音频资源,然后根据需要多次播放它们。如果你每次都在加载和播放相同的片段或几个片段,那么你做错了。