logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class UnityAudioPlayerService

    Unity-specific audio player service that manages audio playback through AudioSource components. Supports both UI and music audio player types with game settings integration.

    Inheritance
    object
    UnityAudioPlayerService
    Implements
    IAudioPlayerService
    IService
    IFoundationSettings
    Namespace: Serenity.AudioPlayer.Infrastructure.Services
    Assembly: Serenity.UnityAudioPlayer.Infrastructure.dll
    Syntax
    public class UnityAudioPlayerService : MonoBehaviour, IAudioPlayerService, IService, IFoundationSettings

    Constructors

    UnityAudioPlayerService()

    Declaration
    public UnityAudioPlayerService()

    Fields

    Guid

    Declaration
    public string Guid
    Field Value
    Type Description
    string

    GUID for this service used by editor tools and serialization.

    Id

    Declaration
    public string Id
    Field Value
    Type Description
    string

    Unique identifier for this audio player service instance.

    _audioGameSettings

    Declaration
    protected UnityAudioGameSettingsDefinition _audioGameSettings
    Field Value
    Type Description
    UnityAudioGameSettingsDefinition

    Audio game settings definition used for resolving audio clips from game settings.

    _audioPlayerType

    Declaration
    protected AudioPlayerType _audioPlayerType
    Field Value
    Type Description
    AudioPlayerType

    Type of audio player (UI or Music) determining behavior and event subscriptions.

    _audioPlayerUseCases

    Declaration
    protected AudioPlayerUseCases _audioPlayerUseCases
    Field Value
    Type Description
    AudioPlayerUseCases

    Use cases for audio player operations and business logic.

    _audioSources

    Declaration
    protected AudioSource[] _audioSources
    Field Value
    Type Description
    AudioSource[]

    Array of Unity AudioSource components used for audio playback.

    _eventDispatcherService

    Declaration
    protected IEventDispatcherService _eventDispatcherService
    Field Value
    Type Description
    IEventDispatcherService

    Event dispatcher service for subscribing to and handling audio-related events.

    _gameSettingsService

    Declaration
    protected IGameSettingsService _gameSettingsService
    Field Value
    Type Description
    IGameSettingsService

    Game settings service for accessing game configuration.

    _isEnabled

    Declaration
    protected bool _isEnabled
    Field Value
    Type Description
    bool

    Flag indicating whether the audio player service is enabled.

    _logService

    Declaration
    protected ILogService _logService
    Field Value
    Type Description
    ILogService

    Logging service for error reporting and debugging.

    Properties

    AudioGameSettings

    Declaration
    public IGameSettingsDefinition AudioGameSettings { get; set; }
    Property Value
    Type Description
    IGameSettingsDefinition

    IsEnabled

    Declaration
    public bool IsEnabled { get; set; }
    Property Value
    Type Description
    bool

    Indicates if the audio player service is enabled and can play audio.

    LogService

    Declaration
    public ILogService LogService { get; set; }
    Property Value
    Type Description
    ILogService

    Methods

    Enable()

    Declaration
    public void Enable()

    GetAudioPlayerType()

    Retrieves the audio player type (UI or Music) of this service.

    Declaration
    public AudioPlayerType GetAudioPlayerType()
    Returns
    Type Description
    AudioPlayerType

    Current audio player type

    GetAudioSources()

    Retrieves the array of AudioSource components managed by this audio player service.

    Declaration
    public AudioSource[] GetAudioSources()
    Returns
    Type Description
    AudioSource[]

    Array of AudioSource components managed by this audio player service

    GetAudioSourcesPlayingCount()

    Counts the number of audio sources currently playing clips.
    Useful for monitoring audio service load and debugging playback issues.

    Declaration
    public int GetAudioSourcesPlayingCount()
    Returns
    Type Description
    int

    Number of active audio sources with playing clips

    GetAvailableAudioSource()

    Finds the first available (non-playing) audio source from the pool.
    Used for audio clip playback to avoid overlapping sounds on the same source.

    Declaration
    protected AudioSource GetAvailableAudioSource()
    Returns
    Type Description
    AudioSource

    First available AudioSource component, null if all sources are busy

    GetId()

    Retrieves the event dispatcher service used by this audio player.

    Declaration
    public string GetId()
    Returns
    Type Description
    string

    Current event dispatcher service instance

    Initialize(IGameSettingsService, IAudioPlayerSettings, object, IEventDispatcherService, ILogService)

    Initializes the audio player service with dependencies and configuration.

    Declaration
    public void Initialize(IGameSettingsService gameSettingsService, IAudioPlayerSettings settings, object audioSourceList, IEventDispatcherService eventDispatcherService, ILogService logService)
    Parameters
    Type Name Description
    IGameSettingsService gameSettingsService

    Game settings service for configuration lookup

    IAudioPlayerSettings settings

    Audio player settings containing configuration

    object audioSourceList

    GameObject containing AudioSource components

    IEventDispatcherService eventDispatcherService

    Event dispatcher for audio playback events

    ILogService logService

    Logging service for error reporting

    InitializeEventDispatcher(IEventDispatcherService)

    Initializes event dispatcher integration and subscribes to relevant audio events.

    Declaration
    public void InitializeEventDispatcher(IEventDispatcherService eventDispatcherService)
    Parameters
    Type Name Description
    IEventDispatcherService eventDispatcherService

    Event dispatcher service to integrate with

    PauseAllClips()

    Pauses all currently playing audio clips on all managed audio sources.
    Clips can be resumed later without losing their playback position.

    Declaration
    public void PauseAllClips()

    PlayClip(AudioPlayerPlayClipInput)

    Plays an audio clip using the first available audio source.
    Assigns the clip, applies mixer group settings, and starts playback.

    Declaration
    public AudioPlayerPlayClipOutput PlayClip(AudioPlayerPlayClipInput input)
    Parameters
    Type Name Description
    AudioPlayerPlayClipInput input

    Input DTO containing service ID and audio clip to play

    Returns
    Type Description
    AudioPlayerPlayClipOutput

    Output DTO with playback result details, null if service disabled

    PlayClipAndGetAudioSource(AudioPlayerPlayClipInput)

    Plays an audio clip and returns the Unity AudioSource used for playback.
    Intended for Unity-layer subclasses that need direct AudioSource access for playback lifecycle management (e.g., track ending detection).
    This method keeps AudioSource entirely within the Unity layer.

    Declaration
    protected AudioSource PlayClipAndGetAudioSource(AudioPlayerPlayClipInput input)
    Parameters
    Type Name Description
    AudioPlayerPlayClipInput input

    Input DTO containing service ID and audio clip to play.

    Returns
    Type Description
    AudioSource

    The AudioSource used for playback, or null if playback could not start.

    ResumeAllClips()

    Resumes all previously paused audio clips on all managed audio sources.
    Only affects sources that were paused, not stopped.

    Declaration
    public void ResumeAllClips()

    SetAudioPlayerType(AudioPlayerType)

    Retrieves the event dispatcher service used by this audio player.

    Declaration
    public void SetAudioPlayerType(AudioPlayerType audioType)
    Parameters
    Type Name Description
    AudioPlayerType audioType

    SetAudioSources(AudioSource[])

    Sets the array of AudioSource components managed by this audio player service.

    Declaration
    public void SetAudioSources(AudioSource[] audioSources)
    Parameters
    Type Name Description
    AudioSource[] audioSources

    Array of AudioSource components to manage

    SetEventDispatcherService(IEventDispatcherService)

    Sets the event dispatcher service for this audio player.

    Declaration
    public void SetEventDispatcherService(IEventDispatcherService eventDispatcherService)
    Parameters
    Type Name Description
    IEventDispatcherService eventDispatcherService

    Event dispatcher service to set

    StopAllClips()

    Stops all currently playing audio clips on all managed audio sources.
    Clips will restart from the beginning if played again.

    Declaration
    public void StopAllClips()

    SubscribeToEventDispatcherEvents()

    Subscribes to event dispatcher events based on audio player type.
    UI audio players listen for both direct clip events and game settings events.

    Declaration
    public virtual void SubscribeToEventDispatcherEvents()

    ValidateAudioGameSettings(string, UnityAudioGameSettingsDefinition)

    Validates that audio game settings are properly configured with valid clip definition GUIDs.

    Declaration
    public virtual void ValidateAudioGameSettings(string id, UnityAudioGameSettingsDefinition audioGameSettings)
    Parameters
    Type Name Description
    string id

    Service identifier for error reporting

    UnityAudioGameSettingsDefinition audioGameSettings

    Audio game settings to validate

    Exceptions
    Type Condition
    Exception

    Thrown when settings are invalid or clip definitions not found

    Implements

    IAudioPlayerService
    IService
    IFoundationSettings
    In this article
    © 2026 Serenity. All Rights Reserved