ReplayKit has really been frustrating me recently. For some reason
RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in
does not actually work when I call it because I have a print() statement inside it and it is never called.
My code in the ViewController is:
import UIKit
import AVFoundation
import SpriteKit
import ReplayKit
import AVKit
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, RPPreviewViewControllerDelegate {
var assetWriter:AVAssetWriter!
var videoInput:AVAssetWriterInput!
func startRecording(withFileName fileName: String) {
if #available(iOS 11.0, *)
{
assetWriter = try! AVAssetWriter(outputURL: fileURL, fileType:
AVFileType.mp4)
let videoOutputSettings: Dictionary<String, Any> = [
AVVideoCodecKey : AVVideoCodecType.h264,
AVVideoWidthKey : UIScreen.main.bounds.size.width,
AVVideoHeightKey : UIScreen.main.bounds.size.height
];
videoInput = AVAssetWriterInput (mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
videoInput.expectsMediaDataInRealTime = true
assetWriter.add(videoInput)
print("HERE")
RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in
print("RECORDING")
}
}
}
func stopRecording(handler: #escaping (Error?) -> Void)
{
if #available(iOS 11.0, *)
{
RPScreenRecorder.shared().stopCapture
{ (error) in
handler(error)
self.assetWriter.finishWriting
{
print("STOPPED")
}
}
}
}
"HERE" is printed, but not "RECORDING"
[p.s. sorry for bad formatting in code, I'm sure you'll understand :)]
I have also tried a different method:
let recorder = RPScreenRecorder.shared()
recorder.startRecording{ [unowned self] (error) in
guard error == nil else {
print("There was an error starting the recording.")
return
}
print("Started Recording Successfully")
}
and to stop the recording...
recorder.stopRecording { [unowned self] (preview, error) in
print("Stopped recording")
guard preview != nil else {
print("Preview controller is not available.")
return
}
onGoingScene = true
preview?.previewControllerDelegate = self
self.present(preview!, animated: true, completion: nil)
}
This method does not stop when I call the recorder.stopRecording() function, "Stopped recording" is never called.
Can someone please help me because this is really frustrating me, how can you PROPERLY use ReplayKit to record your screen in iOS 11? I have searched all over the internet and none of the methods work for me, I don't why. P.S. I have the necessary permission keys in my Info.plist.
Thanks
A huge reminder that ReplayKit doesn't work in simulator. I wasted hours on the exact same issue until realized that ReplayKit will never trigger startCapture handler because it never records in simulator.
Well there are quite few possible causes for this issue.
Some of them are here:
Your Replay kit Shared Recorder might be crashed, For that you can restart your device and check again.
There might be printable issue in your replay kit. For that kindly conform to the RPScreenRecorderDelegateProtocol and add Recording Changes
screenRecorder:didStopRecordingWithPreviewViewController:error:
method to your class and check if any error shows up in this method.
Related
I'm building a video editor that lets you apply a CIFilter to a video. And it works well.
The only problem I'm facing is that when I dismiss the ViewController I get this error:
Unfinished AVAsynchronousVideoCompositionRequest deallocated - should
have called finishWithComposedVideoFrame:, finishWithError: or
finishCancelledRequest
This error doesn't make the app crash or slower, but when I try to edit another video the preview in the AVPlayer becomes black.
This is my current code:
var mutableComposition = AVMutableVideoComposition()
let exposureFilter = CIFilter.exposureAdjust()
override func viewDidLoad() {
updateComposition()
}
func updateComposition() {
mutableComposition = AVMutableVideoComposition(asset: player.currentItem!.asset, applyingCIFiltersWithHandler: { [weak self] request in
guard let self = self else {
return
}
self.exposureFilter.inputImage = request.sourceImage.clampedToExtent()
self.exposureFilter.ev = 5
let output = self.exposureFilter.outputImage!.cropped(to: request.sourceImage.extent)
request.finish(with: output, context: nil)
})
player.currentItem?.videoComposition = mutableComposition
}
If I remove the [weak self] no error it's printed, but it keeps the ViewController in memory when I dismiss it, creating an unwanted memory leak.
after follow #Marco comment, i updated code like below, but still not working, the loudspeaker sometimes can not enabled
Before report new call/ user accepted call I called the 2 methods below:
configureAudioSessionToDefaultSpeaker()
func configureAudioSessionToDefaultSpeaker() {
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSession.Category.playAndRecord, mode: .default)
try session.setActive(true)
try session.setMode(AVAudioSession.Mode.voiceChat)
try session.setPreferredSampleRate(44100.0)
try session.setPreferredIOBufferDuration(0.005)
} catch {
print("Failed to configure `AVAudioSession`: \(error)")
}
}
I updated more code:
func startCallWithPhoneNumber(call : CallInfoModel) {
configureAudioSessionToDefaultSpeaker()
currentCall = call
if let unwrappedCurrentCall = currentCall {
let handle = CXHandle.init(type: .generic, value: unwrappedCurrentCall.CallerDisplay ?? UNKNOWN)
let startCallAction = CXStartCallAction.init(call: unwrappedCurrentCall.uuid, handle: handle)
let transaction = CXTransaction.init()
transaction.addAction(startCallAction)
requestTransaction(transaction: transaction)
self.provider?.reportOutgoingCall(with: startCallAction.callUUID, startedConnectingAt: nil)
}
}
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
configureAudioSessionToDefaultSpeaker()
delegate?.callDidAnswer()
action.fulfill()
currentCall?.isAccepted = true
let sb = UIStoryboard(name: "main", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "SingleCallVC") as! SingleCallVC
vc.modalPresentationStyle = .fullScreen
vc.callObj = currentCall
vc.isIncoming = true
let appDelegate = AppDelegate.shared
appDelegate.window?.rootViewController?.present(vc, animated: true, completion: nil)
}
My call almost work normally but sometime loudspeaker can not be enabled. I read many documents but nothing worked for me. Could someone give me some advice? Thanks.
You're configuring the AudioSession two times. The RTCAudioSession it's a proxy of AVAudioSession. You should do only one configuration to avoid unexpected results. RTCAudioSession should expose all the methods of the AVAudioSession, so you should be able to make all the configurations you want inside configureRtcAudioSession() and eliminate configureAudioSessionToDefaultSpeaker() or viceversa. I'm not sure if it will solve your issue but at least it should help to avoid unexpected behaviors.
I've had success with enabling the speaker using the method below.
let audioQueue = DispatchQueue(label: "audio")
func setSpeaker(_ isEnabled: Bool) {
audioQueue.async {
defer {
AVAudioSession.sharedInstance().unlockForConfiguration()
}
AVAudioSession.sharedInstance().lockForConfiguration()
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(isEnabled ? .speaker : .none)
} catch {
debugPrint(error.localizedDescription)
}
}
}
// Enables the audio speaker.
setSpeaker(true)
// Disables the audio speaker.
setSpeaker(false)
I'm attempting to use my Mac as a bluetooth peripheral using the CoreBluetooth module. I've looked at the docs and various examples of it, but my code still doesn't work. I put print statements throughout it, and for some reason the only print statements which show up in my output are "1" and "1.5". This is in an XCode 11.6 playground running on Mac OS Catalina 10.15.6, if that helps.
import AppKit
import PlaygroundSupport
import CoreBluetooth
print("1")
let myCBUUID = CBUUID(string:"2FC62EDD-EFED-457A-A88E-6E9BC1B8D7AF")
let properties: CBCharacteristicProperties = [.notify, .read, .write]
let permissions: CBAttributePermissions = [.readable, .writeable]
let characteristic = CBMutableCharacteristic(type:myCBUUID,properties: properties, value:nil, permissions: permissions)
let myCBService = CBMutableService(type:myCBUUID,primary: true)
myCBService.characteristics = [characteristic]
print("1.5")
class Peripheral: CBPeripheralManager, CBPeripheralManagerDelegate
{
var peripheralManager : CBPeripheralManager!
convenience init(delegate: CBPeripheralManagerDelegate?,
queue: DispatchQueue?){
print("1.75")
self.init()
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: [CBPeripheralManagerOptionShowPowerAlertKey: true])
peripheralManager.add(myCBService)
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
{
print("2")
print("state: \(peripheral.state)")
}
func Advertise(){
print("hello its me")
peripheralManager.startAdvertising([CBAdvertisementDataLocalNameKey : "My Peripheral", CBAdvertisementDataLocalNameKey : myCBUUID])
}
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: NSError?)
{
if let error = error
{
print("Failed due to error: \(error)")
return
}
print("Success")
}
}
let myPeripheral = Peripheral.init()
myPeripheral.Advertise()
(this is my first time posting, sorry if i messed anything up and feel free to make suggestions in the comments if i left something out)
I am following instructions here, I've put together this test project to handle interruptions to audio play. Specifically, I'm using the alarm from the default iphone clock app as interruption. It appears that the interruption handler is getting called but is not getting past the let = interruptionType line as "wrong type" showed up twice.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player = AVAudioPlayer()
let audioPath = NSBundle.mainBundle().pathForResource("rachmaninov-romance-sixhands-alianello", ofType: "mp3")!
func handleInterruption(notification: NSNotification) {
guard let interruptionType = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? AVAudioSessionInterruptionType else { print("wrong type"); return }
switch interruptionType {
case .Began:
print("began")
// player is paused and session is inactive. need to update UI)
player.pause()
print("audio paused")
default:
print("ended")
/**/
if let option = notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? AVAudioSessionInterruptionOptions where option == .ShouldResume {
// ok to resume playing, re activate session and resume playing
// need to update UI
player.play()
print("audio resumed")
}
/**/
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
player.numberOfLoops = -1 // play indefinitely
player.prepareToPlay()
//player.delegate = player
} catch {
// process error here
}
// enable play in background https://stackoverflow.com/a/30280699/1827488 but this audio still gets interrupted by alerts
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
} catch let error as NSError {
print(error.localizedDescription)
}
} catch let error as NSError {
print(error.localizedDescription)
}
// add observer to handle audio interruptions
// using 'object: nil' does not have a noticeable effect
let theSession = AVAudioSession.sharedInstance()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleInterruption(_:)), name: AVAudioSessionInterruptionNotification, object: theSession)
// start playing audio
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Furthermore, following an idea here, I have modified the handler to
func handleInterruption(notification: NSNotification) {
//guard let interruptionType = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? AVAudioSessionInterruptionType else { print("wrong type"); return }
if notification.name != AVAudioSessionInterruptionNotification
|| notification.userInfo == nil{
return
}
var info = notification.userInfo!
var intValue: UInt = 0
(info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)
if let interruptionType = AVAudioSessionInterruptionType(rawValue: intValue) {
switch interruptionType {
case .Began:
print("began")
// player is paused and session is inactive. need to update UI)
player.pause()
print("audio paused")
default:
print("ended")
/** /
if let option = notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? AVAudioSessionInterruptionOptions where option == .ShouldResume {
// ok to resume playing, re activate session and resume playing
// need to update UI
player.play()
print("audio resumed")
}
/ **/
player.play()
print("audio resumed")
}
}
}
Results are that all of "began", "audio paused", "ended" and "audio resumed" show up in console but audio play is not actually resumed.
Note: I moved the player.play() outside of the commented out where option == .ShouldResume if statement because that if condition is not true when the .Ended interruption occurs.
(Posted on behalf of the question author, after it was posted in the question).
Solution found! Following discussion here, inserted this in viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
} catch {
}
After clicking "ok" on the alarm interruption, the audio play continued. Unlike previously noted, the solution does NOT require an interruption handler (which #Leo Dabus has since removed).
However if you are using an interruption handler, .play() must NOT be invoked within handleInterruption() as doing so does NOT guarantee play to resume & seems to prevent audioPlayerEndInterruption() to be called (see docs). Instead .play() must be invoked within audioPlayerEndInterruption() (any of its 3 versions) to guarantee resumption.
Furthermore, AVAudioSession must be give option .MixWithOthers noted by #Simon Newstead if you want your app to resume play after interruption when your app is in the background. It seems that if a user wants the app to continue playing when it goes into the background, it is logical to assume the user also wants the app to resume playing after an interruption while the app is in the background. Indeed that is the behaviour exhibited by the Apple Music app.
#rockhammers suggestion worked for me. Here
before class
let theSession = AVAudioSession.sharedInstance()
in viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.handleInterruption(notification:)), name: NSNotification.Name.AVAudioSessionInterruption, object: theSession)
And then the Function
func handleInterruption(notification: NSNotification) {
print("handleInterruption")
guard let value = (notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber)?.uintValue,
let interruptionType = AVAudioSessionInterruptionType(rawValue: value)
else {
print("notification.userInfo?[AVAudioSessionInterruptionTypeKey]", notification.userInfo?[AVAudioSessionInterruptionTypeKey])
return }
switch interruptionType {
case .began:
print("began")
vox.pause()
music.pause()
print("audioPlayer.playing", vox.isPlaying)
/**/
do {
try theSession.setActive(false)
print("AVAudioSession is inactive")
} catch let error as NSError {
print(error.localizedDescription)
}
pause()
default :
print("ended")
if let optionValue = (notification.userInfo?[AVAudioSessionInterruptionOptionKey] as? NSNumber)?.uintValue, AVAudioSessionInterruptionOptions(rawValue: optionValue) == .shouldResume {
print("should resume")
// ok to resume playing, re activate session and resume playing
/**/
do {
try theSession.setActive(true)
print("AVAudioSession is Active again")
vox.play()
music.play()
} catch let error as NSError {
print(error.localizedDescription)
}
play()
}
}
}
some reasons interruptionNotification is not working correctly on iOS 12.x So I added silenceSecondaryAudioHintNotification
With alarm notification incoming, you can try to use silenceSecondaryAudioHintNotification.
#objc func handleSecondaryAudioSilence(notification: NSNotification) {
guard let userInfo = notification.userInfo,
let typeValue = userInfo[AVAudioSessionSilenceSecondaryAudioHintTypeKey] as? UInt,
let type = AVAudioSession.SilenceSecondaryAudioHintType(rawValue: typeValue) else {
return
}
if type == .end {
// Other app audio stopped playing - restart secondary audio.
reconnectAVPlayer()
}
}
https://github.com/leoru/SwiftLoader
I'm using this activity indicator library in my app, but it's freezing my main UICollectionView after I call SwiftLoader.hide() to where I can't interact with anything. My iOS device itself is not freezing as I can perform all other functions fine, and my debugger isn't complaining about anything. It's just that my UICollectionView isn't interactive after calling that one method. I know that this library is the problem because it never freezes unless I use it. Also from looking at the library's code, it involves adding UIViews/subviews and removing or dismissing them. I have suspicions that something isn't being removed or dismissed properly. Can someone confirm my suspicions? Below is the only code I'm using that involves a library called SwiftLoader. If I take the SwiftLoader functions out, it works perfectly with no freezing.
func queryPostObjectsWithLocation(loci: CLLocation) {
SwiftLoader.show(title: "Loading...", animated: true)
let postQuery = PFQuery(className: "Post")
let postsNearThisUser = postQuery.whereKey("location", nearGeoPoint:PFGeoPoint(location: loci),withinMiles: miles)
let whiteList = postQuery.whereKey("objectId", notContainedIn: self.flaggedPosts)
let combo = PFQuery.orQueryWithSubqueries([postsNearThisUser,whiteList])
combo.limit = 20
combo.findObjectsInBackgroundWithBlock {(result: [AnyObject]?, error: NSError?) -> Void in
self.posts = result as? [PFObject] ?? []
if self.posts == [] {
SwiftLoader.hide()
}
self.collectionView.reloadData()
SwiftLoader.hide()
}
}
Try executed your code on the main thread like this:
combo.findObjectsInBackgroundWithBlock {(result: [AnyObject]?, error: NSError?) -> Void in {
self.posts = result as? [PFObject] ?? []
NSOperationQueue.mainQueue().addOperationWithBlock {
if self.posts == [] {
SwiftLoader.hide()
}
self.collectionView.reloadData()
SwiftLoader.hide()
}
}