NStimer and Dispatchqueue starts a little late when using the airpot - swift

My app provides voice to AVPlayer and gives a little term using Timer in the middle of playback so that users can read along.
It works well on simulators and real devices.
However, when you pair the air pot, the timer starts about 0.2 seconds late.
Why does this happen and how can I fix it?
I used 'Timer.scheduler' and 'Dispatchqueue.Main.asyncAfter', but both had the same symptoms.

I gather that you are starting playback and simultaneously starting a timer, but when you do this with Bluetooth earphones, that they don’t seem in sync. If that’s the case, I might suggest not using your own timer, but rather let the AVPlayer tell you where it is.
For example, you might use addPeriodicTimeObserver(forInterval:queue:using:). The AVPlayer tell you when it got to a particular point in the playback.

Related

AVAudioPlayerNode - Get Player State?

In an iOS-project I am using the AVAudioPlayerNode in conjunction with the AVAudioEngine and an AVAudioUnitTimePitch. Everything works peachy. However, I was wondering if there is a way to figure out what the current player's state (e.g. isPlaying, isPaused) or at least the playback position is.
While AVAudioPlayer at least allows you to get the currentTime-parameter, I could not yet figure out how to get that information with AVAudioPlayerNode. I tried playing around with the nodeTimeForPlayerTime and playerTimeForNodeTime methods described in the swift documentation but I couldn't make any progress.
Any help would be highly appreciated.
Since the AVAudioPlayerNode is designed as an audio stream, it doesn't necessarily keep track of the time within a particular file. However, the AVAudioPlayerNode does keep a running time of how long its been playing all audio. This timer doesn't reset with each file, in order to change it, you must explicitly tell it where you want to start counting from.
So to find the current time the player has been playing you must do the following:
player.lastRenderTime.sampleTime / file.fileFormat.sampleRate
Now in order to get the timer to reset after each file, you must explicitly reset the players current time. To do this use the player.playAtTime: function.
If you would like an example, check one out here: https://github.com/danielmj/AEAudioPlayer

Using AVAudioPlayer, my sounds (sporadically!) play weird

I've done a simple audio playback GUI implemented using AVAudioPlayer.
When playing my sound, I use a UISlider to provide playback feedback...
Here's where it gets weird.
I have a problem that happens very (very) sporadically - and mostly it doesn't happen, making it really hard to debug.
The problem is that sometime, once the sound ends, and I play it again, it's as if it starts looping (as if I set numberOfLoops to -1), without ever calling audioPlayerDidFinishPlaying.
Now, no where is my code do I "touch" numberOfLoops - it defaults to 0, and I leave it that way.
To make the problem weirder, then once this problem happens, I don't "hear" the audio - though it does appear to play (i have a timer function that provides the visual feedback, and it checks the sound is playing...)
Any ideas? Directions?
I faced the same issue once and I got around the issue by increasing the updating timer interval to .25 seconds,which was earlier .1 seconds. Also I tried to avoid files with very small duration. Hope this helps you to some extend. Please try it and let me know if it worked for you.

How to play sound at precise moments on iPhone?

I'm working on creating a simple metronome on the iPhone. What the app does right now is to run a timer, entering the timer's function every 1/1000th of second. Then it checks the current time vs time of starting the app (I'm using CACurrentMediaTime() function).
CFTimeInterval currentTime = CACurrentMediaTime();
if (self.beatingStartTime == 0) {
self.beatingStartTime = currentTime;
}
if ( (currentTime - self.beatingStartTime) >= self.timeIntevalBetweenTicks * self.internalTimerCounter ) {
self.internalTimerCounter ++;
// ...
}
If there is a good moment to play audio, the code using OpenAL to play it gets fired.
Basicly that's it. I checked the sounds played when running both in simulator as well as on 2 devices (iPad and jailbroken iPhone 3GS) and there is a problem - when I recorded the sound and reviewed the waveform in Reaper software, some sounds play a bit too late, and some of them - bit too early (even I could understand the "too late" part, I don't really get how it can play earlier then it should - since the app checks the number of seconds every time, it basicly can't be ealier then specified time - yet it is, according to my recods).
At the same time there are some metronome apps that are known for being "rock-solid" when it comes to timing, so I guess there is a way. I just wonder what I'm missing...
edit: Changing timer call from 1/1000th second to, for example 1/100th doesn't help.
edit 2: When I switched from timer to threads (and I put the thread to sleep for specified time) I still get a strange behavior. The tempo moves around and while I could understand a little lag and playing some sounds too late, the problem is some of them do play too early - it means time distance between 2 beats is less then the time that should pass.
The diffrence is about 3%, which translates to about 10-15 miliseconds, which is quite a lot for me. Anyone got an idea why the sound can play earlier? I tried it both on iPhone simulator and on iPad actual device, and my only guess is there's something wrong with the timer - CACurrentMediaTime() returning more seconds that it should. Is it even possible?
Try using an NSSound, and load it up as an instance variable and don't release it unless your metronome isn't running. Delays can be caused by loading the file into memory on the loop. The other thing to consider is that a metronome probably doesn't need to poll every 1/1000th of a second. If you do it less often, you're less likely to saturate the CPU and you might get more consistent results.
Lastly, check out how Apple's demo works: http://developer.apple.com/library/ios/#samplecode/Metronome/Introduction/Intro.html
Might give you a better idea how to accomplish what you're trying to do :)
What you want is COCOS DENSHION which is a simple reliable easy to use sound library, that we have found solves all problems.
I (just personally) don't like "Cocos2D" but you can just take and use CocosDenshion.
Secondly -- 1000th of a second is ridiculous for a timer. Just totally forget it.
Thirdly -- AVAudioPlayer is worthless as you found.
Note - "ObjectAL" is a new, perhaps better, alternative
to CocosDenshion. Check it out.

AudioServices (Easy), AVAudioPlayer (Medium), OpenAL (Hard & Overkill?)

I need to play sounds (~5 seconds each) throughout my iphone application. When they're triggered, they need to play immediately.
For the moment I'm using AudioServices and (as you probably know) the first time you play a sound it lags, then every time there after it's perfect. Is there some code available that's clever enough to preload an AudioServices sound (by playing it silently maybe?). I've read adjusting the system volume programmatically will get your app rejected, so that's not an option. Seems AudioServices isn't made for volume correction from what I can see.
I've looked into OpenAL and while feasible seems a little over kill. AVAudioPlayer seems like a little bit of a better option, I'm using that for background music at present. Extending my music player to handle a 'sound board' might be my last resort.
On the topic of OpenAL, does anyone know of a place with a decent (app store friendly) OpenAL wrapper for the iPhone?
Thanks in advance
Finch could be perfect for you. It’s a tiny wrapper around OpenAL with very low latency and simple API. See also all SO questions tagged ‘Finch’.
If you use an AVAudioPlayer, you can call prepareToPlay when you initialize the object to reduce the delay between calling play and having the audio start.

iPhone MPMusicPlayerController playback starting position

I'm using Media Player Framework to access the user's music library on iPhone. I would like to set the playback starting position so that I can start playing a song from 30 second mark, for example.
I have trouble finding out how to do this. The MPMediaPlayerController only offers beginSeekingForward but that's not quite what I'm looking for as it simply accelerates the playback speed.
There is probably something really simple that I'm missing.
MPMusicPlayerController's property currentPlaybackTime is a writeable property, so adjusting the playback starting point can be done with player.currentPlaybackTime = 30.0
You can use player.currentPlaybackTime to set the time, before you start playing and playback will start at your desired point.
UPDATE
2009 me had some real problems. He didn't really understand properties and missed the fact that MPMusicPlayerController.currentPlaybackTime is writable! And he was angry. Angry because iOS3.0 had promised iPod Library "Access" and instead delivered MPMusicPlayerController. He had been hoping for speedy access to the music packet data upon which he would have built many fascinating and magical audio applications. Luckily, iOS4.1's AVAssetReader came along 1 year later and he was finally able to stop hating.
WRONG 2009 ANSWER
Nope, this API is deliberately crippled, which is why you don't see any functions for
opening, or streaming from, the media file.
Your only hope is lowering the volume and calling beginSeekingForward until currentPlaybackTime returns >= 30s.
Enjoy!