I have a small audio app that records audio and then the user can play back. I am having an issue trying to display elapsed and total times.
When I record the file, I use the audiorecorder.currentTime to get the recording length but when I load it in AVPlayer, it shows a different length. It is usually around 200ms off but not always.
When comparing the raw files. the AVPlayer does seem to report the correct time (length).
I am using a timer when recording that fires every 100ms since I don't think AVAudioRecorder has an observer like the AVPlayer, that updates the recording time using the audiorecorder.currentTime.
I've checked out a number of things online and all of the example code I've found seems to have the same issue with the recorded time and player duration not being the same (The examples I have show the recorded time but never show the playback length but I manually loaded the file to check the playback length and it was always longer than the recorded display length) but there has to be a way to do this properly?
Hopefully someone has some ideas or can tell me of a way they worked around this. I am recording using the following settings;
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 16000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
Post
Replies
Boosts
Views
Activity
I have an NSTextView where I am getting the current position in the text when the textview is clicked. For this, I am using the NSClickGestureRecognizer which calls a selector that grabs the click position from the .location method of the recognizer.
It works as expected however the issue I have is that the click doesn't get recognized by the textView so the insertion point cursor doesn't update in the text view. The actual caret position is updated as if I start typing, the text goes where I last clicked even though the insertion point cursor may be somewhere else.
The documentation states that by default, the delaysPrimaryMouseButtonEvents is set to true when using the recognizer with states that the default event will only trigger on gesture failure which isn't what I want as I always want the default event to pass through so I set it to false when initializing the recognizer.
What happens when I do this is that the default even goes through but the gesture method is never called. If I remove that, it triggers the gesture method but doesn't pass the event through.
It's pretty easy to replicate. Just create a new project with an NSTextView, and use the following ViewController code;
class ViewController: NSViewController, NSTextViewDelegate {
@IBOutlet var textView: NSTextView! {
didSet {
let singleClickGesture = NSClickGestureRecognizer(target: self, action: #selector(singleClickGesture(_:)))
singleClickGesture.numberOfClicksRequired = 1 // single
//If delaysPrimaryMouseButtonEvents set to true or omitted, textView never sets insertion point cursor nor clears any selected text
// but singleClickGesture runs every time
//If delaysPrimaryMouseButtonEvents set to false, textView sets insertion point cursor every time
// but singleClickGesture never runs
// I need the singleClickGesture to run then set the insertion point (Cursor Flashing)
singleClickGesture.delaysPrimaryMouseButtonEvents = false
singleClickGesture.delegate = self
textView.addGestureRecognizer(singleClickGesture)
textView.delegate = self
textView.font = NSFont.systemFont(ofSize: CGFloat(16.0))
self.textView.isSelectable = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
textView.string = """
This is a block of text I will insert into the text view.
It will have multiple lines and will be enough text to reproduce the issue.
"""
}
@objc func singleClickGesture(_ recognizer: NSClickGestureRecognizer) {
print("User clicked in textView")
if recognizer.state == .ended {
let caretLocation = recognizer.location(in: textView)
print("Caret location is: \(caretLocation)")
let cursorPosition = textView.characterIndexForInsertion(at: caretLocation)
print("Cursor position is \(cursorPosition)")
}
}
}
extension ViewController: NSGestureRecognizerDelegate {
}
I've been playing around with this for some time and can't get it to work as I need. If anyone has any suggestion, I would love to hear them.
Thx
I have implemented a custom URL scheme for my app and things were working fine until all of a sudden, launching the app via URL (In Safari for example) didn't seem to be taking any of my code changes. If I ran the app from XCode, it was fine.
After beating my head against the wall for a bit, I found that the URL scheme was launching the application from my xcode Archive folder which obviously doesn't have any of my latest code.
I can't figure out how to change this. Prior to this, launching via URL would launch from the build folder. I'm sure I could remove the Archive folder and re-associating but I'm hoping there is a better way to accomplish this.
Thx
I am using information from the post at https://developer.apple.com/forums/thread/98830
Hi,
I am using the example from the above forum post in my Swift app and it's working perfectly however pre-compiling the app is causing me an issue.
My script is checking if an app existing using Finder and does this before doing other tasks but for some reason. when my app first runs and the script compiles, it is looking for the app and prompting the user to choose it. Any subsequent script calls work as expected.
let script = NSAppleScript(source: """
on lazyPasteRDP()
try
tell application "Finder" to get displayed name of application file id "com.microsoft.rdc.macos"
set appExists to true
on error
set appExists to false
end try
if appExists
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "Microsoft Remote Desktop" is in activeApp then
tell application "myApplication"
activate
end tell
tell application "Microsoft Remote Desktop"
activate
delay 0.5
tell application "System Events" to keystroke "v" using control down
end tell
else
tell application "System Events" to keystroke "v" using command down
end if
end tell
else
tell application "System Events" to keystroke "v" using command down
end if
end lazyPasteRDP
"""
)!
Is there a way to prevent prompting the user to choose the app during compile time? Again, once the script is compiled, the logic works as expected and doesn't prompt again.
The other option would be to only compile the script on a condition but my feeble attempts haven't worked.
I tried to post a comment in the original thread but couldn't get enough detail in the 500 character limit
I have an app that has field delimiters in a textField and I have a function that can go to next field or go to previous field. It works as expected when called as a function but I want to assign the Tab and Shift tab to functions as well. I added some logic to only process the function if the window with the field delimiters is open.
It works however it also inserts a Tab character in the document which I don't want. I want to override the default Tab insertion with my function similar to how it working in JS with preventDefault.
Here is the tab key check code;
if (keyPressedBitwise == tabKeyBW) && (editorWindowOpen == true) {
if !event.isARepeat {
print("***NOT A REPEAT, LETS FIRE IT OFF***")
if event.type.rawValue == 10 {
print("Sending next field command")
sendNextFieldKey()
}
}
return
}
I use this code in other places but they keys are assigned to non-character keys ie: F3, Ctrl+Home etc... and I don't seem to have an issue anywhere else.
My thought is maybe the Tab character is getting inserted into the document before it hits my code.
If I have to I can assign a non-character code to the next and previous field function but would prefer to use the Tab and Shift tab as they are familiar.
Any suggestions are appreciated.
I have a combobox in a View Controller that I am trying to populate using an enum. I did some research and think I've done it correct but whenever the view controller launches, I get an error Illegal NSComboBox data source.
I thought it may be an issue with the ComboBox (It was copied from another one in the View Controller) so I tried creating one from scratch but I am getting the same thing. I can populate the ComboBox using the addItem no problem.
The Combobox does have Use Data Source selected in the Storyboard. Here is my code;
enum Server_Locations: String, CaseIterable {
case us = "United States"
case canada = "Canada"
case other = "Other"
}
class PreferenceController: BaseVC, NSComboBoxDelegate, NSComboBoxDataSource {
@IBOutlet weak var countryComboBox: NSComboBox!
override func viewDidLoad() {
super.viewDidLoad()
countryComboBox.dataSource = self
countryComboBox.delegate = self
numberOfItemsInComboBoxCell(aComboBox: countryComboBox)
comboBoxCell(aComboBox: countryComboBox, objectValueForItemAtIndex: 0)
}
func numberOfItemsInComboBoxCell(aComboBox: NSComboBox) -> Int {
return Server_Locations.allCases.count
}
func comboBoxCell(aComboBox: NSComboBox, objectValueForItemAtIndex index: Int) -> AnyObject {
return Server_Locations.allCases[index].rawValue as AnyObject
}
It seems pretty straightforward but I'm obviously doing something wrong? I think the issue is in the calls to numberOfItemsInComboBoxCell and comboBoxCell
I've checked some examples online and they have referencing outlets to the delegate and the dataSource but I am defining them in the viewDidLoad().
I'm using Swift 5 in Storyboard mode with XCode 14.2
I have a view controller that is attached to my main menu via Segue and I am trying to figure out how to programatically call that segue?
I can't use the performSegue() as when I try to use it on an NSMenu object, I get an error saying NSMenu has no method performSegue.
I tried just calling self?.performSegue(withIdentifier:"mySegueName", sender: nil) from another viewController where I want to call the segue but the segue doesn't exist in that viewController so I'm assuming that's why it does nothing.
Does anyone have any suggestion as to how I can call this?
I am trying to troubleshoot an issue that surfaced recently that I am having difficulty fixing as it is only happening on the exported (Archived) releases of my app. Error in crash log is Namespace SIGNAL, code 4, Illegal Instruction 4
My project uses a cocoapod to work with hardware devices and this error happens only for specific device and only on client machines.
For testing, my Xcode dev machine and my test machine are both Macbook Pros running 12.6.
Before testing, I delete the old app completely, remove any privacy entries, and preferences files, caches etc.
When I first install the app (No installer, just coping .app file), the app launches and it doesn't throw the error and works. If I close the app and reopen, as soon as I try to use the device, app crashes. I use a different hardware device and there are no issues. I tested on another client as well with the same hardware and same issue.
The thing that makes this difficult is that I can run it in debug mode from XCode with no issues.
I did some research but everything I've seen seems to be about this happening on app launch but that's not happening here. Plus, it works first time app launches but on subsequent launches, as soon as I use the hardware device it crashes.
Looking at the crash log, the crash happens as soon as it receives the data from the HID device. But again, different hardware model doesn't crash, on my dev machine, doesn't crash. Also, if I run the app on my dev machine as a standalone, it doesn't crash either.
I'm hoping something can provide me with some troubleshooting or debug ideas to help me isolate this issue. I've never had this type or crash not reproducible on a client machine so the extent of my troubleshooting that way is limited to crash dumps.
I drive removing all DerivedData as I found on StackOverflow and that actually made the app run twice. Then crashed.
I am trying to get the PKContactField.postalAddress prior to displaying the Payment Sheet so that I can use the postalCode to calculate taxes.
I've found examples on how to get the information after the user has selected the billing details from the payment sheet but I can't figure out how to get it before.
I have included the requiredBillingFields = [.postalAddress, .name] and it is displaying on the payment sheet.
It looks like I should be able to get it from the ApplePayContactField prior to displaying the payment sheet but I can't figure out how to implement.