Post

Replies

Boosts

Views

Activity

How can I open an audio file into a buffer that I can read pieces of said buffer?
I would like to open an audio file on my iOS device and remove long silences. I already have the code for calculating volumes so am not pasting that here. What I am unsure of "how to do" is: While I believe that I have the proper code to read the file below, I am unsure as to how to read it in proper pieces to I can later get the volume of each piece. I realize that this might be a situation of calculating the size of frames and whatnot. But I am totally green when it comes to audio. I would seriously appreciate any guidance. guard let input = try? AVAudioFile(forReading: url) else { return nil } guard let buffer = AVAudioPCMBuffer(pcmFormat: input.processingFormat, frameCapacity: AVAudioFrameCount(input.length)) else { return nil } do { try input.read(into: buffer) } catch { return nil }
2
0
918
Dec ’22
Is there a global Alert View in SwiftUI?
I am writing a SwiftUI based app, and errors can occur anywhere. I've got a function that logs the error. But it would be nice to be able to call an Alert Msg, no matter where I am, then gracefully exits the app. Sure I can right the alert into every view, but that seems ridiculously unnecessary. Am I missing something?
2
0
114
Apr ’25
How can I access an array in one of my classes?
myGV is a structure where I store a handful of global variables. The following are all sub-classes of SKSpriteNode: I have a class called guessingBar which holds 6 guessSlots. The former class has an array of the guessSlots for me to loop through. I just don't know the syntax of how to access the array. myGV holds multiple variables, so the SKSpriteNode guessBar can be found at: myGV.guessBar I expected to be able to read the array with: myGV.guessBar.guessSlots[x] but as you can see from the debugger screenshot, I cannot. In the screenshot you can see that everything is initialized. Am I missing some silly typo, or is the syntax escaping me? http: //98.7.37.117/s.png
3
0
618
Jun ’21
How can I pass a function pointer in Swift?
I would like to take this sample code override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50)) button.backgroundColor = .greenColor() button.setTitle("Test Button", forState: .Normal) button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) self.view.addSubview(button) } func buttonAction(sender: UIButton!) { print("Button tapped") } ...and place the code to create the button in another file. So it would look more like this: class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() makeButton(vControl: self) } @objc func buttonAction(sender: UIButton!) { print("Button tapped") } } Of course the main flaw is that I would have to pass a pointer to the function buttonAction. Something like we do in C makeButton(vControl: self, bFunc: &buttonAction) Have Googled a lot but can't seem to find the way to do this. How do I set up the makeButton page to recieve this? func makeButton (vControl: ViewController, bFunc: ???) What would the ??? be in reality? thanks
3
0
1k
Oct ’21
How can I use a custom image for a Swift button but still control the text separately?
Below is a simple code patch I use to create a custom button, am new at this. I noticed the text does not appear and am wondering what the proper method is for making the text appear? func makeButton (vControl: ViewController, action: Selector) { let myButtonImage = UIImage(named: "Picture1.png") let imageScale = myButtonImage!.size.width / myButtonImage!.size.height let wwidth = vControl.self.view.bounds.width let button = CreateButton(frame: CGRect(x: 100, y: 100, width: (myButtonImage?.size.width)!/3, height: (myButtonImage?.size.height)!/3)) button.setImage(myButtonImage, for: .normal) button.backgroundColor = .clear button.setTitleColor(.black, for: .normal) button.setTitle("Test Button", for: .normal) button.addTarget(vControl, action: action, for: .touchUpInside) vControl.view.addSubview(button) button.center = vControl.view.center }
3
0
2.0k
Oct ’21
Why does a dismissed child UIView doesn't reappear properly after its first appearance?
Thanks to people on this board I am able to successfully calla up a child UIViewConroller via animation with: This is the buttonAction from the Main UIViewController, which calls up setController @objc func buttonAction(sender: UIButton!) { guard let theButton = sender as? MyButton else { return} UIView.transition(with: self.view, duration: 0.5, options: .transitionCurlDown, animations: { [self] in self.addChild(setController); self.view.addSubview(setController.view); }, completion: { [self]_ in setController.didMove(toParent: self); setController.doLayout();}) } the doLayout method lies within the child: func doLayout (){ guard let parent = cView!.view.superview else {return} //make sure UIV honors safeAreaLayouts setConstraints(vc: self, pc: parent) } A button within the child, setController, dismisses itself: @objc func buttonAction(sender: UIButton!) { self.willMove(toParent: nil) self.removeFromParent() self.view.removeFromSuperview() self.dismiss(animated: false, completion: nil) } Everything works great the first time I call up the child UIView. It curls down while covering the first/parent UIVIEW, etc. etc. Figure 1 But after I dismiss the child view and call it again, the child view scrolls down without really covering the main view, it's like a mishmash. Figure 2 Only after all is said and done, then the child view covers everything. So am curious if I am dismissing something incorrectly.
3
0
776
Dec ’21
Can I create a class instance using DispatchQueue.global().async and have its methods run asynchronously?
I created the playground below to answer my question "If I created a class instance using DispatchQueue.global().async would that class remain in its own asynchronous queue? Even if the main app called one of that classes methods, would that method run asynchronously compared to the main app? With the sleep line I discovered that the answer is "no." But I am curious if there is a legit way to do this? Or even if there is, it is considered bad programming? import UIKit class MyFunx : NSObject { var opsCount = 0 override init() { super.init() } func addValues (a: Int, b: Int) { let c = a + b opsCount += 1 sleep(1) } } var firstVar = 0 var secondVar = 0 var myFunx : MyFunx? while secondVar < 100 { print ("starting") if myFunx == nil { print ("making myFunx") DispatchQueue.global().async { myFunx = MyFunx() } } else { myFunx!.addValues(a: firstVar, b: secondVar) } firstVar += 1 secondVar += 1 } print ("myFunx = \(myFunx)") print ("\(myFunx?.opsCount)") print ("exiting")
3
0
774
Jan ’22
How can I get my app to wait for a permission request to complete?
Am working on a recording app from scratch and it just has the basics. Within my info.plist I do set Privacy - Microphone Usage Description Still, I always want to check the "privacy permission" on the microphone because I know people can hit "No" by accident. However, whatever I try, the app keeps running without waiting for the iOs permission alert to pop up and complete. let mediaType = AVMediaType.audio let mediaAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: mediaType) switch mediaAuthorizationStatus { case .denied: print (".denied") case .authorized: print ("authorized") case .restricted: print ("restricted") case .notDetermined: print("huh?") let myQue = DispatchQueue(label: "get perm") myQue.sync { AVCaptureDevice.requestAccess(for: .audio, completionHandler: { (granted: Bool) in if granted { } else { } }) } default: print ("not a clue") }
3
0
1.6k
Feb ’22
Getting strange message in debugger since Xcode last updated
Xcode updated yesterday, and am now at Xcode Version 13.3 (13E113) Since then, whenever I debug my app the the simulator, I get the message below. Am curious if I should be concerned. If it makes a difference: Yes, I have an IBM keyboard attached via USB. Also this does not happen when I debug the app on the iPhone itself. 2022-03-15 14:50:45.745237-0400 Hello World[48883:648134] [HardwareKeyboard] -[UIApplication getKeyboardDevicePropertiesForSenderID:shouldUpdate:usingSyntheticEvent:], failed to fetch device property for senderID (778835616971358211) use primary keyboard info instead.
3
1
7.1k
May ’22
What is the difference between func (to view: UIView) and not using the 'to'?
I have a function in my UIView extension and I can call it both ways This way: func anchorSizePercent(to view: UIView, sFactor: CGSize) { ... } myHeader.anchorSizePercent(to: myView, sFactor: CGSize(width: 0.8, height: 0.2)) As well, I can call it without the to, such as: func anchorSizePercent(view: UIView, sFactor: CGSize) { ... } myHeader.anchorSizePercent(view: myView, sFactor: CGSize(width: 0.8, height: 0.2)) May I ask what the difference is?
3
0
497
Mar ’22