ToneGenerator Android的崩溃 [英] ToneGenerator crash Android
问题描述
有一个前几天,我收到了崩溃日志我发布的应用程序。
A couple of days ago, I received a crash log on my released app.
该错误来自ToneGenerator,我不能发现问题。
The error comes from the ToneGenerator, and I can't find the problem.
在这里,我哈瓦一个倒数计时器,当计时器达到0,应用程序启动ToneGenerator。
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();
}
和我收到的崩溃日志:
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)
那么,什么可能是问题吗?我cheched如果ToneGenerator有try catch块,但没有。
So what can be the problem? I cheched if the ToneGenerator have a try Catch block, but there wasn't.
我想,也许该设备所在的崩溃发生没有一个AudioManager.Stream_Notification?
I am thinking that maybe the device where the crash occured didn't have an AudioManager.Stream_Notification?
推荐答案
我用的Soundpool ,以解决我的probem。我读的是SoundManager类播放短声音效果是最好的选择,所以我用在这里发表解决方案,因为ToneGenerator太不稳定似乎。
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);
}
}
所以我不知道它的使用可以给ToneGenerator当它崩溃这往往
So I'm wondering which use can be given to ToneGenerator when it crash this often.
这篇关于ToneGenerator Android的崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!