Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Last active October 1, 2021 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/a3bf5019107375ed4bce9df8853ef62b to your computer and use it in GitHub Desktop.
Save mao-test-h/a3bf5019107375ed4bce9df8853ef62b to your computer and use it in GitHub Desktop.
Unity iOSビルドにてオーディオのバックグラウンド再生を行なう

Unity iOSビルドにてオーディオのバックグラウンド再生を行なう

表題についてのメモ及び機能有効化を自動設定するためのコードを載せておく。

前提として以下の環境にて動作確認を行った結果とする。
例外や「これだと動かなかった」的なのがあればコメントにて追記していただけると嬉しい。

  • 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にアップしているコードにて自動的に適用しているだけとなる。

  1. Unityからビルドして生成された.xcodeprojを開き、Signing & CapabilitiesからBackground Methodsを追加
  2. 追加したBackground MethodsからAudio, AirPlay, and Picture in Pictureにチェックを付ける
  3. 初期化時のタイミングで以下のコードを実行
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

参考記事としては以下を参考にさせて頂いた点もあるが...現時点ではリンクが切れているっぽいので自分の備忘録 + 必要設定の自動化を踏まえてこちらを書き残させて頂きました。

#import <AVFoundation/AVFoundation.h>
@interface EnableBackgroundAudioWrapper : NSObject
+ (void)enableBackgroundAudio;
@end
@implementation EnableBackgroundAudioWrapper
+ (void)enableBackgroundAudio {
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
}
@end
#ifdef __cplusplus
extern "C" {
#endif
void __enableBackgroundAudio() {
return [EnableBackgroundAudioWrapper enableBackgroundAudio];
}
#ifdef __cplusplus
}
#endif
using System.Runtime.InteropServices;
using UnityEngine;
namespace iOSNative
{
public static class EnableBackgroundAudioBridge
{
[DllImport("__Internal")]
static extern void __enableBackgroundAudio();
/// <summary>
/// バックグラウンドでのオーディオ再生を有効化
/// </summary>
[System.Diagnostics.Conditional("UNITY_IOS")]
public static void EnableBackgroundAudio()
{
__enableBackgroundAudio();
}
// NOTE: 自動有効化が不要なら切ること
[RuntimeInitializeOnLoadMethod]
static void AutoEnabling()
{
EnableBackgroundAudio();
}
}
}
#if UNITY_IOS
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
namespace iOSNative.Editor
{
sealed class XcodePreProcess : IPreprocessBuildWithReport
{
public void OnPreprocessBuild(BuildReport report)
{
PlayerSettings.iOS.appInBackgroundBehavior = iOSAppInBackgroundBehavior.Custom;
PlayerSettings.iOS.backgroundModes = iOSBackgroundMode.Audio;
}
public int callbackOrder { get; }
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment