ToneGenerator崩溃Android

FR0*_*73N 7 crash audio 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();

}
Run Code Online (Sandbox Code Playgroud)

我收到的崩溃日志:

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)
Run Code Online (Sandbox Code Playgroud)

那可能是什么问题呢?如果ToneGenerator有一个尝试Catch块,我会打瞌睡但是没有.

我想也许发生崩溃的设备没有AudioManager.Stream_Notification?

zwe*_*bie 6

我在这里找到了这个:

听起来你的应用程序没有发布其媒体播放器资源.当你完成播放声音时,你需要调用MediaPlayer.release()方法.如果你快速播放很多声音,垃圾收集器将无法跟上.


FR0*_*73N 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我想知道当 ToneGenerator 经常崩溃时可以给它什么用途。