2012-11-18 235 views
5

幾天前,我收到了我發佈的應用程序的崩潰日誌。ToneGenerator崩潰Android

錯誤來自ToneGenerator,我找不到問題。

在這裏,我有一個倒數計時器,當計時器達到0時,應用程序啓動ToneGenerator。

private void lanceMinuteur(int temps, int color){ 
    if(color == 0) 
     minuteur.setTextColor(getResources().getColor(R.color.color_important)); 
    else 
     minuteur.setTextColor(getResources().getColor(R.color.color_informative)); 

    if(chronoLance){ 
     chrono.cancel(); 
    } 
    chronoLance = true; 

    chrono = new CountDownTimer((temps + 1) * 1000, 1000) { 
     public void onTick(long millisUntilFinished) { 
      minuteur.setText(String.valueOf(millisUntilFinished/1000) + "\""); 
     } 
     public void onFinish() { 
      minuteur.setText("0\""); 
      minuteur.setTextColor(getResources().getColor(R.color.textcolor1design)); 
      chronoLance = false; 
      final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100); //The error is coming from here 
      tg.startTone(ToneGenerator.TONE_PROP_ACK); 
     } 
    }.start(); 

} 

而崩潰日誌我收到:

java.lang.RuntimeException: Init failed 
at android.media.ToneGenerator.native_setup(Native Method) 
at android.media.ToneGenerator.<init>(ToneGenerator.java:798) 
at ma.myperf.musculation.activ.GoPerformanceActivity$2.onFinish(GoPerformanceActivity.java:350) 
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:150) 
at android.app.ActivityThread.main(ActivityThread.java:4385) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
at dalvik.system.NativeStart.main(Native Method) 

那麼什麼可以是問題?如果ToneGenerator有一個try Catch塊,但我沒有。

我在想,也許發生崩潰的設備沒有AudioManager.Stream_Notification?

+1

同樣的問題在這裏:(http://stackoverflow.com/questions/13463691/error-generating-beep-using-tonegenerator-class –

+1

你的代碼在'GoPerformanceActivity.java'中,第350行? – dumbfingers

+0

行350是這行最後ToneGenerator tg =新ToneGenerator(AudioManager.STREAM_NOTIFICATION,100);這是我把「//錯誤來自這裏」 – FR073N

回答

1

我用SoundPool來解決我的問題。我讀過SoundManager是播放短聲效果的最佳選擇,所以我使用了這裏發佈的解決方案,因爲ToneGenerator看起來太不穩定。

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

public void addSound(int index, int soundID) { 
    mAvailibleSounds.add(index); 
    mSoundPoolMap.put(index, mSoundPool.load(mContext, soundID, 1)); 

} 

public void playSound(int index) { 
    // dont have a sound for this obj, return. 
    if (mAvailibleSounds.contains(index)) { 

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

     mKillSoundQueue.add(soundId); 

     // schedule the current sound to stop after set milliseconds 
     mHandler.postDelayed(new Runnable() { 
      public void run() { 
       if (!mKillSoundQueue.isEmpty()) { 
        mSoundPool.stop(mKillSoundQueue.firstElement()); 
       } 
      } 
     }, 3000); 
    } 
} 

所以我想知道哪些使用可以給予ToneGenerator,當它經常崩潰。

5

我發現這個here

這聽起來像你的應用程序沒有釋放其媒體播放器 資源。當您完成播放聲音時,您需要調用MediaPlayer.release()方法 。如果你正在快速播放很多聲音,垃圾收集器將無法跟上。

+0

我認爲你是對的:但我們什麼時候才確切知道什麼時候聲音的播放完成? – FR073N