# AudioWarper Component

### AudioWarper

#### Overview

The **AudioWarper** component allows you to associate multiple audio clips with different languages directly in the inspector. Unlike the **LanguageAudioManager**, this component manages its own audio clips and swaps them automatically based on the selected language, without depending on any external audio manager.

This is useful for situations where audio needs to be localized on a per-object basis, and you want each object to handle its audio internally.

#### Key Features

* **Localized Audio**: Each object can have its own localized audio clips based on language.
* **Automatic Switching**: The audio clips automatically change based on the currently selected language.
* **Flexible Setup**: Can be configured to play the audio either on an `AudioSource` component or using `PlayClipAtPoint` for 3D sound positioning.
* **Easy Integration**: Integrates seamlessly with the existing **LanguageManager** to track language changes in real-time.

#### How It Works

The **AudioWarper** component stores a list of `LocalizedAudioClip` objects, each of which contains a `languageID` and an associated `AudioClip`. When the language is changed in the game, the component automatically updates the audio clip being used.

**Setup Instructions**

1. **Add the Component**: Add the **AudioWarper** component to any GameObject that requires language-specific audio.
2. **Configure Localized Audio Clips**: In the **Inspector**, configure the `audioClips` list to include the `languageID` (e.g., "en", "pt-BR") and the associated audio clip.
3. **Optional - Auto Update AudioSource**: If you want the component to automatically update the `AudioSource` on the object, enable the `Change AudioSource Clip based on Language` option and assign an `AudioSource`.
4. **Play Audio**: Use the `PlayLocalizedAudio()` method to play the audio in the correct language.

#### AudioWarper Properties

**audioClips**

* A list of localized audio clips. Each entry includes a `languageID` and an `AudioClip`.

**changeAudioSourceClip**

* If enabled, the `AudioSource` on the GameObject will be automatically updated with the correct audio clip based on the current language.

**audioSource**

* The `AudioSource` component to update with the localized audio clip. If left empty, the component will try to get the `AudioSource` automatically from the GameObject.

#### API Reference

**`UpdateAudioClip()`**

Updates the audio clip of the associated `AudioSource` based on the current language.

```csharp
audioWarper.UpdateAudioClip();
```

**`PlayLocalizedAudio()`**

Plays the localized audio clip for the current language. If an `AudioSource` is assigned, it plays through the `AudioSource`. Otherwise, it uses `AudioSource.PlayClipAtPoint()` to play the audio at the GameObject's position.

```csharp
audioWarper.PlayLocalizedAudio();
```

**`GetAudioClipForLanguage(string languageID)`**

Retrieves the `AudioClip` associated with a specific language ID.

```csharp
AudioClip clip = audioWarper.GetAudioClipForLanguage("en");
```

**`StopAudio()`**

Stops any currently playing audio on the assigned `AudioSource`.

```csharp
audioWarper.StopAudio();
```

#### Example: Playing Localized Audio

Here's an example of how you can set up and use the **AudioWarper** component to play localized audio.

1. Add the **AudioWarper** component to a GameObject.
2. Add different audio clips for each language to the `audioClips` list.
3. Ensure the `AudioSource` is assigned or use the default settings.
4. Call `PlayLocalizedAudio()` when you want to play the audio in the correct language.

```csharp
csharpCopiar códigopublic class AudioPlayer : MonoBehaviour
{
    public AudioWarper audioWarper;

    private void Start()
    {
        audioWarper.PlayLocalizedAudio();
    }
}
```

#### Use Case

The **AudioWarper** component is ideal for use cases where you want to manage localized audio clips directly on a GameObject without relying on a global audio manager. It gives you more granular control over individual audio sources, allowing each GameObject to manage its own language-specific audio assets.

<figure><img src="https://3626215024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FF9jo80etwnAiJ4U7LlGC%2Fuploads%2FBoSceBkGiLWkBMknmHDw%2FAudioLocalized.png?alt=media&#x26;token=a44d835c-84c7-4487-bd26-fa60d8630129" alt=""><figcaption></figcaption></figure>

