简体   繁体   English

在ANDROID中记录去电和呼入电话

[英]Recording OUTGOING CALL and INCOMING CALL in ANDROID

I have found many question about this issue but i am not clear at all. 我已经发现许多有关此问题的问题,但我一点也不清楚。 I want to develop and app so that it can record any outgoing call and Incoming call. 我要开发和应用程序,以便它可以记录任何拨出电话和呼入电话。 I wrote this code below but its not working. 我在下面编写了此代码,但无法正常工作。 Please help details explanation would be preferred.Thanks in advance. 请帮助细节解释为可取。谢谢。

import java.io.File;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class serviceBroadcast extends BroadcastReceiver {
    Context mycontext;
    final MediaRecorder recorder = new MediaRecorder();
    public void onReceive(Context context, Intent intent) {
        mycontext=context;
        try {
            // TELEPHONY MANAGER class object to register one listner
            TelephonyManager tmgr = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            //Create Listner
            TeleListener PhoneListener = new TeleListener();

            // Register listener for LISTEN_CALL_STATE
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }
    class TeleListener extends PhoneStateListener {
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                StopRecording();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                startRecording();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                break;
            default:
                break;
            }

        }
    }
    public void startRecording(){
        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(Environment.getExternalStorageDirectory()+ "/RecordMyVoice.mp4");
        try {
            recorder.prepare();
            Log.i(this.getClass().getName(), "Prepared");
        } catch (IllegalStateException e) {
            e.printStackTrace();
            Log.i(this.getClass().getName(), "ERR 1");
        } catch (IOException e) {
            e.printStackTrace();
            Log.i(this.getClass().getName(), "ERR 2");
        }
        recorder.start(); 
    }
    public void StopRecording(){
        recorder.stop();          
        recorder.release();
        Log.i(this.getClass().getName(), "Stop Recording");
   }

}

} }

Maybe this answer doesn´t fix Your problem, but it´s to long for a comment. 也许这个答案不能解决您的问题,但是需要发表评论。 Without seeing Logcat it´s hard to say, why You get an exception, but one thing I see is, that it maybe could give errors of Your release() call. 没有看到Logcat,很难说为什么会出现异常,但是我看到的一件事是,它可能会给您的release()调用带来错误。 So, if You call release() on a MediaRecorder object, it is in the null-state and can not used after that. 因此,如果您在MediaRecorder对象上调用release(),则它处于空状态,此后不能使用。 So the problem is, You called 所以问题是,你打电话

   final MediaRecorder recorder = new MediaRecorder();

at the beginning. 一开始。 It would be a better practise to make the MediaRecorder Global 最好将MediaRecorder设置为Global

    private MediaRecorder recorder=null;

and create the Object in Your startRecording() method: 并在您的startRecording()方法中创建对象:

    private void startRecording(){

     recorder = new MediaRecorder();
                .
                .
                .

     }

Also, don´t forget to set permissions in manifest: 另外,不要忘记在清单中设置权限:

    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM