[英]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.