【问题标题】:Call recording, call multiple(repetitive) telephonic stages and creates multiple audio files通话录音,通话多个(重复)电话阶段并创建多个音频文件
【发布时间】:2018-10-24 20:34:29
【问题描述】:

我创建了一个服务和广播接收器来获取电话状态。

下面是我的代码:

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_OUT);
        filter.addAction(ACTION_IN);
        if (br_call == null) {
            br_call = new CallBr();
            registerReceiver(br_call, filter);
        }
        return super.onStartCommand(intent, flags, startId);
    }

public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_IN)) {
                if ((bundle = intent.getExtras()) != null) {
                    state =bundle.getString(TelephonyManager.EXTRA_STATE);
                    Log.d("tag", "state ::" + state);
                    if (!wasRinging) {
                     if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                            inCall=bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                            wasRinging = true;
                            Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show();
                        }
                    }
                    if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                        if (wasRinging) {
                            Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show();
                            if (recorder == null) {
                                File sampleDir = new File(Environment.getExternalStorageDirectory(), "/RecordingDemo");
                                if (!sampleDir.exists()) {
                                    sampleDir.mkdirs();
                                }
                                String file_name = inCall;
                                try {
                                    audiofile = File.createTempFile(file_name, ".amr", sampleDir);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                String path = Environment.getExternalStorageDirectory().getAbsolutePath();

                                recorder = new MediaRecorder();
                                recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                                recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                                recorder.setOutputFile(audiofile.getAbsolutePath());

                                try {
                                    recorder.prepare();
                                    recorder.start();
                                    recordstarted = true;
                                } catch (IllegalStateException e) {
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                        wasRinging = false;
                        Toast.makeText(context, "REJECT || DISCONNECT", Toast.LENGTH_LONG).show();
                        if (recordstarted) {
                            try {
                                recorder.stop();
                                recordstarted = false;
                            } catch (RuntimeException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            if (intent.getAction().equals(ACTION_OUT)) {
                if ((bundle = intent.getExtras()) != null) {
                    outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                    Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show();
                }
            }
        }
    }

我面临以下两个问题

1) 我已经打印了不同状态的登录代码,但是所有状态都打印了多次,并且音频文件也为同一个调用创建了多次。

2) 如果我杀死应用程序并重新启动,很多时候它不会记录任何呼叫。我只需要添加新版本。

【问题讨论】:

    标签: java android call call-recording


    【解决方案1】:

    有时广播接收器会为单个事件触发多次。调试一次即可查看。

    如果是这种情况,请使用布尔值或很小的时间延迟来触发onReceive(),每个事件仅触发一次。

    也可以用不同的手机检查。

    【讨论】:

      【解决方案2】:

      最后,我找到了解决方案。请尝试以下代码。

      public int onStartCommand(Intent intent, int flags, int startId) {
              if (intent == null || !ACTION.equals(intent.getAction())) {
                  return super.onStartCommand(intent, flags, startId);
              }
              String state = intent.getStringExtra(STATE);
              String phoneNo = intent.getStringExtra("android.intent.extra.PHONE_NUMBER");
              Log.d("tag", "state: " + state + " phoneNo: " + phoneNo);
              if (OUTGOING.equals(state)) {
                  fileNamePrefix = "mob_no_s" + phoneNo + "mob_no_d" + OUTGOING_CALL_SUFFIX;
              } else if (INCOMING.equals(state)) {
                  fileNamePrefix = "mob_no_s" + phoneNo + "mob_no_d" + INCOMING_CALL_SUFFIX;
              } else if (BEGIN.equals(state)) {
                  if (SecurePreferences.getBooleanPreference(this, Constants.PREF_RECORD_CALLS)) {
                      try {
                          startRecording();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              } else if (END.equals(state)) {
                  try {
                      stopRecording();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              } else if (STORAGE.equals(state)) {
                  if ("mounted".equals(Environment.getExternalStorageState())) {
                      prepareAmrDir();
                  } else {
                      isMounted = false;
                  }
                  if (!isInRecording) {
                      stopSelf();
                  }
              }
              return START_STICKY;
          }
      
          public Context getContext() {
              return cntx;
          }
      
          private void stopRecording() throws IOException {
              updateNotification(Boolean.valueOf(false));
              if (isInRecording) {
                  isInRecording = false;
                  try {
                      recorder.stop();
                      recorder.release();
                      recorder = null;
                  } catch (IllegalStateException e) {
                      e.printStackTrace();
                  }
                  releaseWakeLock();
                  if (SecurePreferences.getBooleanPreference(this, Constants.PREF_SAVE_RECORDING)) {
                      Intent intent = new Intent(this, AutoRunReceiver.class);
                      intent.putExtra("absolutepath", amr.getAbsolutePath());
                      sendBroadcast(intent);
                  }
                  stopSelf();
                  Log.d("tag", "call recording stopped");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2023-03-17
        • 1970-01-01
        • 2021-01-15
        • 2020-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多