3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AVAudioSession.requestRecordPermission(_:)がDeprecatedなので対応しておく

Posted at

はじめに

マイクへのアクセス許可を求める処理として、 AVAudioSession.requestRecordPermission(_:) を利用していたのですが、ドキュメントを読むと DeprecatedとなっておりiOS17までの利用となっているので対応しておくことにしました。

Deprecated
Use requestRecordPermission(completionHandler:) on AVAudioApplication instead.

と記載してありますが、iOS17+Beta対象なので少し対応しづらいところがあります。
また個人的にはWebRTCを利用しているところもあり、 AVAudioSession ではなく RTCAudioSession を利用したいのですが、 RTCAudioSession には requestRecordPermission(_:) のようなAPIが用意されてなさそうだったので、別方法での対応を取ることにしました。

これまでのAVAudioSession.requestRecordPermission(_:) での対応

AVAudioSession.requestRecordPermission(_:) を利用していたコードはシンプルで、音声周りの管理クラスに以下のようなメソッドを用意して利用していました。

private let session = AVAudioSession.sharedInstance()

func requestRecordPermission(completion: @escaping (Bool) -> Void) {
    session.requestRecordPermission { granted in
        completion(granted)
    }
}

一応 AVAudioApplication.requestRecordPermission(completionHandler:) への置き換えも

AVAudioApplication.requestRecordPermission(completionHandler:)を代用するようにとドキュメントにも記載があったので、一応その対応も載せておきます。

func requestRecordPermission() {
    AVAudioApplication.requestRecordPermission { granted in
        completion(granted)
    }
}

というように、 AVAudioSession.requestRecordPermission(_:) と同じように完了ハンドラで同期的にも書けるし、
class func requestRecordPermission() async -> Bool のような非同期メソッドとしても利用できます。
ドキュメントのまま載せておきます。

// Request permission to record.
if await AVAudioApplication.requestRecordPermission() {
    // The user grants access. Present recording interface.
} else {
    // The user denies access. Present a message that indicates
    // that they can change their permission settings in the
    // Privacy & Security section of the Settings app.
}

AVCaptureDevice.requestAccess(for:completionHandler:) を利用する

はじめに記載しましたが、現状だとiOS17への対応がしづらいのと、個人的にはそもそも AVAudioSession ではなく RTCAudioSession を利用したいことから、 AVCaptureDevice.requestAccess(for:completionHandler:) で代用しました。

こちらはiOS17以降でも問題ない予定ですし、completionHandlerで同期的に、 class func requestAccess(for mediaType: AVMediaType) async -> Bool のような非同期的にも利用できます。
以下のように等価で代用できました。

func requestRecordPermission(completion: @escaping (Bool) -> Void) {
    AVCaptureDevice.requestAccess(for: .audio) { granted in
        completion(granted)
    }
}

for mediaTypeには AVMediaTypeを指定しますが、 .audioを指定すれば、 AVAudioSession.requestRecordPermission(_:) を呼び出すことと同じであることも記載されていました。(安心です)

Note
Calling this method with a media type of audio is equivalent to calling the requestRecordPermission(_:) method on AVAudioSession.

念の為、実際の動作的にも、マイクへのアクセス許可をしている場合は、システムがアラートを出さないように制御されることを確認できました。

終わりに

現状だと、同じようにDeprecatedに対応したいとなれば、ミニマムに修正できる AVCaptureDevice.requestAccess(for:completionHandler:) を利用する形で良いのではと思います。
また言うまでもないと思いますが、もし新規にマイクを使用する機能を入れる場合は、 Info.plistNSMicrophoneUsageDescription キーとそれに対応する文字列を記述するのを忘れないようにしてください。アプリがデバイスのマイクにアクセスしようとした際にクラッシュしてしまうので、、

参考文献

この記事は、以下の情報を参考に書きました。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?