Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToneGenerator crash Android

A couple of days ago, I received a crash log on my released app.

The error comes from the ToneGenerator, and I can't find the problem.

Here, I hava a countdown timer, when the timer reaches 0, the app launch a 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();

}

And the crash log I received:

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)

So what can be the problem? I cheched if the ToneGenerator have a try Catch block, but there wasn't.

I am thinking that maybe the device where the crash occured didn't have an AudioManager.Stream_Notification?

like image 375
FR073N Avatar asked Nov 18 '12 09:11

FR073N


3 Answers

I found this here:

It sounds like your application is not releasing its media player resources. When you're done playing a sound, you need to call the MediaPlayer.release() method. If you're playing a lot of sounds rapidly, the garbage collector won't be able to keep up.

like image 89
zwebie Avatar answered Oct 23 '22 20:10

zwebie


I also had the same issue. You have to release the tone generator after it is finished. So, use the handler with the same millis as for tone generator.

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (toneGenerator != null) {
                            toneGenerator.release();
                            toneGenerator = null;
                        }
                    }

                }, 2000);
like image 2
Kavya Shravan Avatar answered Oct 23 '22 20:10

Kavya Shravan


I used a SoundPool to solve my probem. I read that SoundManager was the best option for playing short sound effect, so I used a solution posted here because ToneGenerator is too unstable it seems.

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);
    }
}

So I'm wondering which use can be given to ToneGenerator when it crash this often.

like image 1
FR073N Avatar answered Oct 23 '22 20:10

FR073N