Performing vibration in a recording application

I'm trying to vibrate in an application like Snapchat, which uses both audio output and input, and also supports mixing sound from other applications, but this seems to be more complicated than I originally thought. It is important to know that I am not trying to vibrate during playback or recording. From all the documentation I could find on this, I realized:

  • To support playback and recording (output and input), I need to use AVAudioSessionCategoryPlayAndRecord
  • Phone vibration reduction through AudioServicesPlaySystemSound (kSystemSoundID_Vibrate) not supported in any of the recording categories, including AVAudioSessionCategoryPlayAndRecord .
  • Enabling other audio playback applications can be accomplished by adding the AVAudioSessionCategoryOptionMixWithOthers option.

Therefore, I do this in my application deletion:

 NSError *error = nil; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers error:&error]; 

Possible solutions to perform vibration that I tried but failed:

  • Deactivate the general AVAudioSession before vibration, and then activate it immediately after.

     [[AVAudioSession sharedInstance] setActive:NO error:nil]; AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); [[AVAudioSession sharedInstance] setActive:YES error:nil]; 

    This successfully vibrates, but then when I try to record a movie, the sound dodges (or something else causes a very quiet sound). It also gives me a message stating that I am not allowed to deactivate a session without first removing its I / O devices.

  • Change the category before vibration, and then change it.

     [[AVAudioSession sharedInstance] setActive:NO error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); [[AVAudioSession sharedInstance] setActive:NO error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; 

    This solution comes up from time to time, but doesn't seem to work for me. No vibration occurs even if the categories seem to be set. This might be the right decision if I set usesApplicationAudioSession = YES to my AVCaptureSession, but I haven't done it yet.

Sources:

+5
source share
2 answers

You did not say what requirements the device supports, but for newer devices you can use the new taptic APIs, for example:

UIImpactFeedbackGenerator

UISelectionFeedbackGenerator

UINotificationFeedbackGenerator

https://developer.apple.com/reference/uikit/uifeedbackgenerator#2555399

+1
source

So, I recently tried to play a simple beep in my application, and one way to do this I came across:

 AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID); 

This is a function that plays system sound from /System/Library/Audio/UISounds/ , which is supported in all versions of iOS.

Sound 1350 is equal to RingerVibeChanged (vibration). So..

 AudioServicesPlaySystemSound(1350); 

... vibrates for about 0.5 seconds.

If you are interested in more sounds, here is a link to all playable sounds and iOS versions into which they are added: http://iphonedevwiki.net/index.php/AudioServices

+1
source

All Articles