Never saw this error, it started to appear on Android 11 when starting a video recording from automatically started foreground service when a device just booted (BOOT_COMPLTED
broadcast)
E/MediaRecorder: setOutputFormat called in an invalid state: 32
W/System.err: java.lang.IllegalStateException
W/System.err: at android.media.MediaRecorder.setOutputFormat(Native Method)
at
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
An interesting fact here that when service starts it can successfully start first video recording and it finishes and stops video recording ok (a video file is good and playable), but when it tries to record next video file then this error appears.
So I checked and I had only one video file (which from logs was the first one reordered when foreground service started on device boot)
So service can't record next video files until I click on foreground service notification (to open my app), then it works ok
It works fine on all previous Android versions (<= 10)
For Android 11 it's absurd that only one video file (first one) is successfully reordered
Yeah I know that it has to be related to those limitations on Android 11 https://developer.android.com/guide/components/foreground-services#bg-access-restrictions
But I don't use audio recording (no micro) for MediaRecorder
I bypassed Camera
start limitation from foreground service by starting such service from a short live activity on BOOT_COMPETED
broadcast with granted SYSTEM_ALERT_WINDOW
permission (draw over all apps) because a foreground service can't start activity without user interaction on newest Android without such permission.
So camera works fine (because there some hacks can still be used without user interaction, yeah... Google...), micro isn't used but still MediaRecorder
successfully records one video file and fails to recording the next ones.
They didn't mentioned it in their new limitations, Google...?
If there was an error with camera it would show with something like this CAMERA_DISABLED (1): connectHelper:1578: Camera "0" disabled by policy
But there is no such issue (already bypassed this limitation), it's E/MediaRecorder: setOutputFormat called in an invalid state: 32
- you can't even find such error on the internet, haha:)
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
BOOT_COMPLETED receiver
<receiver android:name=".service.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
override fun onReceive(context: Context?, intent: Intent?) {
context ?: return
if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Settings.canDrawOverlays(context)) {
Intent(context, ServiceHackActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(this)
}
} else {
// show error to a user: Android 11 is bad, can't do anything automatically for you
// try to start app manually to start service to use camera for video recording
}
} else {
AppForegroundService.startService(context)
}
}
}
p.s. beside Settings.canDrawOverlays(context)
we also have to check if a device is unlocked and screen is on
activity
class ServiceHackActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AppForegroundService.startService(this)
finish()
}
}
After these steps camera can opened just fine on Android 11 :) But here's an error with MediaRecorder
after first recording, absurd...
So annoying!. They added ACCESS_BACKGROUND_LOCATION
, but didn't add ACCESS_BACKGROUND_CAMERA
and so on...
How to implement dashboard camera applications now?
A driver of a car just wants to boot his device and that's all, video recording has to be started automatically, no user interaction!
Auto starting recording options for a dashcam video recorder app:
All these options don't need user interaction...
Update
Another interesting fact that camera can be opened just fine when a foreground service is being started using my method, but if we will stop it once and try to open again then we will receive Camera "0" disabled by policy
error.
So basically MediaRecorder
,Camera
, Microphone
works only once on Android 11, at first use in a foreground service, but if any of that was restarted then you can't use it anymore until there is an interaction from user (he has to start your app at least once, make activity visible).
Seems Google made a bug, it should not work in the first place then... Or what did they want to do...
Due to background limitations of Android 11 I decided to start normal launch activity without finishing it on BOOT_COMPLETED
broadcast when device isn't not locked, no hacks, when this activity starts it also starts needed services for video recording in background.
This the only way which will work normally on device boot for Android 11+.