Version: 2022.3
言語: 日本語
WebGL graphics
Embedded resources in WebGL

Audio in WebGL

This page only provides information about audio capabilities in WebGL. To learn how to use audio in your Unity project, see the Audio Overview.

Because Unity uses FMOD to manage audio for platforms, Unity WebGL supports limited audio functionality, which only includes the basic features. FMOD relies on threads, which WebGL doesn’t support. For this reason, Unity uses an implementation based on the internal Web Audio API, which enables the browser to handle audio playback and mixing.

Note: Google Chrome’s new Autoplay policy prevents autoplay of audio and video under certain conditions. For example, while your game might be set to autoplay some background music soon after the game loads, it won’t play automatically unless you click or tap on the website. For more information on how to enable or disable this policy, see Google Chrome’s documentation on Autoplay policy in Chrome.

対応可能なクラス

Unity WebGL は以下の API クラスをサポートしています。

クラス WebGL サポート状況
AudioSource WebGL supports some APIs. See AudioSource for specific support details.
AudioListener すべての API に対応しています。
AudioClip WebGL はいくつかの API をサポートしています。具体的なサポート内容については、AudioClip を参照してください。
SystemInfo.supportsAudio ブラウザーは WebGL のオーディオサポートを提供しています。このため、 SystemInfo.supportsAudio は常に true です。
Microphone 不対応。

AudioSource

AudioSource API は、以下のような基本的な位置に関するオーディオ再生をサポートしています。

  • ポーズと再開
  • パンニング
  • ロールオフ
  • ピッチ設定
  • ドップラー効果対応

Unity WebGL は、以下の AudioSource API をサポートします。

設定 説明 
Clip 次に再生するオーディオクリップを決定します。
dopplerLevel AudioSource のドップラースケールを設定します。
ignoreListenerPause AudioSource が、AudioListener.pause を無視して、オーディオの再生を継続できるようにします。
ignoreListenerVolume エンドユーザーの AudioSource の音量を無視します。
isPlaying AudioSource.clip が再生されている場合に true を返します。
loop アプリケーションで AudioSource.clip をループさせることができます。
maxDistance AudioSource.clip の減衰が止まる、または聞き取れなくなる最大距離を設定します。
minDistance AudioSource.clip の音量がこれ以上上がらなくなる最小距離を設定します。最小距離を超えると、音が減衰し始めます。
mute AudioSource をミュートします。
pitch AudioSource.clip のピッチを設定します。WebGL は正のピッチ値のみをサポートしています。
playOnAwake Awakeで AudioSource を再生します。
rolloffMode 距離による AudioSource の減衰量を設定します。
time 再生位置を秒単位で設定します。
timeSamples 再生位置をパルス符号変調 (PCM) サンプルで設定します。
velocityUpdateMode AudioSource の更新を固定更新ループで行うか、動的更新ループで行うかを設定します。
volume オーディオソースの音量を設定します (0.0 - 1.0)。
Pause AudioSource.clip を一時停止します。
Play AudioSource.clip を再生します。
PlayDelayed AudioSource.clip を、秒単位で指定した遅延時間を付けて再生します。
PlayOneShot AudioClip を再生し、AudioSource の音量を volumeScale でスケールします。
PlayScheduled 指定した時間にオーディオソースを再生します。
SetScheduledEndTime スケジュールされた AudioSource.clip が終了する時間を設定します。
SetScheduledStartTime スケジュールされた AudioSource.clip の開始時間を設定します。
Stop AudioSource.clip の再生を停止します。
UnPause 一時停止した AudioSource.clip を解除します。
PlayClipAtPoint AudioSource.clip をワールド空間の指定された位置で再生します。

AudioClip

Unity WebGL imports AudioClip files in the AAC Format, which is supported by most browsers. Unity WebGL supports the following AudioClip APIs:

Properties 説明 
length オーディオの長さ (秒単位)。
loadState AudioClip に関連付けられたオーディオデータの現在の読み込み状況を返します。
samples サンプルの AudioClip の長さ。
loadType クリップのロードタイプです。AudioClip のロードタイプは、Inspector で設定できます。
メソッド 説明  追加情報
AudioClip.Create 指定した名前と長さの AudioClip を作成します。 Unity WebGL partially supports AudioClip.Create. Browsers don’t support dynamic streaming, so to use AudioClip.Create, set the Stream to false.
AudioClip.SetData AudioSource.Clip のサンプルデータを設定します。 Unity WebGL は、 AudioClip.SetData を部分的にサポートしています。このメソッドは、AudioClip のコンテンツを置き換えるためにのみ使用できます。WebGL では、Unity は offsetSample パラメーターを無視します。

Note: For audio clip support on Linux, make sure you’ve installed the ffmpeg package.

圧縮オーディオ

To use compressed audio with WebGL in Unity, set the AudioClip loadType to one of the following options:

Compression method 説明  Considerations
CompressedInMemory Use this to compress the audio on disk and have it remain compressed after it loads into your application memory. Compressed audio can cause latency and is less precise when it comes to audio playback. However, compressed audio uses less memory in your application than decompressed audio. It’s best practise to use CompressedInMemory for audio that’s unaffected by precision for example, background music.
DecompressOnLoad Use this to compress the audio on disk, similar to CompressedInMemory, and decompress when it loads into your application memory. Decompressed audio uses a significant amount of memory compared to compressed audio but has lower latency and more audio flexibility. Use DecompressedOnLoad for audio that’s affected by precision (for example, character dialog or sound effects).

オーディオ再生とブラウザーのセキュリティ

セキュリティ上の理由から、ブラウザーは、エンドユーザーがマウスのクリック、タッチイベント、キーの押下などでアプリケーションのウェブページを操作するまで、オーディオの再生を許可しません。ローディングスクリーンを使用して、エンドユーザーがアプリケーションと相互作用できるようにし、主要コンテンツが始まる前にオーディオ再生を開始します。

WebGL graphics
Embedded resources in WebGL