ToneGenerator在Android 6.0中崩溃

时间:2017-04-20 09:31:07

标签: android media tone-generator

在我的应用程序中,我使用ToneGenerator播放简单的声音。通过使用6.0编译应用程序来测试我的应用程序时,我的应用程序由于ToneGenerator init方法而随机崩溃。以下是例外。

 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)

我正在以下方式使用音调发生器。

    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中应用程序崩溃

2 个答案:

答案 0 :(得分:5)

使用处理程序解决了这个问题。

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

答案 1 :(得分:0)

以下是一个基于音频发生器的解决方案,它在DialogFragment中使用ontouch侦听器显得稳定。为OnViewCreated方法中的每个按钮设置onTouchListener,然后使用以下ontouch侦听器方法,该方法在触摸按钮时连续播放SIP呼叫上的所选DTMF音,然后在不再触摸按钮时停止播放。 AnswerDialogListener是MainActivity的回调。

编辑:我通过使用toneType = ToneGenerator.TONE_DTMF_0尝试修复编译器警告,打破了以前工作的DTMF音调RFC2833正确输出;而不是toneType = toneGenerator.TONE_DTMF_0;

private View.OnTouchListener myTouchListener = new View.OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {
        String callidText = callid;
        AnswerDialogListener listener = (AnswerDialogListener) getActivity();
        int streamType = AudioManager.STREAM_MUSIC;
        int volume = 50;
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            switch (v.getId()) {
                case R.id.answer_dialog_0_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad0");
                    toneType = toneGenerator.TONE_DTMF_0;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_1_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad1");
                    toneType = toneGenerator.TONE_DTMF_1;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_2_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad2");
                    toneType = toneGenerator.TONE_DTMF_2;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_3_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad3");
                    toneType = toneGenerator.TONE_DTMF_3;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_4_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad4");
                    toneType = toneGenerator.TONE_DTMF_4;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_5_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad5");
                    toneType = toneGenerator.TONE_DTMF_5;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_6_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad6");
                    toneType = toneGenerator.TONE_DTMF_6;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_7_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad7");
                    toneType = toneGenerator.TONE_DTMF_7;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_8_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad8");
                    toneType = toneGenerator.TONE_DTMF_8;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_9_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "Keypad9");
                    toneType = toneGenerator.TONE_DTMF_9;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_asterisk_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "KeypadAsterisk");
                    toneType = toneGenerator.TONE_DTMF_S;
                    toneGenerator.startTone(toneType);
                    return true;
                case R.id.answer_dialog_hash_button:
                    toneGenerator = new ToneGenerator(streamType, volume);
                    toneGenerator.getAudioSessionId();
                    listener.onAnswerResponse(callidText, "KeypadHash");
                    toneType = toneGenerator.TONE_DTMF_P;
                    toneGenerator.startTone(toneType);
                    return true;
            }
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            switch (v.getId()) {
                case R.id.answer_dialog_0_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_1_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_2_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_3_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_4_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_5_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_6_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_7_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_8_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_9_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_asterisk_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
                case R.id.answer_dialog_hash_button:
                    toneGenerator.stopTone();
                    toneGenerator.release();
                    return true;
            }
        }
        return false;
    }
};

以下是MainActivity回调处理程序中if语句的片段,用于在活动SIP呼叫上输出DTMF:

if (response.equals("Keypad0")) {
            try {
                if (call != null) {
                    call.sendDtmf(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (response.equals("Keypad1")) {
            try {
                if (call != null) {
                    call.sendDtmf(1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }