簡體   English   中英

在錄音應用程序中執行振動

[英]Performing vibration in recording app

我正在嘗試在類似於 Snapchat 的應用程序中執行振動,它使用音頻 output 和輸入以及支持來自其他應用程序的音頻混合,但這似乎是我最初認為的一項更艱巨的任務。 重要的是要知道我並沒有試圖在播放或錄音過程中振動。 通過閱讀我能找到的有關該主題的所有文檔,這就是我的理解:

  • 為了同時支持播放和錄音(輸出和輸入),我需要使用AVAudioSessionCategoryPlayAndRecord
  • 任何錄音類別都不支持通過AudioServicesPlaySystemSound (kSystemSoundID_Vibrate)使手機振動,包括AVAudioSessionCategoryPlayAndRecord
  • 可以通過添加選項AVAudioSessionCategoryOptionMixWithOthers來啟用其他應用程序播放音頻。

因此,我在我的應用委托中這樣做:

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

我嘗試過但失敗的振動的可能解決方案是:

  • 在振動之前停用共享的AVAudioSession ,然后直接激活它。

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

    這成功地執行了振動,但是之后,當我嘗試錄制電影時,音頻被隱藏了(或者其他原因導致它非常安靜)。 它還給我一個錯誤,說我不允許在不先刪除其 I/O 設備的情況下停用 session。

  • 在振動之前更改類別,然后再將其更改回來。

     [[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];

    這個解決方案時不時出現,但似乎對我不起作用。 沒有振動發生,即使類別似乎已設置。 如果我在我的 AVCaptureSession 上設置usesApplicationAudioSession = YES ,這可能仍然是一個有效的解決方案,但我還沒有讓它工作。

資料來源:

您沒有說支持的設備要求是什么,但是對於較新的設備,可以使用新的Taptic Engine API,例如:

UIImpactFeedbackGenerator

UISelectionFeedbackGenerator

UINotificationFeedbackGenerator

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

因此,我最近一直在嘗試在應用中播放一個簡單的提示音,而我偶然發現的一種方法是:

AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID);

此功能可播放/System/Library/Audio/UISounds/中的系統聲音,並且所有iOS版本均支持該功能。

聲音1350等於RingerVibeChanged (振動)。 所以..

AudioServicesPlaySystemSound(1350);

...振動約0.5秒。

如果您對更多聲音感興趣,請單擊此處,以鏈接到所有可播放的聲音以及在其中添加了哪些iOS版本: http : //iphonedevwiki.net/index.php/AudioServices

作為參考,我不是使用AVCaptureSession而是使用AVAudioEngine來記錄某些內容,就我而言,我將其精確定位為必須暫停輸入,以便在記錄會話期間使用AudioServicesPlaySystemSound正確播放振動。

所以就我而言,我做了這樣的事情

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

我不確定是否也可以對AVCaptureSession進行類似的操作( stopRunning()startRunning() ),但是如果有人想嘗試一下,可以將其保留在這里。

您可以嘗試通過調用將AVAudioSession上的allowHapticsAndSystemSoundsDuringRecording標志設置為true (該標志默認false

sessionInstance.setAllowHapticsAndSystemSoundsDuringRecording(true)

它對我有用。

請注意,系統會阻止播放觸覺和聲音反饋(例如來自UIPickerViewUIDatePicker的反饋),以避免在錄制時出現意外的系統噪音。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM