简体   繁体   English

我正在尝试记录通话。 我可以录音,但我不知道在哪里停止录音。 下面是我的代码

[英]I am trying to record calls. I can record call but i dont know where to stop recorder. Below is my Code

Here is my CallBroadcastReceiver and in that I am trying to record call. 这是我的CallBroadcastReceiver,我正在尝试记录通话。 Its work properly without recorder.stop method. 没有recorder.stop方法,它可以正常工作。 When I add stop method it crash the application. 当我添加stop方法时,它会使应用程序崩溃。

Give me suggestion for where I should call StopRecording Method. 给我建议我应该在哪里调用StopRecording方法。 So that i can resume the recoder and save the file in Sdcard. 这样我就可以恢复编码器并将文件保存在SD卡中。

I am trying to put stopRecording in TelephonyManager.CALL_STATE_IDLE switch case but it just crash recording. 我试图将stopRecording放在TelephonyManager.CALL_STATE_IDLE开关中,但它会崩溃。 And does not record anything or not create file. 并且不记录任何内容或不创建文件。

public class CallBroadcastReceiver extends BroadcastReceiver 
{
String path = "/sdcard/AudioRecording";
private final int audioformat = 3;
public class MyPhoneStateListener extends PhoneStateListener implements  
SensorEventListener 
{
     Context context;
    AudioManager audioManager;
    MediaRecorder recorder;
    private SensorManager mSensorManager;
    private Sensor myLightSensor;
    private boolean CallState;
    private float sensorState;
    public MyPhoneStateListener(Context arg0) 
    {
        // TODO Auto-generated constructor stub
        this.context = arg0;
        mSensorManager = (SensorManager)
this.context.getSystemService(Context.SENSOR_SERVICE);
        myLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        audioManager = (AudioManager)
this.context.getSystemService(Context.AUDIO_SERVICE);
        if (myLightSensor == null)
        {
            Log.i("On Receive", "Not Support");
        }
        else
        {
mSensorManager.registerListener(this,myLightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) 
    {
        // TODO Auto-generated method stub
        switch (state) 
        {
        case TelephonyManager.CALL_STATE_IDLE:

            System.out.println("My Call IDLE");
            CallState = false;
            StartAudioSpeacker();
            //StopRecording();
            System.out.println("Is phone speaker : "+
audioManager.isSpeakerphoneOn());
            if (audioManager.isSpeakerphoneOn()) {
                audioManager.setSpeakerphoneOn(false);
                mSensorManager.unregisterListener(this);
            }
            break;

        case TelephonyManager.CALL_STATE_OFFHOOK :

            System.out.println("My Call OFFHOOK");
            CallState = true;
            StartAudioSpeacker();
            StartRecording();
            System.out.println("Is phone speaker : "+
audioManager.isSpeakerphoneOn());
            break;

        case TelephonyManager.CALL_STATE_RINGING :
             System.out.println("My Call RINGING");
             break;
        default:
            break;
        }
    }

    private void StartAudioSpeacker() 
    {
        // TODO Auto-generated method stub
        if (CallState && sensorState == 1.0) 
        {
            audioManager = (AudioManager)
this.context.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setSpeakerphoneOn(true);
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
            audioManager.setStreamVolume(AudioManager.MODE_IN_CALL,  
audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL), 1);
            System.out.println("Is phone speaker : "+ 
audioManager.isSpeakerphoneOn());
        }
        else
        {
            audioManager = (AudioManager)
this.context.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setSpeakerphoneOn(false);
            audioManager.setStreamVolume(AudioManager.MODE_IN_CALL,
audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL), 1);
            System.out.println("Speaker Volume :"+ 
audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL));
            System.out.println("Is phone speaker : "+ 
audioManager.isSpeakerphoneOn());
        }

    }
    private void StartRecording() 
    {
        // TODO Auto-generated method stub

         recorder = new MediaRecorder();
         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         recorder.setOutputFormat(audioformat);
         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
         recorder.setOutputFile(path+"test.amr");
         try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        recorder.start();   // Recording is now started
        Log.i(this.getClass().getName(), "Start Recording");

    }

    private void StopRecording() 
    {
        // TODO Auto-generated method stub
        recorder.stop();
        recorder.reset();   
        recorder.release();
        Log.i(this.getClass().getName(), "Stop Recording");
    }
@Override
    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        // TODO Auto-generated method stub
        if (arg0.getType() == Sensor.TYPE_PROXIMITY) 
        {
            Log.i("Sensor Changed", "Accuracy :" + arg1);
        }

    }
    @Override
    public void onSensorChanged(SensorEvent arg0) 
    {
        // TODO Auto-generated method stub
        if (arg0.sensor.getType() == Sensor.TYPE_PROXIMITY) 
        {
            Log.i("Sensor Changed", "onSensor Change :" + arg0.values[0]);
            sensorState = arg0.values[0];
            StartAudioSpeacker();
        }
    }
}
public void onReceive(Context arg0, Intent arg1) 
{

      TelephonyManager tmanager = 
(TelephonyManager)arg0.getSystemService(Context.TELEPHONY_SERVICE);
      MyPhoneStateListener PhoneListener = new MyPhoneStateListener(arg0);
     tmanager.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}       

}   

Just stop the recording when you get CALL_STATE_IDLE as the new state. 当您将CALL_STATE_IDLE设为新状态时,只需停止录制即可。 This means that the call has ended... 这表示通话已结束...

暂无
暂无

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

相关问题 如何进行 API 调用。 我是初学者 - How to make API calls. I am a beginner 我正在尝试使用服务录制视频 - I am trying to record a video using a service 我一直在制作一个应用程序,希望为其所有呼叫维护私人呼叫日志。 有人可以帮我吗 - I have been making an app for which I want to maintain a private call log for all my calls. Can someone please help me out with this 我想知道是否可以实现录音并在Android 7上录音? - I want to know is it possible and will it be implemented to record calls on Android 7? 我正在使用 ViewModel Live 数据进行 UI 更新。 我想使用 Retrofit 进行网络调用。 我可以在服务(意图服务)中使用 retrofit 调用吗? - I am using ViewModel Live data for UI update. I want to use Retrofit for network calls. Can I use retrofit calls in Service(intent Service)? 我无法记录我的FramLayout此代码是全屏记录 - I cannot record my FramLayout this code are whole screen record 我想使用媒体录制器在android中录制视频 - I want to record a video in android using media recorder 我不知道将意图放在进度栏中 - I dont know where to put the intent in a progressbar 我正在尝试开发一项活动,用户可以在其中录制音频并将其保存在内部存储中 - I am trying to develop an activity in which user can record and save there audio in the internal storage 为什么我无法从应用程序中删除Firebase记录? - Why am I unable to delete a firebase record from my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM