<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TheChemical</title>
    <description>The latest articles on DEV Community by TheChemical (@thechemical).</description>
    <link>https://dev.to/thechemical</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3202254%2Fe2a9e8e7-b3b1-408b-9b73-c3de2a505c43.png</url>
      <title>DEV Community: TheChemical</title>
      <link>https://dev.to/thechemical</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thechemical"/>
    <language>en</language>
    <item>
      <title>My recorder app deleted interrupted recordings that would have played</title>
      <dc:creator>TheChemical</dc:creator>
      <pubDate>Thu, 19 Nov 2026 00:06:28 +0000</pubDate>
      <link>https://dev.to/thechemical/my-recorder-app-deleted-interrupted-recordings-that-would-have-played-4m5f</link>
      <guid>https://dev.to/thechemical/my-recorder-app-deleted-interrupted-recordings-that-would-have-played-4m5f</guid>
      <description>&lt;p&gt;"I lost my 1 hour recording." That is a 2-star Google Play review of &lt;a href="https://play.google.com/store/apps/details?id=me.jagar.xscamera2" rel="noopener noreferrer"&gt;XSCamera&lt;/a&gt;, the Android background video recorder I work on, written on a OnePlus Nord CE5 with Android 16 and app version 4.0.9. Two more reviews in the same seven days said a recording was gone or would not open.&lt;/p&gt;

&lt;p&gt;I have none of their files, so I read every code path in the app that can remove a video. One function deleted every &lt;code&gt;.mp4&lt;/code&gt; in the app's folder that had no database row, each time the recordings list opened. A recording whose process is killed never gets a row. The function assumed such a file cannot be played. On four phones with Android 13 to 16, all 22 recordings I killed on purpose played, the two longest minus their last 7 seconds and 1 minute 56 seconds. Version 4.0.11, in staged rollout since 18 November 2026, keeps the file and lists it as a partial recording.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code that did it
&lt;/h2&gt;

&lt;p&gt;This function shipped in every 4.0.x release, from 4.0.0 in October 2026 to 4.0.10:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Deletes leftover .mp4 files in the hidden video folder that have no database entry.
 * These are unplayable fragments from a recording that was interrupted mid-segment
 * (the MP4 index is never written, so the file can't be repaired or played).
 */&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;cleanupOrphanedSegments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listFiles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forEach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isFile&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;".mp4"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
            &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;knownPaths&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;absolutePath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database row is written when CameraX reports that the file is finalized. If Android kills the process during a recording, that event never arrives. The file stays on disk without a row, the recording never shows up in the list, and the next time the list opens the file is removed.&lt;/p&gt;

&lt;p&gt;The function could also delete a recording that was stopped normally. Stop ended the foreground service at once, while the encoder needed about another second to finish the file, and during that second the file had no row. A list opened inside that second deleted a file that was still being written. A second reviewer wrote: "deleted my 5 minute recording ... when I had to view the recording immediately". That fits this path, but I never saw the file, so for that review it is a likely cause and not a proven one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The comment describes the old MediaMuxer
&lt;/h2&gt;

&lt;p&gt;I checked the comment's claim on a desktop first. One 60 second clip (1280x720, 30 fps, H.264 at 4 Mbit/s, AAC, 1,800 frames), stream-copied with ffmpeg 6.1.1 into four containers, each file cut with &lt;code&gt;head -c&lt;/code&gt; at 25%, 50% and 90% of its size. Frames ffmpeg could still decode:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Container&lt;/th&gt;
&lt;th&gt;Cut at 25%&lt;/th&gt;
&lt;th&gt;Cut at 50%&lt;/th&gt;
&lt;th&gt;Cut at 90%&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;MP4, index at the end&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fragmented MP4&lt;/td&gt;
&lt;td&gt;450&lt;/td&gt;
&lt;td&gt;899&lt;/td&gt;
&lt;td&gt;1,617&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Matroska (MKV)&lt;/td&gt;
&lt;td&gt;450&lt;/td&gt;
&lt;td&gt;899&lt;/td&gt;
&lt;td&gt;1,617&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MPEG-TS&lt;/td&gt;
&lt;td&gt;452&lt;/td&gt;
&lt;td&gt;900&lt;/td&gt;
&lt;td&gt;1,618&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[mov,mp4,m4a,3gp,3g2,mj2] moov atom not found
cut90_plain.mp4: Invalid data found when processing input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 90% file holds 28 MB of intact H.264 and ffmpeg will not open it, because a classic MP4 writes its index (the &lt;code&gt;moov&lt;/code&gt; box) after the last frame. Android's &lt;code&gt;MediaMuxer&lt;/code&gt; works this way. Its reference says "The caller must not use the file path before calling stop()." For a recorder built on &lt;code&gt;MediaMuxer&lt;/code&gt; the comment is correct.&lt;/p&gt;

&lt;p&gt;Every 4.0.x version of XSCamera records with CameraX 1.6.2. CameraX moved video recording to the Media3 muxer in 1.6.0 (25 March 2026), and the 1.6.0-beta02 notes list a fix for "video corruption during unexpected interruptions or app termination". So the comment describes a muxer that no 4.0.x release used.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Media3 muxer writes
&lt;/h2&gt;

&lt;p&gt;CameraX builds the muxer with &lt;code&gt;new Mp4Muxer.Builder(output).build()&lt;/code&gt;, all defaults. &lt;code&gt;Recorder.Builder&lt;/code&gt; in camera-video 1.6.2 has nine setters and none of them touches the container. These constants are in &lt;a href="https://github.com/androidx/media/blob/release/libraries/muxer/src/main/java/androidx/media3/muxer/Mp4Writer.java" rel="noopener noreferrer"&gt;&lt;code&gt;Mp4Writer.java&lt;/code&gt;&lt;/a&gt; of media3-muxer, release branch, read on 18 November 2026:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Used for updating the moov box periodically when sample batching is disabled.&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="no"&gt;MOOV_BOX_UPDATE_INTERVAL_US&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1_000_000L&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;DEFAULT_MOOV_BOX_SIZE_BYTES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqgmzj4zsbn1aqv7s71kl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqgmzj4zsbn1aqv7s71kl.png" alt="Three MP4 layouts: index written at stop, index rewritten every second in reserved space, index at the end rewritten in 20% steps" width="799" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The writer reserves 400,000 bytes after &lt;code&gt;ftyp&lt;/code&gt; and rewrites the whole index into them once per second of video. A kill in this phase can cost up to one second.&lt;/p&gt;

&lt;p&gt;When the index outgrows the reserved space it moves to the end of the file. From then on it is rewritten only when the data area has to be extended:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;minBytesToExtend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500_000L&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;maxBytesToExtend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1_000_000_000L&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;extensionRatio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.2f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each extension adds 20% of the current size, at most 1 GB. Video written since the last extension has no index entry. Just before the next one, that part is 0.2 of 1.2, a sixth of the file. I do not know the exact moment the index leaves the reserved space, and the phone runs below did not log it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Killing it on real phones
&lt;/h2&gt;

&lt;p&gt;A test build of 4.0.11 ran on a Samsung S23 (Android 13), Pixel 8 (14), Samsung S25 (15) and Pixel 10 (16) on BrowserStack. A driver app started a recording and killed the process by force-stop, by an injected crash on the main thread or by one on the overlay thread. After the restart the test read the video samples back from the file, took the timestamp of the last one and played the file in the app's own player.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Killed after&lt;/th&gt;
&lt;th&gt;Runs&lt;/th&gt;
&lt;th&gt;File size&lt;/th&gt;
&lt;th&gt;Lost at the end&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;25 to 30 s&lt;/td&gt;
&lt;td&gt;16 (4 per phone)&lt;/td&gt;
&lt;td&gt;5 to 25 MB&lt;/td&gt;
&lt;td&gt;nothing measurable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2 min&lt;/td&gt;
&lt;td&gt;4 (1 per phone)&lt;/td&gt;
&lt;td&gt;21 to 96 MB&lt;/td&gt;
&lt;td&gt;nothing measurable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15 min, Samsung S25&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;840 MB&lt;/td&gt;
&lt;td&gt;7 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15 min, Pixel 8&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;706 MB&lt;/td&gt;
&lt;td&gt;1 min 56 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;"Nothing measurable" means the playable length was within two seconds of the planned kill time, and my kill timer is no more exact than that. Several 30 second files came out at 31.&lt;/p&gt;

&lt;p&gt;I did not log where the extension steps fell in the two long runs. Both losses are under the one-sixth ceiling: 1 min 56 s is 13% of the Pixel 8 recording. Two runs say little about what is typical.&lt;/p&gt;

&lt;p&gt;4.0.10 was not part of this run. Going by the function above, it deletes a file like each of these 22 when the list opens.&lt;/p&gt;

&lt;h2&gt;
  
  
  What 4.0.11 changed
&lt;/h2&gt;

&lt;p&gt;4.0.11 removes the &lt;code&gt;file.delete()&lt;/code&gt; call. When the list opens, a video file without a row is read with &lt;code&gt;MediaMetadataRetriever&lt;/code&gt;. If it has a duration it gets a row, a "partial" mark and its real length. If it cannot be read it is still kept and listed, and a tap offers to copy it to a folder. The app now removes only empty files older than three minutes.&lt;/p&gt;

&lt;p&gt;Stop no longer ends the service. The service stays in the foreground until the file is finalized and the row is written, then stops itself. The notification stays about one second longer because of that. A test opened the list 0.4 to 1.3 s after stop, five times per phone, and the recording was there all 20 times.&lt;/p&gt;

&lt;p&gt;With "save to gallery" on, a cut recording was visible in the phone's gallery but missing in the app. The app now saves the file name when a gallery recording starts and lists that file as partial after a kill.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not solved
&lt;/h2&gt;

&lt;p&gt;A recording is still one file per session. By the one-sixth rule a kill at one hour can cost up to ten minutes. I have not run a kill later than 15 minutes.&lt;/p&gt;

&lt;p&gt;The phone tests used the debug build of the same code. No Xiaomi, realme, vivo or OPPO phone was in the test, and nothing below Android 13.&lt;/p&gt;

&lt;p&gt;The muxer builds the whole index in memory on each rewrite. Crashlytics shows &lt;code&gt;OutOfMemoryError&lt;/code&gt; in &lt;code&gt;androidx.media3.muxer.Boxes.moov&lt;/code&gt; for 28 users in one week, 32 events. I do not know how long those recordings were. I think one MP4 per session is the wrong default for a recorder, and mine still ships it. A new file every 30 minutes should cap both the index size and the loss after a kill, at five minutes by the same rule. That is not built.&lt;/p&gt;

&lt;h2&gt;
  
  
  A broken MP4 from an older recorder
&lt;/h2&gt;

&lt;p&gt;My three dead desktop files came back with &lt;a href="https://github.com/anthwlock/untrunc" rel="noopener noreferrer"&gt;untrunc&lt;/a&gt;, which learns the frame layout from a healthy file made by the same encoder. Commit 87a5d92:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ untrunc ref.mp4 cut90_plain.mp4
Info: Found 4152 packets ( mp4a: 2531 avc1: 1621 avc1-keyframes: 55 )
Info: Duration of avc1: 54s 33ms  (54033 ms)
Info: saving cut90_plain.mp4_fixed.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;54 seconds out of the 54 in the file, with sound. The 25% and 50% cuts gave 451 and 899 frames. The reference was an 8 second clip from the same encoder with the same settings. With a 1080p clip from a different encoder as reference, untrunc found 2 frames and stopped.&lt;/p&gt;

&lt;p&gt;For a phone that would mean keeping the broken file, recording ten seconds with the same app, camera and resolution as the reference, and running &lt;code&gt;untrunc good.mp4 broken.mp4&lt;/code&gt; on a computer. I have not run it on a file recorded by a phone, and not on H.265.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>mobile</category>
      <category>debugging</category>
    </item>
    <item>
      <title>What Android 14, 15 and 16 still let a background video recorder do</title>
      <dc:creator>TheChemical</dc:creator>
      <pubDate>Tue, 17 Nov 2026 10:40:04 +0000</pubDate>
      <link>https://dev.to/thechemical/what-android-14-15-and-16-still-let-a-background-video-recorder-do-46k7</link>
      <guid>https://dev.to/thechemical/what-android-14-15-and-16-still-let-a-background-video-recorder-do-46k7</guid>
      <description>&lt;p&gt;I work on &lt;a href="https://play.google.com/store/apps/details?id=me.jagar.xscamera2" rel="noopener noreferrer"&gt;XSCamera&lt;/a&gt;, a background video recorder for Android. It records with the screen off or while you use another app. It has a bit over 6 million installs, which mostly means we get to find every edge case in production.&lt;/p&gt;

&lt;p&gt;People regularly ask whether this kind of app is "still possible" on new Android versions. It is. But the list of things you must do correctly gets longer every year, and the failure mode is nasty: the app installs, the button works, and the recording silently never starts or dies ten minutes in.&lt;/p&gt;

&lt;p&gt;This is the short version of what we've learned. If you're building anything that holds the camera or microphone while the user is elsewhere (a dashcam, a baby monitor, a bodycam for cyclists), most of it applies to you.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rules, by version
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6ezdo5l30qeci1ra4q04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6ezdo5l30qeci1ra4q04.png" alt="Android versions and what each one changed for background camera use" width="799" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Android 9&lt;/strong&gt; cut off camera and microphone access for apps that are idle in the background. From here on you need a foreground service, and a foreground service means a notification. That's the end of "invisible" recorders, seven years ago now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Android 10 and 11&lt;/strong&gt; introduced &lt;code&gt;foregroundServiceType&lt;/code&gt; and then enforced it for camera and microphone. A service that touches the camera without &lt;code&gt;camera&lt;/code&gt; in its type gets nothing but black frames or an exception, depending on the device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Android 12&lt;/strong&gt; did two things. It added the green privacy indicator, which the system draws and you cannot influence. And it blocked starting a foreground service from the background in most cases (&lt;code&gt;ForegroundServiceStartNotAllowedException&lt;/code&gt;). In practice: start the service while your activity is visible, in response to a tap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Android 13&lt;/strong&gt; made the notification permission a runtime permission, and gave users a "stop" button for foreground services in the notification drawer. If your service is stopped from there, you get no polite callback. Plan for it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Android 14&lt;/strong&gt; is the one that broke old apps. Every foreground service must declare a type, each type needs its own permission (&lt;code&gt;FOREGROUND_SERVICE_CAMERA&lt;/code&gt;, &lt;code&gt;FOREGROUND_SERVICE_MICROPHONE&lt;/code&gt;), and the system checks at start time that you actually hold the runtime permission for that type. Miss any of it and &lt;code&gt;startForeground()&lt;/code&gt; throws a &lt;code&gt;SecurityException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Android 15&lt;/strong&gt; tightened what may be started from &lt;code&gt;BOOT_COMPLETED&lt;/code&gt;. A camera service started at boot is not allowed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The minimum that works
&lt;/h2&gt;

&lt;p&gt;Manifest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.CAMERA"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.RECORD_AUDIO"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.FOREGROUND_SERVICE"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.FOREGROUND_SERVICE_CAMERA"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.FOREGROUND_SERVICE_MICROPHONE"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.POST_NOTIFICATIONS"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;service&lt;/span&gt;
    &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;".RecordingService"&lt;/span&gt;
    &lt;span class="na"&gt;android:exported&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt;
    &lt;span class="na"&gt;android:foregroundServiceType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"camera|microphone"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Starting it, from an activity that is on screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In the activity, after CAMERA and RECORD_AUDIO are granted&lt;/span&gt;
&lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startForegroundService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;RecordingService&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In the service&lt;/span&gt;
&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStartCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;startId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ServiceInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FOREGROUND_SERVICE_TYPE_CAMERA&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recordAudio&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;ServiceInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FOREGROUND_SERVICE_TYPE_MICROPHONE&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;ServiceCompat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startForeground&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NOTIFICATION_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;buildNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;openCameraAndRecord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;START_NOT_STICKY&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details in there are not decoration.&lt;/p&gt;

&lt;p&gt;Only pass the microphone type if you hold &lt;code&gt;RECORD_AUDIO&lt;/code&gt;. If the user turned sound off and denied the permission, including that type throws on Android 14.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;START_NOT_STICKY&lt;/code&gt; is deliberate. If the system kills you and restarts the service later, you're now a service started from the background, which can't get the camera. Better to stop cleanly and tell the user than to come back as a zombie holding a notification and recording nothing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuxh0vt0f1rv47pfsb7lk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuxh0vt0f1rv47pfsb7lk.png" alt="The order of events that keeps camera access after the screen turns off" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The bugs that actually cost us
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The timestamp overlay.&lt;/strong&gt; Our users like a date and time burned into the video. In one release line we drew that overlay on the main thread, once per frame. On fast phones nobody noticed. On slow ones, with a 4K preview, the main thread fell behind, and our ANR rate climbed from 0.28% to 0.40%. Google's bad-behaviour threshold is 0.47%, and Play shows your app less once you cross it, so that was close enough to ruin a week. The fix was boring: the overlay got its own thread. The lesson was less boring. A camera app has a hot loop, and anything on the main thread inside it is a liability on a 2 GB phone, even if it profiles fine on your desk.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Battery managers.&lt;/strong&gt; A correct foreground service is still not safe on many phones. Samsung, Xiaomi, Oppo and Vivo all ship their own task killers on top of Android's. The only real fix is on the user's side (battery set to Unrestricted, autostart on Xiaomi), so the app has to detect the situation and explain it, with the exact path for that manufacturer. &lt;a href="https://dontkillmyapp.com" rel="noopener noreferrer"&gt;dontkillmyapp.com&lt;/a&gt; keeps the list. We read 2,187 low-star reviews across the big apps in this category and "it stopped by itself" was the number one complaint, 340 of them. Most of those are this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The file that won't play.&lt;/strong&gt; A normal MP4 gets its index written when recording stops. If the process dies first (battery killer, crash, full disk), you have a large file that no player will open. It was the smallest group in those reviews, 115, and the angriest. If you record long sessions, don't rely on a clean stop: write in segments or use a container layout that stays readable. Then test it by killing your own process mid-recording.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Storage.&lt;/strong&gt; 4K runs about 350 MB a minute. Users don't do that maths, so do it for them and show the remaining minutes before they press record.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd check before shipping
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjm7iex8ztstj3cpsva1u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjm7iex8ztstj3cpsva1u.png" alt="Checklist for a background camera service" width="799" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Service declares &lt;code&gt;camera&lt;/code&gt; (and &lt;code&gt;microphone&lt;/code&gt; if used), with both &lt;code&gt;FOREGROUND_SERVICE_*&lt;/code&gt; permissions in the manifest.&lt;/li&gt;
&lt;li&gt;Service is started from a visible activity, after the runtime permissions are granted.&lt;/li&gt;
&lt;li&gt;The types passed to &lt;code&gt;startForeground()&lt;/code&gt; match the permissions you hold right now.&lt;/li&gt;
&lt;li&gt;The notification is real and useful: what's recording, for how long, a Stop action. Reviewers at Play look at this, and so do users.&lt;/li&gt;
&lt;li&gt;Nothing per-frame runs on the main thread.&lt;/li&gt;
&lt;li&gt;Kill the process mid-recording. Is the file playable?&lt;/li&gt;
&lt;li&gt;Run it for an hour with the screen off on a Samsung and a Xiaomi with default battery settings. Then with Unrestricted. Know the difference and tell your users.&lt;/li&gt;
&lt;li&gt;Fill the storage during a recording. Do you stop cleanly?&lt;/li&gt;
&lt;li&gt;Fill in Play Console's foreground service declaration and record the demo video before you upload, not after the rejection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  One more thing
&lt;/h2&gt;

&lt;p&gt;Don't promise hidden recording in your listing. It isn't possible, so it's a false claim, and Play removes apps for it under the stalkerware policy. The honest pitch (keeps recording when the screen is off, always shows that it is recording) is also the one that survives review.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'm on the team behind XSCamera at XS Labs. The review counts come from our own read of public Play reviews in November 2026. If something here is wrong for a device you know well, I'd like to hear it.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>camera</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
