Voice Call Recording in Android 5.1 - android

I have an application whcih records incoming and outgoing voice calls and it is working fine on devices with marshmallow or higher versions.But when i tried running it on android 5.1.1 it gives me error and app stops responding.
The error shown in logcat is-ava.lang.RuntimeException: Unable to start service com.android.hitech.calls.Unused.MyRecordingService#2cb0b090 with Intent { cmp=com.android.hitech.calls/.Unused.MyRecordingService (has extras) }: java.lang.IllegalStateException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2925)
at android.app.ActivityThread.access$2100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1408)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.android.hitech.calls.Unused.MyRecordingService.onStartCommand(MyRecordingservice.java:88)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2908)
My code for recording the voice calls is-
recorder = new MediaRecorder();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
System.out.println("Present in MIC");
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALLS);
}
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOnErrorListener(this);
recorder.setOnInfoListener(this);
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
Edit- I did as suggested in the answer below but i am the error is still there.The call state is being called only once now by tweaking code a little bit but the app still crashes and even exception is the same.

Because in android 5.1 android system triggers CALL_STATE twice every time...!! i.e. once your NEW_OUTGOING_CALL is started or if any incoming call STATE_RINGING , STATE_ONHOOK and STATE_IDLE gets triggered twice..!! You have to manage it via code.
You can find reference what the problem is over 5.1 here... This is causing error

Related

Android MediaPlayer - setPlaybackParams throws a security exception

I'm trying to play a video faster/slower via the following block of code.
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
socket.send(positionID + ":playState:ready:empty");
player = mp;
player.setPlaybackParams(new PlaybackParams().setSpeed(1.0f));
}
});
I'm passing '1' as the parameter at the moment just for testing, which is supposed to be normal playback speed. But I get the following error regardless of what number I pass.
01-04 18:49:17.308 24548-24548/com.spectiv.slave E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.spectiv.slave, PID: 24548
java.lang.SecurityException
at android.media.MediaPlayer.setPlaybackParams(Native Method)
at com.spectiv.slave.videoActivity$3.onPrepared(videoActivity.java:80)
at android.widget.VideoView$2.onPrepared(VideoView.java:432)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2830)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I'm working targeting API level 23 with a compiled SDK version of 24 and I'm running the application on an Odroid C2. Any help is greatly appreciated.
EDIT - This error doesn't happen on an emulator. I have no idea what's different about the Android running on this Odroid C2. They were both Marshmallow.
I would try with
player.setPlaybackParams(player.getPlaybackParams().setSpeed(1.0f))
assuming that the video is playing without setting the playback params
Just wanted to put this as an answer for anyone searching. The error only happens on this specific piece of hardware (Odroid C2). Running the code in an emulator works.
I got the same problem on C2.
Try release media player onDestroyView() function of your fragment.
or release media player quicker than that.
It appears because pervious media player is not correctly released.
You have not started the Media Player..
player = mp;
player .start()
player.setPlaybackParams(new PlaybackParams().setSpeed(1.0f));

java.lang.NullPointerException IntentService

I'm trying to start a service in a different app that's installed on the device. I get a NullPointerException when using startService
public void notConnected(){
Log.i(TAG,"no connection... reconnecting.");
Intent reset = new Intent("com.famoco.intent.action.TOGGLE_DATA");
Log.i(TAG,"calling " + reset.getAction());
if(reset.getAction().equals("com.famoco.intent.action.TOGGLE_DATA"))
{
startService(reset);
}
else
{
Log.i(TAG,"couldn't start service");
}
}
and this is error log
E/AndroidRuntime: FATAL EXCEPTION: IntentService[MyAppCommService]
Process: com.myapppackage.MyApp, PID: 8583
java.lang.NullPointerException
at android.content.ContextWrapper.startService(ContextWrapper.java:494)
at com.myapppackage.MyApplocation.activity.MyAppActivity.notConnected(MyAppActivity.java:591)
at com.myapppackage.MyApplocation.api.MyAppApi.submitClock(MyAppApi.java:228)
at com.myapppackage.MyApplocation.service.MyAppCommService.submitTags(MyAppCommService.java:52)
at com.myapppackage.MyApplocation.service.MyAppCommService.onHandleIntent(MyAppCommService.java:98)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:61)
Turns out my context returned null, I fixed it by getting the context in onCreate and calling myContext.startservice.
You are starting a new Intent. And looking at that Google example, looks like you use the wrong method to start it up.
- you use:
startService(reset);
- try this one:
startActivity(intent);
But that starts an Activity. And you want an service. I don't think you can do that unless you are the developer of that other app. In general, Android Apps don't have access each others code & resources.
Fixed it by returning context in the onCreate() and then calling context.startService()

Getting a lot of crashes from android youtube player api

I am using version 1.2.1 (tried with latest version 1.2.2) of android's youtube player api. It works fine on most of the devices. However now and then, I keep on getting crashes on crashlytics. I am getting the following crashes
Fatal Exception: java.lang.IllegalStateException: android.os.TransactionTooLargeException
at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.x(SourceFile:558)
at bpd.w(SourceFile:576)
at tef.onTransact(SourceFile:390)
at android.os.Binder.transact(Binder.java:395)
at com.google.android.youtube.player.internal.d$a$a.r(Unknown Source)
at com.google.android.youtube.player.internal.s.h(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerView.e(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerSupportFragment.onSaveInstanceState(Unknown Source)
at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1936)
at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1654)
at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1722)
at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1938)
at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1654)
at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1722)
at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:527)
at com.newshunt.news.activities.NewsBaseActivity.onSaveInstanceState(NewsBaseActivity.java:56)
at com.newshunt.news.activities.NewsDetailsActivity.onSaveInstanceState(NewsDetailsActivity.java:613)
at android.app.Activity.performSaveInstanceState(Activity.java:1388)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1286)
at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4588)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3960)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4023)
at android.app.ActivityThread.access$1200(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1498)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Also getting crashes for the following exception.
Fatal Exception: java.lang.IllegalStateException: android.os.DeadObjectException
at com.google.android.apps.youtube.api.jar.a.eo.surfaceDestroyed(SourceFile:236)
at android.view.SurfaceView.updateWindow(SurfaceView.java:589)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:237)
at android.view.View.dispatchDetachedFromWindow(View.java:12854)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2757)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2757)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3844)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3819)
at android.view.ViewGroup.removeView(ViewGroup.java:3751)
at com.google.android.youtube.player.YouTubePlayerView$1.b(Unknown Source)
at com.google.android.youtube.player.internal.r.h(Unknown Source)
at com.google.android.youtube.player.internal.r$e.onServiceDisconnected(Unknown Source)
at android.app.LoadedApk$ServiceDispatcher.doDeath(LoadedApk.java:1111)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1125)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(NativeStart.java)
The crash happens to appear in specific versions of youtube application like 5.2.27. Although there are a lot of issues filed for these crashes, there has been no reply from the youtube developers on how to mitigate this issue.
Some of the fellow developers have suggested the following workarounds
1) Use loadVideo instead of cueVideo. But I cannot use this workaround because loadVideo always autoplays the video which is not a requirement of my application. Also someone mentioned that with loadVideo also, this problem is happening although in some different version.
2) Put check in the code to check the youtube application version and then put the specific code. Now the problem with this approach is that I have to check each and every version of youtube app ever released and check which versions are causing the issue which is not a good workaround.
Now is there any fix which I can apply to avoid this issue or are the youtube developers planning to release some jar which internally takes care of all these issues?
I reduced the bug occurrence by putting youtube calls (like youtubePlayer.loadVideo(), cueVideo(), getCurrentTimeMillis() etc.) in a try catch block and catch the IllegalStateException exception then reinitialize youtube player.
To create a new instance of the YoutubePlayer just call the initialize() method in the catch block.
Example:
if (youtubePlayer != null) {
try {
youtubePlayer.loadVideo(videoId);
} catch (IllegalStateException e) {
initialize(API_KEY, this);
}
}
but the bug still occurred , I worked around it by catching these exceptions and restart activity. This uncaught exceptions and to catch them you need to use UncaughtExceptionHandler
example :
private Thread.UncaughtExceptionHandler defaultUEH;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
// setup handler for uncaught exception
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e(TAG, "uncaughtException: ", ex);
PendingIntent myActivity = PendingIntent.getActivity(getApplicationContext(),
192837, new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager;
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
15000, myActivity );
System.exit(2);
// re-throw critical exception further to the os (important)
defaultUEH.uncaughtException(thread, ex);
}
};
The android YouTube Player API is not stable, there are known bugs in it. The team from YouTube said that they will release a new version of the library.
For now, the best solution I have found is to build my own library.

DeadObjectException with com.google.android.gms

I'm getting a frequent crash with the log below. It doesn't reference my application code but I'm guessing it may have something to do with GoogleApiClient connecting/disconnecting. Anyone get anything similar to this? I haven't been able to find anything on here.
java.lang.IllegalStateException: android.os.DeadObjectException
at com.google.android.gms.internal.ao.removeAllListeners(Unknown Source)
at com.google.android.gms.internal.ap.disconnect(Unknown Source)
at com.google.android.gms.common.api.b.n(Unknown Source)
at com.google.android.gms.common.api.b.a(Unknown Source)
at com.google.android.gms.common.api.b$2.onConnectionSuspended(Unknown Source)
at com.google.android.gms.internal.r.y(Unknown Source)
at com.google.android.gms.internal.q$a.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.DeadObjectException
at android.os.BinderProxy.transact(Native Method)
at com.google.android.gms.internal.an$a$a.a(Unknown Source)
... 15 more
Possibly where it's happening. I added a try/catch to catch the exception
mGApiClientMgr.addTask(mGApiClientMgr.new GoogleApiClientTask() {
#Override
public void run() {
Log.d(LOG_TAG, "Refreshing data set.");
Location location;
try {
location = LocationServices.FusedLocationApi.getLastLocation(getGoogleApiClient());
onLocationChanged(location);
}
catch(IllegalStateException ex) {
// TODO
}
}
});
where addTask does:
private final LinkedBlockingQueue<GoogleApiClientTask> mTaskQueue = new LinkedBlockingQueue
<GoogleApiClientTask>();
mTaskQueue.offer(task);
This seems related to handlers and message passing...Based on below snippet from your stack trace, gms is seeing a DeadObjectException when trying to process a message on the looper. Even though the stack trace shows gms related, it could have be triggered by your code.
at com.google.android.gms.internal.q$a.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
This exception is seen if the message its trying to access belong to a process that has since exited/killed. Do a code search for all handler sendMessage* message dispatch calls, through out your code. Even this may not catch all instances as some gms calls could result in handler message dispatches.
Also, check if any of your background services, or activities that allocated handler messages, are exiting. Android could be destroying them depending on life cycle states, try overriding onDestroy.
In all your activities/services, any where you make calls to gms api, check the objects you create and pass to gms; If they die, those objects are not valid any more.

Android 2.3 "start failed -2147483648" mediaRecorder error

On Android 2.2 the code works fine on 2.3 it crashes at MediaRecorder.start() though. The log and the code itself is below. As you see it doesn't give much information, log gives "start failed -2147483648" message. I've been trying for ages and can't figure this one out, what am I doing wrong?
mediaRecorder = new MediaRecorder();
try {
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mediaRecorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());
mediaRecorder.prepare();
mediaRecorder.start(); // thats the line code fails
fightTimer.start();
}
catch (RuntimeException ex) {
Toast.makeText(this,"Sorry, camera is currently not available"+ex.toString(), 2000).show();
}
catch (IOException ex) {
Toast.makeText(this,"Sorry, camera is currently not available"+ex.toString(), 2000).show();
}
and log
08-13 06:34:56.914: I/MediaRecorderJNI(2256): prepare: surface=0x2bb230 (identity=13)
08-13 06:34:57.764: E/MediaRecorder(2256): start failed: -2147483648
That's just a bad error code. However, the first line prepare: surface indicates something related to constructing the VideoPlayer bounds and suddenly an exception occurs.
Do you need to setVideoSize(), maybe?
TIP: I'd search for "android mediarecorder start failed" (without the error code - it seems like those error codes are just as random as the error message itself! )
EDIT:
Since it works on Android 2.2, did you look at the API changes between API 2.2 and API 2.3 for MediaRecorder?

Categories

Resources