Astra Child

SoundPlayer

Estimated reading: 3 minutes 0 views

The SoundPlayer is a versatile audio component designed to handle the playback of various sound effects and audio clips in Unity projects. It offers an easy-to-use interface for managing sounds and provides functionalities like volume control, pitch adjustment, and spatial 3D sound positioning.

Key features and components of the SoundPlayer class:

  1. Initialization: The SoundyService.Initialize() method is called in the Awake() method to ensure that the SoundyService is initialized before using it.
  2. SoundId and Sound Library: The SoundPlayer has a SoundId field, which represents the sound to be played. The SoundId combines the name of the sound library and the name of the sound within that library. The class provides methods to set a new sound by either providing the SoundId or the library name and sound name directly.
  3. PlayOnStart, PlayOnEnable, PlayOnDisable: These boolean properties determine whether the sound should automatically play when the GameObject is started, enabled, or disabled, respectively.
  4. StopOnDisable, StopOnDestroy: These boolean properties determine whether the sound should automatically stop when the GameObject is disabled or destroyed, respectively.
  5. Follow Target: The SoundPlayer allows you to set a transform as the follow target. While playing, the sound will follow the specified transform, which can be useful for creating 3D audio effects.
  6. AudioPlayer, SoundLibrary, SoundObject: The SoundPlayer holds references to the AudioPlayer, SoundLibrary, and SoundObject related to the loaded sound. The AudioPlayer is responsible for actual sound playback.
  7. ResetPlayer(): This method clears the SoundPlayer, resets the SoundId, and sets all references to null. After calling this method, the SoundPlayer won’t be able to play any sound until a new sound is set.
  8. Play(): This method plays the loaded sound object. If the sound is already playing, it restarts it. If the sound object is not loaded or cannot play, nothing happens. If an AudioPlayer is already available (from the object pool), it will be used; otherwise, a new AudioPlayer is obtained from the SoundyService.
  9. Stop(): This method stops the loaded sound object if it is currently playing.

Getting Started

Follow these simple steps to set up and utilize the SoundPlayer in your project:

  1. Attach the SoundPlayer component to a GameObject in your scene.
  2. Customize the properties of the SoundPlayer using the inspector to configure its behavior according to your project’s requirements.
  3. Use the provided methods to play, stop, pause, resume, and control the volume and pitch of your sound effects.

Sound Playback

The SoundPlayer allows you to effortlessly play sound effects and audio clips through simple-to-use methods. You can trigger sounds from user interactions, events, or scripts, providing auditory feedback to enhance the overall user experience.

Code Example

// Create a new SoundPlayer component and set the sound to play
SoundPlayer soundPlayer =
    SoundPlayer
        .Get()
        .SetSound("LibraryName", "SoundName");

// Play the sound
soundPlayer.Play();Code language: C# (cs)

For more detailed information, please refer to the official API documentation available at https://api.doozyui.com.

Share this Doc
CONTENTS
Scroll to Top