ToneGenerator crashes in android 6.0

前端 未结 2 638
小鲜肉
小鲜肉 2020-12-20 00:20

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

2条回答
  •  梦毁少年i
    2020-12-20 01:05

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

提交回复
热议问题