Daniel Larsson
Daniel Larsson

Reputation: 6394

Performing vibration in recording app

I am trying to perform a vibration in an app similar to Snapchat, that uses both audio output and input as well as supports audio mixing from other apps, but this seems to be a harder task that I initially thought it would be. Important to know is that I am not trying to vibrate during playback or recording. From reading all the documentation I could find on the subject, this is what I have come to understand:

Therefore, I do this in my app delegate:

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

The possible solutions to doing the vibration that I have tried but failed at are:

Sources:

Upvotes: 12

Views: 1778

Answers (4)

leo
leo

Reputation: 360

You may try setting the allowHapticsAndSystemSoundsDuringRecording flag on AVAudioSession to true (the flag defaults to false) by calling

sessionInstance.setAllowHapticsAndSystemSoundsDuringRecording(true)

It worked for me.

Note the system prevents haptic and sound feedback (such as those from a UIPickerView or UIDatePicker) from playing, to avoid unexpected system noise while recording.

Upvotes: 0

jayOrange
jayOrange

Reputation: 163

For reference, I'm not using AVCaptureSession but an AVAudioEngine to record something, and for my case I pinpointed it down to having to pause the input in order to play the vibration properly with AudioServicesPlaySystemSound during a record session.

So in my case, I did something like this

audioEngine?.pause()
AudioServicesPlaySystemSound(1519)
do {
  try audioEngine.start()
} catch let error {
  print("An error occurred starting audio engine. \(error.localizedDescription)")
}

I'm not sure if it would also work with doing something similar for AVCaptureSession (i.e., stopRunning() and startRunning()), but leaving this here in case someone wants to give it a try.

Upvotes: 1

Adam Svestka
Adam Svestka

Reputation: 112

So, I've been recently trying to play a simple beep in my app and one of the methods of doing so I stumbled upon is:

AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID);

It's a function that plays a system sound from /System/Library/Audio/UISounds/ it's supported on all iOS versions.

The sound 1350 equals to RingerVibeChanged (vibration). So..

AudioServicesPlaySystemSound(1350);

...vibrates for about 0.5 seconds.

Incase you're interested in more sounds here is a link to all the playable sounds and what iOS versions they've been added in: http://iphonedevwiki.net/index.php/AudioServices

Upvotes: 3

atomkirk
atomkirk

Reputation: 3801

You didn't say what your supported device requirements are, but for newer devices, you can use the new taptic engine APIs, such as:

UIImpactFeedbackGenerator

UISelectionFeedbackGenerator

UINotificationFeedbackGenerator

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

Upvotes: 1

Related Questions