我的问题是如下:当通话结束,我得到一个错误。我认为有财产以后与上下文。我在这种情况下,录音机的Widget问题犯规停止录制请帮帮我!
公共类调用扩展的BroadcastReceiver
{
公共无效的onReceive(上下文的背景下,意图意图)
{
捆绑包= intent.getExtras();
如果(空==束)的回报;
字符串状态= bundle.getString(TelephonyManager.EXTRA_STATE);
如果(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
mFileName =/sdcard/Record.3gp;
MediaRecorder mRecorder =新MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
尝试{mRecorder prepare();}
赶上(IOException异常E){}
mRecorder.start();
}
如果(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE))
{
mRecorder.stop();
mRecorder.release();
mRecorder = NULL;
}
}
}
这是从LogCat中:
7月10日至1日:13:28.054:ERROR / AndroidRuntime(553):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):java.lang.RuntimeException的:无法启动接收器xxx.xxx.xxx.Call:显示java.lang.NullPointerException
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在android.app.ActivityThread.access $ 3100(ActivityThread.java:119)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1913)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在android.os.Handler.dispatchMessage(Handler.java:99)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在android.os.Looper.loop(Looper.java:123)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在android.app.ActivityThread.main(ActivityThread.java:4363)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在java.lang.reflect.Method.invokeNative(本机方法)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在java.lang.reflect.Method.invoke(Method.java:521)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在dalvik.system.NativeStart.main(本机方法)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):由:显示java.lang.NullPointerException
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在xxx.xxx.xxx.Call.onReceive(Call.java:49)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):在android.app.ActivityThread.handleReceiver(ActivityThread.java:2637)
7月10日至1号:13:29.134:ERROR / AndroidRuntime(553):10 ...更多
7月10日至1号:13:33.184:ERROR / audio_input(31):不支持的参数:X-PVMF /媒体输入节点/帽配置界面; valtype = key_specific_value
7月10日至1号:13:33.184:ERROR / audio_input(31):VerifyAndSetParameter失败
解决方案
在 mRecorder
是空
时调用结束,因为,永远不会初始化。你应该让 mRecorder
变量类变量和初始化的时候调用启动。
现在,你只是声明了一个新的 MediaRecorder
在的范围if语句
通话开始时。
因此,它应该是这样的:
公共类调用扩展的BroadcastReceiver {
私人MediaRecorder mRecorder;
公共无效的onReceive(上下文的背景下,意图意图){
// ...
如果(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){
mRecorder =新MediaRecorder();
mRecorder.start();
// ....
}
如果(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
如果(mRecorder!= NULL){
mRecorder.stop();
mRecorder.release();
mRecorder = NULL;
}
}
}
}
My problem is follow: when the call ends I get an error. I think there somthing with Context. My problem as in this case Sound Recorder Widget doesnt stop recording Please help me!
public class Call extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if(null == bundle) return;
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
mFileName = "/sdcard/Record.3gp";
MediaRecorder mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {mRecorder.prepare();}
catch (IOException e){}
mRecorder.start();
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE))
{
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
}
This is from LogCat:
10-01 07:13:28.054: ERROR/AndroidRuntime(553): Uncaught handler: thread main exiting due to uncaught exception
10-01 07:13:29.134: ERROR/AndroidRuntime(553): java.lang.RuntimeException: Unable to start receiver xxx.xxx.xxx.Call: java.lang.NullPointerException
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at android.app.ActivityThread.access$3100(ActivityThread.java:119)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:123)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:521)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): Caused by: java.lang.NullPointerException
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at xxx.xxx.xxx.Call.onReceive(Call.java:49)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2637)
10-01 07:13:29.134: ERROR/AndroidRuntime(553): ... 10 more
10-01 07:13:33.184: ERROR/audio_input(31): unsupported parameter: x-pvmf/media-input-node/cap-config-interface;valtype=key_specific_value
10-01 07:13:33.184: ERROR/audio_input(31): VerifyAndSetParameter failed
解决方案
The mRecorder
is null
when the call ends because, is never initialized. You should make the mRecorder
variable a class variable and initialize it when the call starts.
Now, your are just declaring a new MediaRecorder
in the scope of that if statement
when the call starts.
So it should be like this:
public class Call extends BroadcastReceiver{
private MediaRecorder mRecorder;
public void onReceive(Context context, Intent intent){
//...
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){
mRecorder = new MediaRecorder();
mRecorder.start();
//....
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
if(mRecorder!=null){
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
}
}