when iPhone has a incoming call , its vibration duration seems longer than invoke the method below:
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
how could i make the vibration duration as long as the iPhone incoming call .
I make a VoIP app, I want the single time vibration duration not too short...
There's no method to achieve a longer vibration. But you could repeat the vibration permanently or with a timer and a little space between the vibrations.
Example of a timer based solution:
//start the vibration
NSTimer * vibrationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(vibrate) userInfo:nil repeats:YES];
//stop the vibration
[vibrationTimer invalidate];
-(void)vibrate{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
Related
I have implemented APNS for push notification. I am using custom sound for alert notification that will play 30 seconds siren audio file. that is working. while playing that sound I get only one time of vibration with the duration of 2 secs. I need the vibration upto 30 seconds . how can I achieve that.
this is the payload I am using.
{"aps":{"alert":"alert message","badge":1,"sound":"SIREN.caf"}}
I have tried implementing code for vibrating upto 30 seconds. it will be call on
didReceiveRemoteNotification
-(void)vibrateDevice{
AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, ^{
AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);
});
vibrateCount++;
if (vibrateCount<30) {
// [self performSelector:#selector(vibrateDevice) withObject:nil afterDelay:1.0];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(vibrateDevice) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
}
the problem is I can able to vibrate only in app foreground. when app
is killed or in background vibration code part is not working.
- (NSTimer *) timer
{
(!_timer)
{
_timer = [NSTimer timerWithTimeInterval:86400 target:self selector:#selector(timeset:) userInfo:nil repeats:YES];
}
return _timer;
}
You can't schedule execution for a specific time in the background on iOS. You can schedule a UILocalNotification - but your app will only be launched if the user taps on the notification.
Your best bet is to use background fetch mode. You app will be woken at intervals and given an opportunity to fetch new data. You can check to see if the time is at or after 7 and decide whether to refresh the data.
I have a driving application when I start the trip I have to collect location details along with date and time in hours, minutes, seconds and milliseconds for every second and accelerometer details along with date and time in hours, minutes, seconds and milliseconds for every 0.25 seconds and when the trip is stopped recording should be stopped. I have taken a timer for location details with interval 1 second
self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(locationTimerFired:)
userInfo:nil
repeats:YES];
- (void) locationTimerFired:(NSTimer *)timer
{
CLLocation *newLocation = locationManager.location;
[self updateLocation:newLocation andSpeed:newLocation.speed];
[defaultCenter postNotificationName:LocationChangedNotification
object:nil
userInfo: userInfo];
}
in the received LocationChangedNotification I am recording the location and time as mentioned for every second.
This is sometime fine when the application is in foreground but when the application is in background timer timer fires interval is not accurate it fires sometimes for 2 seconds, for 3 seconds and some times 2 times a second why this is happening?
Please suggest. Also I have registered for location updates in background mode in info.plist.
I have taken a timer for accelerometer details with interval 0.25 second as follows
self.accelTimer = [NSTimer scheduledTimerWithTimeInterval:0.25
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
Thanks.
Don't use timers. Instead, configure the location manager to give you callbacks at your desired accuracy (presumably maximum) and use the callback to save the location and speed with the current time.
Then, when you come to use / display the data you can filter / extrapolate to get the location data at your desired time interval.
The timers don't work in the background because they aren't designed to. Location callbacks are designed to...
As the title states, i have a while loop that will be executed until certain condition is met, or until 5 seconds have passed.
What is the best way to solve this? I have seen some simple tutorial about NSTimer, but it seems to me that selector that is fired within NSTimer will be executed after time interval specified no matter what. I only need to execute it if condition is not met...
Just create an NSTimer scheduled action store the timer and if you reach your what you wanted to achieve deactivate this timer so that it doesn't trigger the action.
Basically:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(yourAction) userInfo:nil repeats:NO];
some code
//for deactivating the timer
[timer invalidate];
timer = nil;
You could start the NSTimer on the main thread (to ensure above code works) with this:
[self performSelectorOnMainThread:#selector(startTimerMethod) withObject:someOrNoObject waitUntilDone:NO];
I want to play any file for 6 seconds. Also suppose the audio is bigger then 6 sec the application will play only for 6 sec.and if it is less then 6 sec then play continuously. So is there any inbuilt option from any framework?
A Simple Way To Play MP3 Audio Files
Assuming you use an AVAudioPlayer, send a -stop message after 6 seconds:
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:6
target:thePlayer selector:#selector(stop)
userInfo:nil repeats:NO];
(Remember to invalidate the timer if the player stops early.)
If you are using AVAudioPlayer you can make use of an NSTimer as mentioned by #Kenny. Just to add to add to that answer.
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:6
target:thePlayer selector:#selector(stopPlayer)
userInfo:nil repeats:NO];
- (void)stopPlayer
{
[audioPlayer stop];
}
In case the duration of your audio file is less than 6 seconds or your desired limit you should continue to play right? so you must set the numberOfLoops for the audio player instance to continue playback. Here is the reference