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