SoundPool在两种情况下崩溃。第一个场景崩溃的代码

时间:2011-02-04 23:59:17

标签: android audio media soundpool

@Ian G. Clifton这是我试过的第一种类型的声池的代码。 当按钮加载活动时,程序FC返回上一个活动。

代码从这里开始,但它不会转移::::::

public class SoundManager {

    private  SoundPool mSoundPool; 
    private  HashMap<Integer, Integer> mSoundPoolMap; 
    private  AudioManager  mAudioManager;
    private  Context mContext;


    public SoundManager()
    {

    }

    public void initSounds(Context theContext) { 
         mContext = theContext;
         mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0); 
         mSoundPoolMap = new HashMap<Integer, Integer>(); 
         mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);          
    } 

    public void addSound(int Index,int SoundID)
    {
        mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
    }

    public void playSound(int index) { 

         int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
         mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
    }

    public void playLoopedSound(int index) { 

         int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
         mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); 
    }

}

:::::::::::::::::::活性:::::::::::::::::::::::::

Activity  {

    private SoundManager mSoundManager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          setContentView(R.layout.nouns);

          mSoundManager = new SoundManager();
          mSoundManager.initSounds(getBaseContext());

          mSoundManager.addSound(1, R.raw.age);
          mSoundManager.addSound(2, R.raw.air);
          mSoundManager.addSound(3, R.raw.anger);
          mSoundManager.addSound(4, R.raw.animal);
          mSoundManager.addSound(5, R.raw.answer);
          mSoundManager.addSound(6, R.raw.apple);
          mSoundManager.addSound(7, R.raw.area);
          mSoundManager.addSound(8, R.raw.arm);
          mSoundManager.addSound(9, R.raw.art);
          mSoundManager.addSound(10, R.raw.atom);
          mSoundManager.addSound(11, R.raw.baby);
          mSoundManager.addSound(12, R.raw.lback);
          mSoundManager.addSound(13, R.raw.ball);
          mSoundManager.addSound(14, R.raw.band);
          mSoundManager.addSound(15, R.raw.bank);


      Button onebutton = (Button) this.findViewById(R.id.n1_button);
            onebutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                  mSoundManager.playSound(1);
                  Toast.makeText(NounsActivity.this, "AGE",
                          Toast.LENGTH_LONG).show();
              }
            });
     Button twobutton = (Button) this.findViewById(R.id.n2_button);
            twobutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                  mSoundManager.playSound(2);
                  Toast.makeText(NounsActivity.this, "AIR",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button threebutton = (Button) this.findViewById(R.id.n3_button);
            threebutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                  mSoundManager.playSound(3);
                  Toast.makeText(NounsActivity.this, "ANGER",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button fourbutton = (Button) this.findViewById(R.id.n4_button);
            fourbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                  mSoundManager.playSound(4);
                  Toast.makeText(NounsActivity.this, "ANIMAL",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button fivebutton = (Button) this.findViewById(R.id.n5_button);
            fivebutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                 mSoundManager.playSound(5);
                  Toast.makeText(NounsActivity.this, "ANSWER",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button sixbutton = (Button) this.findViewById(R.id.n6_button);
            sixbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                  mSoundManager.playSound(6);
                  Toast.makeText(NounsActivity.this, "APPLE",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button sevenbutton = (Button) this.findViewById(R.id.n7_button);
        sevenbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                  mSoundManager.playSound(7);
                  Toast.makeText(NounsActivity.this, "AREA",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button eightbutton = (Button) this.findViewById(R.id.n8_button);
            eightbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                 mSoundManager.playSound(8);
                  Toast.makeText(NounsActivity.this, "ARM",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button ninebutton = (Button) this.findViewById(R.id.n9_button);
            ninebutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {

                  mSoundManager.playSound(9);
                  Toast.makeText(NounsActivity.this, "ART",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button tenbutton = (Button) this.findViewById(R.id.n10_button);
            tenbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  mSoundManager.playSound(10);
                  Toast.makeText(NounsActivity.this, "ATOM",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button elevenbutton = (Button) this.findViewById(R.id.n11_button);
            elevenbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  mSoundManager.playSound(11);
                  Toast.makeText(NounsActivity.this, "BABY",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button twelvebutton = (Button) this.findViewById(R.id.n12_button);
            twelvebutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  mSoundManager.playSound(12);
                  Toast.makeText(NounsActivity.this, "BACK",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button thirteenbutton = (Button) this.findViewById(R.id.n13_button);
            thirteenbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  mSoundManager.playSound(13);
                  Toast.makeText(NounsActivity.this, "BALL",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button fourteenbutton = (Button) this.findViewById(R.id.n14_button);
            fourteenbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  mSoundManager.playSound(14);
                  Toast.makeText(NounsActivity.this, "BAND",
                          Toast.LENGTH_LONG).show();
              }
            });
        Button fifteenbutton = (Button) this.findViewById(R.id.n15_button);
            fifteenbutton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  mSoundManager.playSound(15);
                  Toast.makeText(NounsActivity.this, "BANK",
                          Toast.LENGTH_LONG).show();
              }
            });
         Button Backbutton = (Button) this.findViewById(R.id.Back_Button);
              Backbutton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    finish();
                }
              });
    }
}

::::::::::::尝试了soundpool并得到了错误日志:::::::::::::::

02-04 15:40:05.534: DEBUG/dalvikvm(471): Trying to load lib /system/lib/libsoundpool.so 0x0
02-04 15:40:05.613: DEBUG/dalvikvm(471): Added shared lib /system/lib/libsoundpool.so 0x0
02-04 15:40:09.220: ERROR/AudioCache(172): Heap size overflow! req size: 1049856, max size: 1048576
02-04 15:40:09.223: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.234: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.243: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.254: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.264: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.333: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.333: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.333: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.344: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.344: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.344: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.353: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.353: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.353: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.353: ERROR/AudioCache(172): Heap size overflow! req size: 1052672, max size: 1048576
02-04 15:40:09.364: ERROR/AudioCache(172): Heap size overflow! req size: 1051392, max size: 1048576
02-04 15:40:15.313: WARN/ActivityManager(179): Launch timeout has expired, giving up wake lock!
02-04 15:40:15.363: WARN/ActivityManager(179): Activity idle timeout for HistoryRecord{43c86078 com.BluMouse.MNEKRENFULL2/.NounsActivity}
02-04 15:40:17.943: DEBUG/AndroidRuntime(471): Shutting down VM
02-04 15:40:18.133: WARN/dalvikvm(471): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-04 15:40:18.186: ERROR/AndroidRuntime(471): Uncaught handler: thread main exiting due to uncaught exception
02-04 15:40:19.433: ERROR/AndroidRuntime(471): java.lang.RuntimeException: Unable to start activity ComponentInfo{package.nameActivity}: java.lang.NullPointerException
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.os.Looper.loop(Looper.java:123)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at java.lang.reflect.Method.invoke(Method.java:521)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at dalvik.system.NativeStart.main(Native Method)
02-04 15:40:19.433: ERROR/AndroidRuntime(471): Caused by: java.lang.NullPointerException
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at package.nameActivity.onCreate(nameActivity.java:168)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-04 15:40:19.433: ERROR/AndroidRuntime(471):     ... 11 more
02-04 15:40:19.573: INFO/Process(179): Sending signal. PID: 471 SIG: 3
02-04 15:40:19.583: INFO/dalvikvm(471): threadid=7: reacting to signal 3
02-04 15:40:21.363: INFO/dalvikvm(471): Wrote stack trace to '/data/anr/traces.txt'
02-04 15:40:29.603: DEBUG/dalvikvm(351): GC freed 44 objects / 2136 bytes in 8302ms
02-04 15:40:29.863: DEBUG/dalvikvm(227): GC freed 2193 objects / 127504 bytes in 1879ms

2 个答案:

答案 0 :(得分:0)

我发现了我的问题。这些代码部分不是问题。实际上,Chirag Shah通过他的消沉让我得到了答案。我查看了我的代码,发现我已经将按钮锁定在上一个活动的代码的create部分之外。

我有

Private Button button1;

这是在oncreate方法之前,但是当我删除私有部分时。

Button button1;

现在它运作正常。

答案 1 :(得分:0)

这可以帮助:

E/AudioCache(  227): Heap size overflow! req size: 1052672, max size: 1048576
D/AudioPlayer(  227): size_t android::AudioPlayer::fillBuffer(void*, size_t) EOS reached, return size =4096

android soundpool heapsize overflow