表題についてのメモ及び機能有効化を自動設定するためのコードを載せておく。
前提として以下の環境にて動作確認を行った結果とする。
例外や「これだと動かなかった」的なのがあればコメントにて追記していただけると嬉しい。
- Unity : 2019.4.11f1
- Xcode : 12.2
- iOS SDK Version : 10.0
- 何も無いシーンに音源をループ再生する
AudioSource
を一点置き、こちらがバックグラウンド再生されるかを確認 - バックグラウンドの遷移条件としては、「アプリのサスレジ」の他にも「端末のロック/解除」や「AirPodsの接続/切断」なども行い、全てで問題なく動作するのを確認
- 検証端末は
iPhone11 ProMax(iOS 14.2)
- 検証端末は
このgistにアップしているコード三点をUnityProjectに導入するだけで良い。
※XcodePreProcess.cs
はEditor拡張となるので、Editor以下に配置すること
アップした時点ではEnableBackgroundAudioBridge.cs
にて[RuntimeInitializeOnLoadMethod]
属性から呼び出す形で機能を自動的に有効化させているが、任意のタイミングで有効化したい場合はこちらを消せばok.
バックグラウンド再生の有効化に必要な以下の手順をgistにアップしているコードにて自動的に適用しているだけとなる。
- Unityからビルドして生成された
.xcodeproj
を開き、Signing & Capabilities
からBackground Methods
を追加 - 追加した
Background Methods
からAudio, AirPlay, and Picture in Picture
にチェックを付ける - 初期化時のタイミングで以下のコードを実行
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
参考記事としては以下を参考にさせて頂いた点もあるが...現時点ではリンクが切れているっぽいので自分の備忘録 + 必要設定の自動化を踏まえてこちらを書き残させて頂きました。