> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-es/scenes-sdk7/media/audio-streaming.md).

# Streaming de audio

Reproduce streams de audio en vivo en tu escena.

Puedes transmitir audio desde una URL. Esto es útil para reproducir música directamente desde una radio por Internet, o transmitir una conferencia a tu escena.

{% hint style="info" %}
**💡 Consejo**: En el [Scene Editor en Creator Hub](/creator/content-creator-es/scene-editor/primeros-pasos/about-editor.md), puedes usar un **Stream de audio** [Smart Item](/creator/content-creator-es/scene-editor/interactividad/smart-items.md) para lograr esto sin código.
{% endhint %}

El audio de la fuente debe estar en uno de los siguientes formatos: `.mp3`, `ogg`, o `aac`. La fuente también debe ser un *https* URL (*http* las URLs no son compatibles), y la fuente debe tener [políticas CORS (Cross Origin Resource Sharing)](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) que permitan acceder a ella externamente. Si no es el caso, quizá necesites configurar un server para actuar como proxy y exponer el stream de una forma válida.

{% hint style="warning" %}
**📔 Nota**: Para reproducir en su lugar un sonido pregrabado en tu escena, consulta [Sonidos](/creator/content-creator-es/scenes-sdk7/fundamentos-del-contenido-3d/sounds.md).
{% endhint %}

Para añadir un stream de audio a tu escena, simplemente añade un `AudioStream` component a un Entity:

```ts
const streamEntity = engine.addEntity()

AudioStream.create(streamEntity, {
	url: 'https://icecast.ravepartyradio.org/ravepartyradio-192.mp3',
	playing: true,
	volume: 0.8,
})
```

{% hint style="warning" %}
**📔 Nota**: El sonido transmitido no es posicional, se escuchará con un volumen constante en toda tu escena. Si un jugador sale de la escena, no escuchará el stream en absoluto.
{% endhint %}

Establece el volumen del `AudioStream` component cambiando su `volume` propiedad.

Activa o desactiva el `AudioStream` component estableciendo su `playing` propiedad en *true* o *false*.

{% hint style="info" %}
**📔 Nota**: No todos los servicios de streaming te permiten reproducir su audio fuera de su sitio. Los siguientes son algunos ejemplos que funcionan en Decentraland:

```ts
DELTA = "https://cdn.instream.audio/:9069/stream?_=171cd6c2b6e"
GRAFFITI = "https://n07.radiojar.com/2qm1fc5kb.m4a?1617129761=&rj-tok=AAABeIR7VqwAilDFeUM39SDjmw&rj-ttl=5"
ISLA NEGRA = "https://radioislanegra.org/listen/up/basic.aac"
```

{% endhint %}

## Estado del stream

Puedes monitorear el estado de un audio stream usando cualquiera de `audioEventsSystem` (basado en callback, recomendado) o consultando con `AudioStream.getAudioState()`.

### Usando audioEventsSystem (recomendado)

Registra un callback que se dispara solo cuando cambia el estado del stream. Este es el mismo sistema usado para `AudioSource` entities (consulta [Detectar cuando un sonido termina](/creator/content-creator-es/scenes-sdk7/fundamentos-del-contenido-3d/sounds.md#detect-when-a-sound-finishes)).

```ts
import { engine, AudioStream, MediaState } from '@dcl/sdk/ecs'
import { audioEventsSystem } from '@dcl/sdk/ecs'

export function main() {
	const entity = engine.addEntity()

	AudioStream.create(entity, {
		playing: true,
		volume: 1,
		url: 'https://audio-edge-es6pf.mia.g.radiomast.io/ref-128k-mp3-stereo',
	})

	audioEventsSystem.registerAudioEventsEntity(entity, (event) => {
		console.log('Stream state: ', event.state)

		if (event.state === MediaState.MS_ERROR) {
			// Intentar reconectar
		}
	})
}
```

### Consultando con getAudioState

También puedes consultar el estado de un stream en cada frame usando `AudioStream.getAudioState()`. Esto devuelve un `PBAudioEvent` object (o `undefined` si aún no se ha reportado ningún estado), con un `state` field y un `marca de tiempo` field. El `state` field es un valor de `MediaState` enum:

* `MS_NONE`
* `MS_LOADING`
* `MS_READY`
* `MS_PLAYING`
* `MS_PAUSED`
* `MS_BUFFERING`
* `MS_SEEKING`
* `MS_ERROR`

```ts
export function main() {
	const entity = engine.addEntity()

	AudioStream.create(entity, {
		playing: true,
		volume: 1,
		url: 'https://audio-edge-es6pf.mia.g.radiomast.io/ref-128k-mp3-stereo',
	})

	let lastState: MediaState | undefined = undefined
	engine.addSystem(() => {
		const currentState = AudioStream.getAudioState(entity)?.state
		if (lastState !== currentState) {
			console.log('Stream state: ', currentState)
			lastState = currentState

			if (currentState == MediaState.MS_ERROR) {
				// Intentar reconectar
			}
		}
	})
}
```

## Audio espacial

Por defecto, el audio de un `AudioStream` componente es global, lo que significa que se escuchará con un volumen constante en toda tu escena. Si un jugador sale de la escena, no escuchará el streaming en absoluto.

Para hacer que el audio sea espacial, establece el `spatial` propiedad en *true*.

```ts
AudioStream.create(entity, {
	url: 'https://radioislanegra.org/listen/up/stream',
    playing: true,
	spatial: true,
})
```

El audio ahora se escuchará desde la posición de la Entity que posee el `AudioStream` componente, y sonará más fuerte a medida que el jugador se acerque.

Controla el audio espacial con las siguientes propiedades:

* `spatialMinDistance`: La distancia mínima a la que el audio se vuelve espacial. Si el jugador está más cerca, el audio se escuchará al volumen máximo. *0* por defecto.
* `spatialMaxDistance`: La distancia máxima a la que se escucha el audio. Si el jugador está más lejos, el audio se escuchará a volumen 0. *60* por defecto

```ts
const audioStreamEntity = engine.addEntity();

Transform.create(audioStreamEntity, {
    position: Vector3.create(8, 0, 8),
});

AudioStream.create(audioStreamEntity, {
    url: 'https://radioislanegra.org/listen/up/stream',
    playing: true,
    volume: 1.0,
    spatial: true,
    spatialMinDistance: 5,
    spatialMaxDistance: 10
});
```

{% hint style="warning" %}
**📔 Nota**: Algunos formatos de audio no soportan audio espacial. Asegúrate de que el audio del stream esté codificado en *mp3*, *AAC-LC* o *FLAC*.
{% endhint %}

## Análisis de audio

Puedes leer datos en tiempo real de amplitud y frecuencia desde un `AudioStream` Entity para impulsar visuales reactivos como ecualizadores o luces que laten al ritmo del beat. Consulta [Análisis de audio](/creator/content-creator-es/scenes-sdk7/media/audio-analysis.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-es/scenes-sdk7/media/audio-streaming.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
