I am Developing an ios app for audio player.
Here now i have to move one slider that represents the current playing track of the song
For Example my song duration is 6sec now i have to move the slider from x point =0 to x=480 by the time audio playing should Finnish it paying one loop.
I am moving the based up Nstimer. now i required the nstime interval for nstimer to move the slider correctly to end that is x=480.
myTimer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:#selector(updatplayer) userInfo:nil repeats:YES];
here i need this timeinterval value
can any one help me ?
You can use your timer as:
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(updatplayer) userInfo:nil repeats:YES];
- (void)updatplayer {
long currentPlaybackTime = moviePlayer.currentPlaybackTime;
playSlider.value = currentPlaybackTime / moviePlayer.duration;
}
You can set your time interval as a class property and access it from there. In example:
#interface YourClass ()
#property (nonatomic, assign) NSTimeInterval myTimeInterval);
#end
#implementation YourClass
- (void)methodWithYourTimerInIt {
// ... Do stuff
self.myTimeInterval = 6.0;
myTimer = [NSTimer scheduledTimerWithTimeInterval:self.myTimeInterval target:self selector:#selector(updatePlayer) userInfo:nil repeats:YES];
}
- (void)updatePlayer {
NSLog(#"My time interval is: %f", self.myTimeInterval);
}
#end
Related
My ProgressView doesn't update the progress.
I Did this by Interface Builder.
My ProgressView is in a tableViewCell.
//Declared in TableViewCell
#property(nonatomic,retain) IBOutlet UIProgressView *progressview;
and also Synthesized in Cell implementation.
then imported in my viewController
//Declared AVAudioPlayer
AVAudioPlayer *player;
in cellForRowAtIndexpath method :
obj.progressview.progress = 0.0;
in didSelectRowAtIndexPath method :
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(methodToUpdateProgress) userInfo:nil repeats:YES];
m_timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:YES];
For every 1 second my methodToUpdateProgress is Called
-(void)methodToUpdateProgress{
dispatch_async(dispatch_get_main_queue(), ^{
[obj.progressview setProgress:(float)(player.currentTime/player.duration) animated:YES];
NSLog(#"method");
});
}
I have a button with 2 actions Play and Pause.
Those 2 actions works completely fine.
If I click on a Row in tableview, corresponding song plays, timer starts and for every 1 sec it called my methodToUpdateProgress,but my ProgressView doesn't show any progress.
Help me what should I do ?
I created a timer, and was wondering if it is at all possible to make the timer go slower when it reaches a certain second: 1..2..3..4..4.1..4.2.
For example, the timer is increasing by 1 second, then at 4 seconds, the time slows down showing milliseconds.
if(i = 4) {
}
It depends on what you want.
Of course, you can't slow down the time, but you can simulate it.
For example, you check how much time has already passed. If it is 1, 2, 3 seconds, you NSLog 1, 2, 3. Then when 4 seconds has passed, you start logging milliseconds.
E.g.
if (i < 4) {
NSLog(#"%d", i);
} else {
NSLog(#"4.%d", i - 4);
}
Sure, logging can be substituted with whatever you need.
P.S. The answer is in Objective-C, but that changes nothing.
You can use a solution like this:
Declare a float to register your time and a NSTimer to perform the action.
#property NSTimer *timer;
#property float time;
Call startTimer to start the actions.
- (void) startTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(secondsAction)
userInfo:nil
repeats:YES];
}
- (void) secondsAction {
self.time += 1;
NSLog(#"%d", (int)self.time);
if(self.time == 4) {
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(milisecondsAction)
userInfo:nil
repeats:YES];
}
}
- (void) milisecondsAction {
self.time += 0.1;
NSLog(#"%.1f", self.time);
}
Im making an iphone game that uses NStimer for movement. I found out that there is a bug that makes the timers stack some times, but i havnt found out what causes is. Is there any way to see how many timers are allocated and is there any way to prevent it by doing something like this:
If(myTimer.numberOfAllocatedTimers == 0) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES];
}
Try this:
// use this to start timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(doSthing) userInfo:nil repeats:YES];
// use this to stop timer
if (myTimer) {
[myTimer invalidate];
myTimer = nil;
}
A little more context on where this code is being used would be helpful.
However, I'll try to explain why it's happening anyway.
Every time you use [NSTimer scheduledTimerWithTimeInterval...], a new timer is created, and will start repeating. If your code creates a timer inside the callback method, see example below, you'll stack timers.
- (void)updateme {
if ([NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES]) {
// This creates stacked timers!
}
}
Here's how you should do it:
#property (nonatomic, strong) NSTimer *myTimer;
...
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme:) userInfo:nil repeats:YES];
...
- (void)updateme:(NSTimer *)timer {
if (timer == self.myTimer) {
// Do something because the timers are equal
}
}
I've created a simple button game that gives the user a point with every tap of the button. The button randomly appears on screen every 1.5 seconds. I want the game to end after 30 seconds or after 20 random button pop ups. I've been using the code below to have the button randomly pop-up on the screen:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
I've declared the timer in the header file:
NSTimer *timer;
#property (nonatomic, retain) NSTimer *timer;
I've read Apple Docs on Using Timers but fail to fully understand it. I thought maybe I could use:
- (void)countedTimerFireMethod:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;
But it does not work properly. What am I doing wrong? I'm new to objective-C so I'm not that familiar with how things work.
The problem is on your timer method you are passing moveButton method but in below method where you are stopping the timer that method name is different so try this:-
self.timer = [NSTimer
scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES];
//just change the method name below
- (void)moveButton:(NSTimer *)timer{
count ++;
if(count > 20){
[self.timer invalidate];
self.timer = nil;}
If you are using new version of Xcode then you don not need to declare
NSTimer *timer;
and when scheduling a timer you can use
self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
instead of
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target:self
selector:#selector(moveButton:)
userInfo:nil
repeats:YES]
You are using correct method to stop the timer i.e invalidate
You can also refer the link for more clarification.
Please let me know if you solve this problem through the above code.
I have a void function which just have NSLog(#"Call me"); in its body.
I call it in my view in every ten seconds by using
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(yourMethod) userInfo:nil repeats:YES];
But I want it to stop it after 5 iterations. However it goes to infinity. How can I do that?
1) Keep a global variable, that increments from 0 to 5.
int i = 0;
2) Incement this variable inside your timer function..
-(void) yourFunction:(NSTimer*)timer{
//do your action
i++;
if(i == 5){
[timer invalidate];
}
}
3) When creating timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:#selector(yourMethod:) // <== see the ':', indicates your function takes an argument
userInfo:nil
repeats:YES];
You should take one counter, increment it every time when your method get called, count for 5 and then invalidate your timer using below code.
[timer invalidate];
To destroy the timer from the current loop, you should call [timer invalidate];
To determine five occurrences, you need to maintain a variable and increment its count each time. If it is equal to 5, call invalidate method.
First of all you need to declare an int and declare your NSTimer *timer, so we can stop it:
#interface AppDelegate : UIViewController {
int myInt;
NSTimer *timer;
}
To start the NSTimer you'll need to change just a bit of the code:
timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(yourMethod) userInfo:nil repeats:YES];
Inside your void function you can make the verification to check if the code's running after 5 iterations:
- (void)myVoid{
NSLog(#"Call Me");
if (myInt == 5) {
[timer invalidate];
timer = nil;
}
myInt++;
}