3

I have tried Broadcast Extension. I have added that extension via target. But, I don't know how to record when my app is in background.

Two ways I am trying to record:

Via Control center: I can see my app’s target name, after selecting that name, then I start to record by clicking Start Broadcast, next by clicking Stop Record, but my video not getting stored either in Camera Roll or in my app. Ref Link: ReplayKit's RPSystemBroadcastPickerView not showing preferredExtension

Via my app: Once press Start button, recording get started, I am trying to record background from my app by clicking home button, obviously recording has been stopped. Ref Link: https://medium.com/@giridharvc7/replaykit-screen-recording-8ee9a61dd762.

jscs
  • 63,694
  • 13
  • 151
  • 195
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
  • "Via my app" - as far as I know, this approach won't work. You can't start recording from your app, press the home button, and expect recording to continue. "Via control center" - you will have to actually show some code to see where the problem is. – TheNextman Feb 04 '19 at 05:08
  • `RPSystemBroadcastPickerView`, this giving "press the home button, and expect recording to continue". @TheNextman . But, I cannot detect when my recording get finish via my app. – McDonal_11 Feb 04 '19 at 05:21
  • `BroadcastExtension` will store saved file to its own folder, you need to manually copy the file to app's document folder, then save the file to Gallery. – RyanB Feb 07 '19 at 04:23
  • Yes. I understood the concept behind `BroadcastExtension` . Once broadcast completed, **asset.finishWriting{}** is not called. Do u know how to make this Completion handler to invoke automatically? But, while we recording, if we face any error in `processSampleBuffer` , **asset.finishWriting{}** its get called automatically. But, record is completed without any error, this function not getting called. – McDonal_11 Feb 07 '19 at 04:36
  • I have the same issue with my code so I put sleep function for 2 seconds which gives my completion handler to create the file in the end. – PAn Kaj Khatri Mar 18 '20 at 07:08

1 Answers1

3

There is no "direct" way to store video in your app while it is in the background. ReplayKit version 2 allows you to get video sample buffer pieces in the app extensions that calls "Broadcast Upload Extension". When you say "Via Control center" it means that you start broadcast (not recording), and at the same time iOS finds your app-extension (not your application) and launch it, then begins send video sample buffers to the extension. On this step you need to decide what to do with these buffers.

I can propose two solutions for you:

  • you can try transfer buffers to the server (and download video when main application will be launched)
  • you can try save buffers to the file using App Groups (to share this data with the main application), you can find example here.

But you need to know that broadcast extension has been designed for buffer transferring, not to store or re rendering or compress or recode buffers. Extension has a lot of limitations based on CPU time and Memory usage (±50 MB), you can try to do anything there but if it is "heavy" operation iOS will close the extension.

Bws Sluk
  • 478
  • 5
  • 11
  • Thanks a lot. Now I am having clear idea. I have added Broadcast Upload Extension to my app. Now, broadcast started "via control center", but no action happening in "class SampleHandler: RPBroadcastSampleHandler" and "class BroadcastSetupViewController: UIViewController" . How to do this? – McDonal_11 Feb 04 '19 at 10:40
  • 1
    For your purpose you need only SampleHandler class. Add print("debug-message") line to broadcastStarted and processSampleBuffer functions to see that extension works, then manually start broadcast via control center. To debug it, you need to change build target to upload extension target and run the project. @McDonal_11 – Bws Sluk Feb 04 '19 at 13:05
  • Yes u r right. I need only SampleHandler class. I have tried with UserDefault logic to check how my extensions work. Its working fine. My next step is, how to save this video in camera roll. Thanks @Bws Sluk. – McDonal_11 Feb 04 '19 at 13:09
  • https://stackoverflow.com/questions/46903118/saving-video-from-cmsamplebuffer-while-streaming-using-replaykit?rq=1 , from here, I am trying to save recorded video in Camera Roll. But, that is not working. I am having content in that recorded URL. I have checked that by converting to Data. Now, I am calling,**PHPhotoLibrary.shared().performChanges({** , then Completion handler, neither success nor failure is triggered, . Can u help me on this.?? – McDonal_11 Feb 06 '19 at 07:41
  • @McDonal_11 Hi, I have created broadcast upload extension and added print messages as said. but I'm not getting any logs neither my methods are getting called. changing the target is asking to select an app to run. how you achieved this? – Krutika Sonawala Jun 24 '19 at 07:14
  • 1
    You will not get any logs. You have to save ur logs as String in UserDefaults. @KrutikaSonawala – McDonal_11 Jun 24 '19 at 07:22
  • @McDonal_11 Ok. now what should be the next step? I have set my server. Now how will I get this data sent on server? webRTC does this? – Krutika Sonawala Jun 24 '19 at 08:39
  • V can't discuss full process here. You can search some live examples like **MobCrush** – McDonal_11 Jun 24 '19 at 09:21
  • I am able to record Video and tried saving the audio also. but audio is not recorded. When i try the same with RPScreenRecorder i can hear audio also along with video. Any help please. – YSR fan Apr 09 '21 at 12:50