Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this code where I want input from the iPhone's microphone. I want the input ten times a second given the sampling rate of 44100 Hz. The tap has to occur "every 4410 samples". But no matter how I do it, the tapping occurs every ~400 ms giving me 16384 samples each time.
What is the correct way to control the tapping frequency?
self.audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setPreferredSampleRate(44100.0)
try audioSession.setPreferredIOBufferDuration(4410.0 / 44100.0)
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setActive(true)
audioSession.requestRecordPermission() {
[unowned self] (allowed: Bool) -> Void in
if allowed {
self.audioEngine = AVAudioEngine()
self.audioInputNode = self.audioEngine.inputNode!
let format: AVAudioFormat = self.audioInputNode.outputFormatForBus(0)
self.audioInputNode.installTapOnBus(0, bufferSize: UInt32(4410), format: format, block: {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) in
// buffer length is 16384
})
do {
try self.audioEngine.start()
} catch {}
} else {...}
}
} catch {...}
I took a break from this problem and returned to it today with XCode 8.1
The above code just works now and I have no idea why. Bug in earlier version of AVAudioSession?
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert this Swift code to Objective C. I searched and did not find a way to convert automatically . Is there any way that can help convert ?
let token="0000000009"
SkyIdConfig.shared.setup(With: token)
var skyIdView:SkyDocumentsAnalyzer?
SkyIdConfig.shared.loadAndConfigure(){[unowned self] isConfigured,error in
if error != nil || !isConfigured {
print(error?.localizedDescription ?? "Error !!!!")
return
}
DispatchQueue.main.async {[unowned self] in
self.skyIdView=SkyIdBuilder.shared.getDocumentAnalyzerInstance(with: "03", lang: "fra")
if self.skyIdView != nil
{
self.skyIdView!.delegate=self
self.skyIdView?.modalPresentationStyle = .fullScreen
self.present(self.skyIdView!, animated: true, completion: nil)
}else{
print("Error in config loading")
}
}
}
Of course there is a way to convert ..
..manually. Which is the best way to understand whats going on.
your token is probably an
NSString *token = #"0000000009";
depending on the Class definition you have to look up how SkyIdConfig is done..
//assuming its something like..
[[SkyIdConfig shared] setupWith:token];
SkyDocumentsAnalyzer *skyIdView = [[SkyDocumentsAnalyzer alloc] init];
[[SkyIdConfig shared] loadAndConfigure];
before going forward in code read more about dispatching in objective-c (aka c) here...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I basically have this issue where this Reporting API only allows me to call it once each 90 seconds.
I'm building an app where I get the data each time you open it, to get the latest data, but I somehow need to stop the app from calling the API if it was before 90s wait and show cached data, how can I do this?
Each time you go to ping the API compare the current time to the last time it was done. Kinda like this:
// set defaults for storage
let userDefaults = UserDefaults.standard
// set time
let date = Date()
//Check Storage
if let theDate = userDefaults.object(forKey: "date") as? Date {
// on successful load compare times
if date.timeIntervalSince(theDate) > 90000 /* I think it runs in milliseconds so I put 90 seconds worth there*/ {
// do API call here
// save time
userDefaults.set(date as! Any, forKey: "date")
} else {
print("too soon")
} else {
// data not successfully loaded
// try to ping API here too
// save time
userDefaults.set(date as! Any, forKey: "date")
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am learner on iOS Development,today i try to code somethingenter image description here about FileManger,but this method return a type '()',how can i to resolve it?
the warning code as follow:
The problem is that your call to createDirectoryAtURL does not have a explicit return value. In Swift, that means it returns a Void which is represented by the empty tuple (). This is what your call should look like.
let temp = NSTemporaryDirectory()
let newDiretoryURL = NSURL.fileURLWithPath(temp + "/MyNewDirectory")
try fileManager.createDirectoryAtURL(newDiretoryURL, withIntermediateDirectories: false, attributes: nil)
print("Success")
}
} catch {
// handle errors here
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following object:
(rates)
{
EUR: "1"
RON: 4.5
USD: 1.3
.
.
.
n: INT/STRING
}
Is there any function that does this?
if you care about the index, you can use a 'traditional' for loop - although #Eric points out this will soon be completely removed
for var i = 0; i < rates.count; i++
{
let rate = rates[i]
// do stuff with rate
}
The enumerate approach looks like this
for (index, rate) in rates.enumerate()
{
print("Do stuff with \(rate) at position \(index)")
}
if you just need each object in turn it's a bit easier
for rate in rates
{
// do stuff with rate
}
Because it's a dictionary, no enumerate() required:
var aDictionary: [String: Float] = ["EUR": 1, "RON": 4.5,
"USD": 1.3]
for (index,item) in aDictionary{
print(index,item)
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am connected to the server via TCP / IP and receives [UInt8]. I know that it is the audio. How to play the stream on the iPhone?
#IBAction func connectAndListen(sender: AnyObject) {
var client:TCPClient = TCPClient(addr: "80.233.248.96", port: 6969)
var (success,errmsg)=client.connect(timeout: 1)
if success{
while (success){
var (success,errmsg)=client.send(str:"GET / HTTP/1.0\n\n" )
if success{
var data = client.read(1024*10)
if let d = data{
var endMarker = NSMutableData(bytes: d, length: d.count)
println(endMarker)
self.audioPlayer = AVAudioPlayer(data: endMarker, error: nil)
self.audioPlayer?.prepareToPlay()
self.audioPlayer?.play()
}
}else{
println(errmsg)
break
}
}
}else{
println(errmsg)
}
}
my Crash:
fatal error: unexpectedly found nil while unwrapping an Optional value
on this self.audioPlayer!.prepareToPlay() Print screen my crash
Create an NSData object from the bytes in your stream.
Create an AVAudioPlayer using the initWithData:error: method.
Play the audio using the AVAudioPlayer play method.
I cannot tell you exactly how to create the NSData object, because I don't know how you get your byte stream, but the NSData class is well documented in the Apple documentation.