Search code examples
iosswiftmpmusicplayercontrollerreplaykit

Why doesn't my audio in my game record in ReplayKit?


I have audio playing in my app using MPMusicPlayerController and Im recording the screen using RPScreenRecorder. The problem Im having is that it only records the screen and not the audio in the app. The other problem I have is that when I press the cancel button for the previewController it doesnt dismiss the view for some reason. What am I doing wrong?

@IBAction func stopTheRecordingAction(sender: AnyObject) {

    stopTheRecording.hidden = true
    recordButton.hidden = false


    RPScreenRecorder.sharedRecorder().stopRecordingWithHandler { (previewController: RPPreviewViewController?, error: NSError?) -> Void in

        if previewController != nil {

            let alertController = UIAlertController(title: "Recording", message: "Do you wish to discard or view your gameplay recording?", preferredStyle: .Alert)

            let discardAction = UIAlertAction(title: "Discard", style: .Default) { (action: UIAlertAction) in
                RPScreenRecorder.sharedRecorder().discardRecordingWithHandler({ () -> Void in
                    // Executed once recording has successfully been discarded
                })
            }

            let viewAction = UIAlertAction(title: "View", style: .Default, handler: { (action: UIAlertAction) -> Void in
                self.presentViewController(previewController!, animated: true, completion: nil)


            })

            alertController.addAction(discardAction)
            alertController.addAction(viewAction)

            self.presentViewController(alertController, animated: true, completion: nil)

        } else {

            // Handle error
        }
    }
}

@IBAction func recordScreen(sender: AnyObject) {
    recordButton.hidden = true
    stopTheRecording.hidden = false

    if RPScreenRecorder.sharedRecorder().available {
        RPScreenRecorder.sharedRecorder().startRecordingWithMicrophoneEnabled(true, handler: { (error: NSError?) -> Void in
            if error == nil { // Recording has started

            } else {
                // Handle error
            }
        })
    } else {
        // Display UI for recording being unavailable

}

}




   func previewControllerDidFinish(previewController: RPPreviewViewController) {
   previewController.dismissViewControllerAnimated(true, completion: nil)
    print("dismiss")
}

Solution

  • Okay I got it work but I had to use AVAudioPlayer instead of MPMusicPlayerController. For some reason replaykit doesnt record the audio using MPMedia.