本文介绍了ToneGenerator在Android 6.0中崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我正在使用ToneGenerator播放简单的声音.通过使用6.0编译应用程序来测试我的应用程序时,由于ToneGenerator的init方法,我的应用程序随机崩溃.以下是例外.
In my application i am using ToneGenerator to play simple sound. When test my application by compiling the application with 6.0, my application randomy crashing due to ToneGenerator init method. Below is the exception.
java.lang.RuntimeException: Init failed
04-21 12:34:05.497 7166 7166 E MyApplication: at android.media.ToneGenerator.native_setup(Native Method)
04-21 12:34:05.497 7166 7166 E MyApplication: at android.media.ToneGenerator.<init>(ToneGenerator.java:746)
我正在以下面的方式使用声音发生器.
I am using the tone generator in below way.
public ToneGenerator toneGenerator;
public void playSound() {
if (toneGenerator == null) {
toneGenerator = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
}
toneGenerator.startTone(ToneGenerator.TONE_CDMA_ANSWER, 200);
}
public void releaseToneGenerator() {
if (toneGenerator != null) {
toneGenerator.release();
}
}
有人遇到相同的问题吗?..以前我的应用程序在4.4上运行,因此我们没有观察到任何崩溃.在6.0中,应用程序崩溃了
Any one faced same issue?..Previously my application was running on 4.4 and in that we did not observe any crash. In in 6.0 application is crashing
推荐答案
通过使用处理程序解决了该问题.
Solved the issue by using handler.
private static void playTone(Context context, int mediaFileRawId) {
Log.d(TAG, "playTone");
try {
if (toneGenerator == null) {
toneGenerator = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
}
toneGenerator.startTone(mediaFileRawId, 200);
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (toneGenerator != null) {
Log.d(TAG, "ToneGenerator released");
toneGenerator.release();
toneGenerator = null;
}
}
}, 200);
} catch (Exception e) {
Log.d(TAG, "Exception while playing sound:" + e);
}
}
这篇关于ToneGenerator在Android 6.0中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!