benjamindean / flutter_vibration

Handle vibration on iOS and Android in Flutter apps
217 stars 67 forks source link

Vibration.cancel() doesn't work on iOS #63

Closed miri-red closed 2 years ago

miri-red commented 2 years ago

I have vibration turned on with a pattern but in some cases I want the vibration to stop earlier, so I use Vibration.cancel(). On android it works great but on iOS it seems to continue to vibrate until the pattern is done.

benjamindean commented 2 years ago

If I remember correctly, there is no method to stop the pattern on iOS. I'll check once again.

baobao2304 commented 2 years ago

hi benjamindean what do you think about AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate) ?

benjamindean commented 2 years ago

@baobao2304 That might work, but not for the pattern unfortunately.

baobao2304 commented 2 years ago

@benjamindean I think this is not bad , you can try that , its worked with me .

bool isCheckStop = false;
int counter = 0;
Timer? timer;

void startVibrationIOS(){
timer = Timer.periodic(Duration(seconds: 1), (timer) {
if (counter < 20) {

  if(isCheckStop) {
    isCheckStop = false;
    counter = 0 ;
    timer.cancel();
  }
  else{
    if(counter % 2 == 0) {
      Vibration.vibrate(duration: 500);
    }
    else {
      Vibration.vibrate(duration: 1000);
    }
  }
} else{

  timer.cancel();
  counter = 0 ;

}
});
}

void stopVibrationIOS(){
    isCheckStop = true;
}

void startVibrate(){
    if(Platform.isAndroid){
        Vibration.vibrate(
        pattern: List.generate(20, (index) => index % 2 == 0 ? 500 : 1000));
    }else{
        startVibrationIOS();
    }
}

void stopVibrate() {
    if(Platform.isAndroid){
        Vibration.cancel();
    }else{
        stopVibrationIOS();
    }
}