How to record game scene in spritekit - ios

I have a project with SpriteKit. I have seen the video of WWDC15 for ReplayKit.
I added ReplayKit to my project and i want record my GameScene and share it in Facebook , save in camera roll more...and more.
anything not working now but i added the functions and i organized all.
thanks!
Start Record
func startScreenRecording() {
// Do nothing if screen recording hasn't been enabled.
let sharedRecorder = RPScreenRecorder.sharedRecorder()
// Register as the recorder's delegate to handle errors.
sharedRecorder.delegate = self
sharedRecorder.startRecordingWithMicrophoneEnabled(true) { error in
if let error = error {
self.showScreenRecordingAlert(error.localizedDescription)
}
}
}
Stop Record
func stopScreenRecordingWithHandler(handler:(() -> Void)) {
let sharedRecorder = RPScreenRecorder.sharedRecorder()
sharedRecorder.stopRecordingWithHandler { (previewViewController: RPPreviewViewController?, error: NSError?) in
if let error = error {
// If an error has occurred, display an alert to the user.
self.showScreenRecordingAlert(error.localizedDescription)
return
}
if let previewViewController = previewViewController {
// Set delegate to handle view controller dismissal.
previewViewController.previewControllerDelegate = self
/*
Keep a reference to the `previewViewController` to
present when the user presses on preview button.
*/
self.presentViewController(previewViewController, animated: true, completion: nil)
}
handler()
}
}
Other Functions of ReplayKit
func showScreenRecordingAlert(message: String) {
// Pause the scene and un-pause after the alert returns.
GameScene().paused = true
// Show an alert notifying the user that there was an issue with starting or stopping the recorder.
let alertController = UIAlertController(title: "ReplayKit Error", message: message, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in
GameScene().paused = false
}
alertController.addAction(alertAction)
/*
`ReplayKit` event handlers may be called on a background queue. Ensure
this alert is presented on the main queue.
*/
dispatch_async(dispatch_get_main_queue()) {
self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
func discardRecording() {
// When we no longer need the `previewViewController`, tell `ReplayKit` to discard the recording and nil out our reference
RPScreenRecorder.sharedRecorder().discardRecordingWithHandler {
self.previewViewController = nil
}
}
// MARK: RPScreenRecorderDelegate
func screenRecorder(screenRecorder: RPScreenRecorder, didStopRecordingWithError error: NSError, previewViewController: RPPreviewViewController?) {
// Display the error the user to alert them that the recording failed.
showScreenRecordingAlert(error.localizedDescription)
/// Hold onto a reference of the `previewViewController` if not nil.
if previewViewController != nil {
self.previewViewController = previewViewController
}
}
// MARK: RPPreviewViewControllerDelegate
func previewControllerDidFinish(previewController: RPPreviewViewController) {
previewViewController?.dismissViewControllerAnimated(true, completion: nil)
}
Game Scene
import SpriteKit
import ReplayKit
class GameScene: SKScene , SKPhysicsContactDelegate{
func addRecordButton() {
RecordButton = SKSpriteNode(imageNamed:"Record-Off.png")
RecordButton.name = "RecordButton"
RecordButton.position = CGPoint(x: self.size.width/2 + 185, y: self.size.height/2 + 285)
RecordButton.size = CGSizeMake(32,32)
addChild(RecordButton)
}
func Record() {
let recorder = RPScreenRecorder.sharedRecorder()
if recorder.available{
RecordButton.texture = SKTexture(imageNamed: "Record-On.png")
print("Record")
} else {
RecordButton.texture = SKTexture(imageNamed: "Record-Off.png")
print("Stop Record")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if {
if (RecordButton.containsPoint(location)){
Record()
}
}
}

class ViewController: UIViewController, RPScreenRecorderDelegate, RPPreviewViewControllerDelegate {
#IBOutlet weak var startRecordingButton: UIButton!
#IBOutlet weak var stopRecordingButton: UIButton!
#IBOutlet weak var activityView: UIActivityIndicatorView!
private let recorder = RPScreenRecorder.sharedRecorder()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if SIMULATOR {
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.showSimulatorWarning), userInfo: nil, repeats: false)
return
}
}
override func viewDidLoad() {
super.viewDidLoad()
recorder.delegate = self
activityView.hidden = true
buttonEnabledControl(recorder.recording)
}
#IBAction func startRecordingAction(sender: AnyObject) {
activityView.hidden = false
// start recording
recorder.startRecordingWithMicrophoneEnabled(true) { [unowned self] (error) in
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.activityView.hidden = true
}
if let error = error {
print("Failed start recording: \(error.localizedDescription)")
return
}
print("Start recording")
self.buttonEnabledControl(true)
}
}
#IBAction func stopRecordingAction(sender: AnyObject) {
activityView.hidden = false
//end recording
recorder.stopRecordingWithHandler({ [unowned self] (previewViewController, error) in
dispatch_async(dispatch_get_main_queue()) {
self.activityView.hidden = true
}
self.buttonEnabledControl(false)
if let error = error {
print("Failed stop recording: \(error.localizedDescription)")
return
}
print("Stop recording")
previewViewController?.previewControllerDelegate = self
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
// show preview vindow
self.presentViewController(previewViewController!, animated: true, completion: nil)
}
})
}
//MARK: - Helper
//control the enabled each button
private func buttonEnabledControl(isRecording: Bool) {
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
let enabledColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
let disabledColor = UIColor.lightGrayColor()
if !self.recorder.available {
self.startRecordingButton.enabled = false
self.startRecordingButton.backgroundColor = disabledColor
self.stopRecordingButton.enabled = false
self.stopRecordingButton.backgroundColor = disabledColor
return
}
self.startRecordingButton.enabled = !isRecording
self.startRecordingButton.backgroundColor = isRecording ? disabledColor : enabledColor
self.stopRecordingButton.enabled = isRecording
self.stopRecordingButton.backgroundColor = isRecording ? enabledColor : disabledColor
}
}
func showSimulatorWarning() {
let actionOK = UIAlertAction(title: "OK", style: .Default, handler: nil)
// let actionCancel = UIAlertAction(title: "cancel", style: .Cancel, handler: nil)
let alert = UIAlertController(title: "ReplayKit不支持模拟器", message: "请使用真机运行这个Demo工程", preferredStyle: .Alert)
alert.addAction(actionOK)
// alert.addAction(actionCancel)
self.presentViewController(alert, animated: true, completion: nil)
}
func showSystemVersionWarning() {
let actionOK = UIAlertAction(title: "OK", style: .Default, handler: nil)
let alert = UIAlertController(title: nil, message: "系统版本需要是iOS9.0及以上才支持ReplayKit", preferredStyle: .Alert)
alert.addAction(actionOK)
self.presentViewController(alert, animated: true, completion: nil)
}
// MARK: - RPScreenRecorderDelegate
// called after stopping the recording
func screenRecorder(screenRecorder: RPScreenRecorder, didStopRecordingWithError error: NSError, previewViewController: RPPreviewViewController?) {
print("Stop Recording ...\n");
}
// called when the recorder availability has changed
func screenRecorderDidChangeAvailability(screenRecorder: RPScreenRecorder) {
let availability = screenRecorder.available
print("Availability: \(availability)\n");
}
// MARK: - RPPreviewViewControllerDelegate
// called when preview is finished
func previewControllerDidFinish(previewController: RPPreviewViewController) {
print("Preview finish");
dispatch_async(dispatch_get_main_queue()) {
[unowned previewController] in
// close preview window
previewController.dismissViewControllerAnimated(true, completion: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

Recording video from ARKit

I would like to add a feature for users to record their ARKit experience. I'm taking the capturedImage of the ARFrame supplied by session(_ session: ARSession, didUpdate frame: ARFrame) and concatenating them into a video.
Unfortunately, ARFrame.capturedImage shows the frame of video captured by the camera, but doesn't include nodes placed by ARKit.
Is there any way to capture video coming from an ARSCNView?
I've tried this library, but it has major bugs (no shadows, large stutter at beginning of recording). I'd also like to not use ReplayKit for this project.
Here is what I'm using to turn ARFrame.capturedImage into a UIImage, and subsequently, a video.
extension UIImage {
convenience init(pixelBuffer: CVPixelBuffer) {
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
let size = CGSize(width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer))
let tempContext = CIContext()
let image = tempContext.createCGImage(ciImage, from: CGRect(origin: CGPoint.zero, size: size))!
// This assumes we're using an iPhone in portrait.
self.init(cgImage: image, scale: 1, orientation: .right)
}
}
I actually found a library to do this. It's SceneKitVideoRecorder.
I don't fully understand how it works yet, but the important code is located in SceneKitVideoRecorder.swift.
You can try to use ReplayKit. I use ReplayKit in my AR App to record when I place a model to the scene and more.
Try this my snippet:
import ReplayKit
class YourController: UIViewController, RPPreviewViewControllerDelegate {
#IBAction func shotVideo(_ sender: UIButton) {
print("Video")
if !isRecording {
startRecording()
} else {
stopRecording()
}
}
func startRecording() {
guard recorder.isAvailable else {
print("The recording isn't available now.")
return
}
recorder.isMicrophoneEnabled = false
recorder.startRecording{ [unowned self] (error) in
guard error == nil else {
print("Trouble with starting this recording.")
return
}
print("Recording started with success.")
self.isRecording = true
}
}
func stopRecording() {
recorder.stopRecording { (preview, error) in
print("The recording is stopped")
guard preview != nil else {
print("The preview controller isn't available.")
return
}
let alert = UIAlertController(title: "End Recording", message: "Want to edit or delete this recording?", preferredStyle: .alert)
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action: UIAlertAction) in
self.recorder.discardRecording(handler: { () -> Void in
print("Recording suffessfully deleted.")
})
})
let editAction = UIAlertAction(title: "Edit", style: .default, handler: { (action: UIAlertAction) -> Void in
preview?.previewControllerDelegate = self
self.present(preview!, animated: true, completion: nil)
})
alert.addAction(editAction)
alert.addAction(deleteAction)
self.present(alert, animated: true, completion: nil)
self.isRecording = false
}
}
// RPPreviewViewControllerDelegate
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
dismiss(animated: true)
}
}
This is my code, I hope I have been helpful :).

BaseViewController subclasses are not releasing in swift 3

I have created BaseViewController to use as subclass for all of my view controllers so that I can show alert whenever I need any progess to show and hide which is lazy variable.
Everything is cool until now. But I figured out that all my viewcontroller which are inherting from this are not releasing. What is the problem?
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 0.4 / 255.0, green: 100 / 215.0, blue: 120 / 255.0, alpha: 1.0)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return preferredStatusBarStyle_Internal()
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return supportedInterfaceOrientations_Internal()
}
lazy var progressHUD: MBProgressHUD = {
if let navController = self.navigationController {
return navController.HUD
}
return self.HUD
}()
func showAlert(_ title: String?, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: { _ in
DispatchQueue.main.async {
self.progressHUD.hide(animated: false, afterDelay: 1.0)
}
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
func showPermissionDeniedAlert(_ message: String) {
let alertController = UIAlertController(title: message, message: "Go to Settings?".localized, preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings".localized, style: .default) { _ in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(settingsUrl, completionHandler: { success in
print("Settings opened: \(success)") // Prints true
})
} else {
let success = UIApplication.shared.openURL(settingsUrl)
print("Settings opened: \(success)")
}
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
}
extension UIViewController {
func preferredStatusBarStyle_Internal() -> UIStatusBarStyle {
return .lightContent
}
func supportedInterfaceOrientations_Internal() -> UIInterfaceOrientationMask {
return isiPad() ? .allButUpsideDown : .all
}
var HUD: MBProgressHUD {
let progressHUD = MBProgressHUD(viewController: self)
return progressHUD
}
}
If the view controller is not releasing it means you are creating retain cycle.
It could be in two places
pass weakself to async block.
DispatchQueue.main.async { [weak self] in
self?.progressHUD.hide(animated: false, afterDelay: 1.0)
}
passing a weak reference if MBProgressHUD init is creating retain cycle
var HUD: MBProgressHUD {
// you can also use weak self
unowned let unownedSelf = self
let progressHUD = MBProgressHUD(viewController: unownedSelf)
return progressHUD
}

Speech recognition error in Swift 3 & iOS 10

I'm using an iPhone 6s plus, here is the code for the speech recognition viewcontroller:
import Speech
import UIKit
protocol SpeechRecognitionDelegate: class {
func speechRecognitionComplete(query: String?)
func speechRecognitionCancelled()
}
class SpeechRecognitionViewController: UIViewController, SFSpeechRecognizerDelegate {
var textView: UITextView!
private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private let audioEngine = AVAudioEngine()
private var query: String?
weak var delegate: SpeechRecognitionDelegate?
var isListening: Bool = false
init(delegate: SpeechRecognitionDelegate, frame: CGRect) {
super.init(nibName: nil, bundle: nil)
self.delegate = delegate
self.view.frame = frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
enum ErrorMessage: String {
case denied = "To enable Speech Recognition go to Settings -> Privacy."
case notDetermined = "Authorization not determined - please try again."
case restricted = "Speech Recognition is restricted on this device."
case noResults = "No results found - please try a different search."
}
func displayErrorAlert(message: ErrorMessage) {
let alertController = UIAlertController(title: nil,
message: message.rawValue,
preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(alertAction)
OperationQueue.main.addOperation {
self.present(alertController, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
speechRecognizer?.delegate = self
//initialize textView and add it to self.view
}
func startListening() {
guard !isListening else {return}
isListening = true
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let recognitionRequest = recognitionRequest else {
print("SpeechRecognitionViewController recognitionRequest \(self.recognitionRequest)")
return
}
recognitionRequest.shouldReportPartialResults = true
recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
var isFinal = false
if result != nil {
self.query = result?.bestTranscription.formattedString
self.textView.text = self.query
isFinal = (result?.isFinal)!
}
if error != nil || isFinal {
print("recognitionTask error = \(error?.localizedDescription)")
self.stopListening()
}
})
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("Audio session isn't configured correctly")
}
let recordingFormat = audioEngine.inputNode?.outputFormat(forBus: 0)
audioEngine.inputNode?.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, time) in
self.recognitionRequest?.append(buffer)
}
audioEngine.prepare()
do {
try audioEngine.start()
textView.text = "Listening..."
} catch {
print("Audio engine failed to start")
}
}
func stopListening() {
guard isListening else {return}
audioEngine.stop()
audioEngine.inputNode?.removeTap(onBus: 0)
recognitionRequest = nil
recognitionTask = nil
isListening = false
}
// MARK: SFSpeechRecognizerDelegate
func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {
if !available {
let alertController = UIAlertController(title: nil,
message: "Speech Recognition is currently unavailable.",
preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default) { (alertAction) in
.self.stopListening()
}
alertController.addAction(alertAction)
present(alertController, animated: true)
}
}
}
This VC is embedded in another viewcontroller.
When a button is tapped in the parent viewcontroller, startListening() is called. When the same button is hit again stopListening() is called.
The first time the speech recognition works just fine. on a second try I get this error (I guess it has to do with grammar loading?):
recognitionTask error = Optional("The operation couldn’t be completed. (kAFAssistantErrorDomain error 209.)")
and speech recognition doesn't works anymore. After 30 seconds I get the timeout error:
Optional(Error Domain=kAFAssistantErrorDomain Code=203 "Timeout" UserInfo={NSLocalizedDescription=Timeout, NSUnderlyingError=0x170446f90 {Error Domain=SiriSpeechErrorDomain Code=100 "(null)"}})
Original code is here SayWhat
What am I missing?
All I had to do was add recognitionRequest?.endAudio() when trying to stop listening:
func stopListening() {
guard isListening else {return}
audioEngine.stop()
audioEngine.inputNode?.removeTap(onBus: 0)
// Indicate that the audio source is finished and no more audio will be appended
recognitionRequest?.endAudio()
recognitionRequest = nil
recognitionTask = nil
isListening = false
}

Cannot list recorded files to a TableView

I am in some desperate need of assistance, since my head is really sore from beating it against the wall.
I have one view controller where I am recording audio with my microphone and saving it to the apps document directory. This is working well and I can see the files if I navigate to that folder in the simulator.
My problem is that I have a TableView with its own TableViewController where I want to list the contents of that document directory that contains my recordings. I created the TableView and it is reading and populating the table based on a basic array. My problem is that I am failing to convert it over to read the Document Directory contents instead.
I have future goals for this table to also allow me play, delete, or rename the file from within this TableView via swiping the particular cell.
I have been working on this issue for about 5 days now and I am certain I am overlooking something very simple to you all.
I am using Xcode 7.3.1 with Swift 2.
Thanks in advance and I can't wait until I get to a point where I can contribute.
ViewControllerRecorder.swift
(This is where I record and save the audio to the Document Directory, which is working.)
import UIKit
import AVFoundation
import PermissionScope
class ViewControllerRecorder: UIViewController, AVAudioRecorderDelegate, AVAudioPlayerDelegate {
#IBOutlet weak var stopButton: UIButton!
#IBOutlet weak var recordButton: UIButton!
#IBOutlet weak var playButton: UIButton!
#IBOutlet weak var audioDuration: UISlider!
var audioRecorder:AVAudioRecorder?
var audioPlayer:AVAudioPlayer?
let pscope = PermissionScope()
override func viewDidLoad() {
super.viewDidLoad()
audioDuration.value = 0.0
// Set up permissions
pscope.addPermission(MicrophonePermission(),
message: "Inorder to use this app, you need to grant the microphone permission")
// Show dialog with callbacks
pscope.show({ finished, results in
print("got results \(results)")
}, cancelled: { (results) -> Void in
print("thing was cancelled")
})
// Disable Stop/Play button when application launches
stopButton.enabled = false
playButton.enabled = false
// Get the document directory. If fails, just skip the rest of the code
guard let directoryURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first else {
let alertMessage = UIAlertController(title: "Error", message: "Failed to get the document directory for recording the audio. Please try again later.", preferredStyle: .Alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alertMessage, animated: true, completion: nil)
return
}
// Set the default audio file
let audioFileURL = directoryURL.URLByAppendingPathComponent("PWAC_" + NSUUID().UUIDString + ".m4a")
// Setup audio session
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker)
// Define the recorder setting
let recorderSetting: [String: AnyObject] = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 32000.0,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.Medium.rawValue
]
// Initiate and prepare the recorder
audioRecorder = try AVAudioRecorder(URL: audioFileURL, settings: recorderSetting)
audioRecorder?.delegate = self
audioRecorder?.meteringEnabled = true
audioRecorder?.prepareToRecord()
} catch {
print(error)
}
}
func updateaudioDuration(){
audioDuration.value = Float(audioPlayer!.currentTime)
}
#IBAction func sliderAction(sender: AnyObject) {
audioPlayer!.currentTime = NSTimeInterval(audioDuration.value)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func play(sender: AnyObject) {
if let recorder = audioRecorder {
if !recorder.recording {
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: recorder.url)
audioDuration.maximumValue = Float(audioPlayer!.duration)
_ = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: #selector(ViewControllerRecorder.updateaudioDuration), userInfo: nil, repeats: true)
audioPlayer?.delegate = self
audioPlayer?.play()
playButton.setImage(UIImage(named: "playing"), forState: UIControlState.Selected)
playButton.selected = true
} catch {
print(error)
}
}
}
}
#IBAction func stop(sender: AnyObject) {
recordButton.setImage(UIImage(named: "record"), forState: UIControlState.Normal)
recordButton.selected = false
playButton.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
playButton.selected = false
stopButton.enabled = false
playButton.enabled = true
audioRecorder?.stop()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(false)
} catch {
print(error)
}
}
#IBAction func record(sender: AnyObject) {
// Stop the audio player before recording
if let player = audioPlayer {
if player.playing {
player.stop()
playButton.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
playButton.selected = false
}
}
if let recorder = audioRecorder {
if !recorder.recording {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(true)
// Start recording
recorder.record()
recordButton.setImage(UIImage(named: "recording"), forState: UIControlState.Selected)
recordButton.selected = true
} catch {
print(error)
}
} else {
// Pause recording
recorder.pause()
recordButton.setImage(UIImage(named: "pause"), forState: UIControlState.Normal)
recordButton.selected = false
}
}
stopButton.enabled = true
playButton.enabled = false
}
// MARK: - AVAudioRecorderDelegate Methods
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) {
if flag {
// iOS8 and later
let alert = UIAlertController(title: "Recorder",
message: "Finished Recording",
preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Keep", style: .Default, handler: {action in
print("keep was tapped")
}))
alert.addAction(UIAlertAction(title: "Delete", style: .Default, handler: {action in
print("delete was tapped")
self.audioRecorder!.deleteRecording()
}))
self.presentViewController(alert, animated:true, completion:nil)
// let alertMessage = UIAlertController(title: "Finish Recording", message: "Successfully recorded the audio!", preferredStyle: .Alert)
// alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
// presentViewController(alertMessage, animated: true, completion: nil)
print("Audio has finished recording")
print(recorder.url)
}
}
// MARK: - AVAudioPlayerDelegate Methods
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
playButton.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
playButton.selected = false
let alertMessage = UIAlertController(title: "Finish Playing", message: "Finish playing the recording!", preferredStyle: .Alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alertMessage, animated: true, completion: nil)
print("Audio has finished playing")
print(player.url)
}
}
ViewControllerFileManager.swift
(This is the view controller that I want to display the contents of the document directory to a TableView.)
UPDATED 2016/07/27
import UIKit
class ViewControllerFileManager: UIViewController, UITableViewDataSource, UITableViewDelegate {
var arrayRecordings = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let fileManager = NSFileManager.defaultManager()
let dirPath = NSBundle.mainBundle().resourcePath!
let items = try! fileManager.contentsOfDirectoryAtPath(dirPath)
for item in items {
if item.hasSuffix("m4a") {
arrayRecordings.append(item)
}
print("Found \(item)")
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return arrayRecordings.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = arrayRecordings[indexPath.row]
return cell
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
If you are populating the array in viewDidLoad, you need to call reloadData of tableView for it to refresh.
override func viewDidLoad()
{
super.viewDidLoad()
// populate recordingFileNames here
// refresh your table
reloadTableViewContent()
}
func reloadTableViewContent()
{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
self.tableView.scrollRectToVisible(CGRectMake(0, 0, 1, 1), animated: false)
})
}
Here is the code that I got working to populate a TableView with the contents of my apps Document Folder
import UIKit
class ViewControllerFileManager: UIViewController, UITableViewDataSource, UITableViewDelegate {
var arrayRecordings = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// MARK: Read Document Directory and populate Array
// Get the document directory url:
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
do {
// Get the directory contents urls (including subfolders urls):
let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( documentsUrl, includingPropertiesForKeys: nil, options: [])
print(directoryContents)
// Filter directory contents:
let audioFiles = directoryContents.filter{ $0.pathExtension == "m4a" }
print("m4a urls:",audioFiles)
// Get the File Names:
let audioFileNames = audioFiles.flatMap({$0.URLByDeletingPathExtension?.lastPathComponent})
print("m4a list:", audioFileNames)
for audioFileName in audioFileNames {
arrayRecordings.append(audioFileName)
print("Found \(audioFileName)")
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
// MARK: TableView Functions
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return arrayRecordings.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = arrayRecordings[indexPath.row]
return cell
}
// MARK: Standard Functions
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}

How can I display a popup message in Swift that disappears after 3 seconds or can be cancelled by user immediatelly?

In my swift app I have a UIViewController with a single button.
This button invokes a function that calls a popup that disappears after 3 seconds. Also, after that time it prints a message to the console. The code of this function is as follows:
func showAlertMsg(title: String, message: String){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
self.presentViewController(alertController, animated: true, completion: nil)
let delay = 3.0 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
alertController.dismissViewControllerAnimated(true, completion: nil)
print("popup disappeared")
})
}
That works fine, but I wanted to introduce some improvement. I wanted to add there a button that will cancel this popup immediately and then avoid displaying the message in the console. Is there a way of displaying such popup to the user? Also - is there a way of showing in this popup message the counter with number of seconds running out that shows how much time is left until the popup disappears?
You can use an NSTimer to decrement a counter, update the alert view and dismiss the alert view when the counter reaches 0. This code is adapted from my Objective-C answer
class ViewController: UIViewController {
var alertController: UIAlertController?
var alertTimer: NSTimer?
var remainingTime = 0
var baseMessage: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.showAlertMsg("Test Alert", message: "This will disappear in ", time: 5)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showAlertMsg(title: String, message: String, time: Int) {
guard (self.alertController == nil) else {
print("Alert already displayed")
return
}
self.baseMessage = message
self.remainingTime = time
self.alertController = UIAlertController(title: title, message: self.alertMessage(), preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
print("Alert was cancelled")
self.alertController=nil;
self.alertTimer?.invalidate()
self.alertTimer=nil
}
self.alertController!.addAction(cancelAction)
self.alertTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.countDown), userInfo: nil, repeats: true)
self.presentViewController(self.alertController!, animated: true, completion: nil)
}
func countDown() {
self.remainingTime -= 1
if (self.remainingTime < 0) {
self.alertTimer?.invalidate()
self.alertTimer = nil
self.alertController!.dismissViewControllerAnimated(true, completion: {
self.alertController = nil
})
} else {
self.alertController!.message = self.alertMessage()
}
}
func alertMessage() -> String {
var message=""
if let baseMessage=self.baseMessage {
message=baseMessage+" "
}
return(message+"\(self.remainingTime)")
}
}
Just in case someone needs it, this is a Swift 4 version of the #Paulw11 solution
import UIKit
class ViewController: UIViewController {
var alertController: UIAlertController?
var alertTimer: Timer?
var remainingTime = 0
var baseMessage: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.showAlertMsg(title: "Test Alert", message: "This will disappear in ", time: 5)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showAlertMsg(title: String, message: String, time: Int) {
guard (self.alertController == nil) else {
print("Alert already displayed")
return
}
self.baseMessage = message
self.remainingTime = time
self.alertController = UIAlertController(title: title, message: self.alertMessage(), preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("Alert was cancelled")
self.alertController=nil;
self.alertTimer?.invalidate()
self.alertTimer=nil
}
self.alertController!.addAction(cancelAction)
self.alertTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.countDown), userInfo: nil, repeats: true)
self.present(self.alertController!, animated: true, completion: nil)
}
#objc func countDown() {
self.remainingTime -= 1
if (self.remainingTime < 0) {
self.alertTimer?.invalidate()
self.alertTimer = nil
self.alertController!.dismiss(animated: true, completion: {
self.alertController = nil
})
} else {
self.alertController!.message = self.alertMessage()
}
}
func alertMessage() -> String {
var message=""
if let baseMessage=self.baseMessage {
message=baseMessage+" "
}
return(message+"\(self.remainingTime)")
}
}
I know this directly doesn't answer your question, but have you considered using MBProgressHUD SCLAlertView? They both offer functions that allow you to display an alert that disappears after a set amount of time. SCLAlertView allows the user to cancel immediately where as MBProgressHUD does not. If you want more info on how to implement these, let me know so I can add more info!

Resources