is there a best practice for getting an event on every second of playback of an AVAudioPlayer instance? I need to change a view automatically depending on the current playback time.
Thank You!
You could start one or more instances of NSTimer when playback starts, and change the view when the timer(s) fire.
Related
I'm working on a multiview app. One of the views is a table view. Each cell has a stopwatch. I'm planning to use NSTimer for the stopwatches. Do I need to implement multithreading for the timers to work properly even when the user switches the view and then comes back later?
I did my research but most of the tutorials cover one NSTimer in a single view. I want to make sure the user can do other things while the timers are running, like use the interface, navigation, etc. In another post Placing an NSTimer in a separate thread someone said you need a different runloop for the timer. Would I need one runloop for each timer in my case? Is it advisable? Any performance drawbacks?
Thanks a lot!
One run loop should be just fine. Your interface will still be responsive.
Keep in mind that timers are never guaranteed to be accurate. They are affected by how much other stuff is on the same loop. Its ok to use the timer to update the display but not to actually measure time. Set an NSDate when you start a stop watch then compare the current date with that start date each time your display timer updates the display.
Since you should only use the NSTimer to update the display, could you just use one generic display update timer that updates all running stopwatches, instead of having one for each stopwatch?
I'm wondering if iOS allows one to do the following:
I have a puzzle game and I've been working on saving data when the user returns to the home screen. To do this, using NSNotificationCenter, I had one of my game classes to observe [UIApplication sharedApplication]'s ApplicationWillResignActive method so when that happens I save my game state. But if the user decides to exit while animations are going on, the game will save a midState where the model values are still changing and that will often cause crashes. My question is if it is possible to somehow delay the saving process (even though it is on the background) until the animations are complete (or some variable equals 1)?
My first idea is to create scheduled event with NSTimer to try to save until everything is set. Any ideas would be appreciated. Thank you
You can use performSelector:withObject:afterDelay:
// Call doSomething after a 1 second delay
[self performSelector:#selector(doSomething) withObject:nil afterDelay:1.0f];
Rather than trying to delay the saving, especially in ApplicationWillResignActive, you should look into how to stop the animation and record the expected final values of the animation. Depending on the method of animation (UIView static methods, block based, 3rd party) there is usually a way to stop them, and since you define what the animation does you should already know the final state.
In the case of block based animations:
How to cancel UIViews block-based animation?
In my application have UIBarButtonSystemItemPlay on the mainwindow and i want to implement something like this as audiofile starts playing it opens pages one by one synchronized with the audiofile.
Anyone have any ideas how to implement it.
Thanks for help.
My best suggestion would be the following:
Create an NSMutableArray containing NSIntegers representing the time-marks where you want the pages to change.
Set a timer to start running when the audio file starts. Have it increment an NSInteger every second.
In the timer's selector method, test the incremented number against the array and see if it's there.
If there, go to the next page.
I have built iPhone game application which has loads of views. The next view gets decided based on the user's current action. It working fine absolutely. I want to start a timer when my game gets started and want to keep incrementing till game finished. Nothing fency! The problem is I can put label on view and use NS Timer ticks to increment its value but how to synchronize that value when move to next view?
Could anyone please let me know if there is already framwork available that support this or any way to implement this.
Thanks.
The easiest solution would be to have the timer be a property in your app delegate, so you can access it from any view/view controller. That's what I'd do here. It doesn't seem to "belong" to individual views based on what you've described.
ViewDidLoad doesnt work.
ViewDidappear Doesnt work.
No matter what happens, the sound is played then the viewController image is displayed.
What I want is the image to be displayed, then the sound plays simultaneously'
did you try viewWillAppear?
That could be because things happen in SEQUENCE unless you give explicit instructions that you want things to happen at the same time. If you want a sound to play WHILE an image is loading you need to use threads.
This guide explains how to accomplish that
I think viewWillAppear might work. viewDidLoad only gets called once, so it wouldn't get played every time a view controller is pushed.
I ended up making the sound effect have an extra 1 second pause at the beginning of it and playing it in viewdidappear. Not the perfect solution, but it was the easiest.
The sound was originally being played, before the phone actually rendered the viewcontroller, which I found counter intuitive seeing as the method 'viewdidappear' kinda sounds like the view should have erm already appeared.
Does that make sense?