# Broadcast

### Creating a broadcast

You can easily create a broadcast by using the broadcast class. The Broacast class requires the `stream` parameter and has an optional `options` parameter. The `options` parameter has the following options:

* noListeners: If there are no members listening in the voice channel.
  * play: Continues playing the stream. **(default and recommended)**
  * pause: Pauses the stream until someone joins again.
  * stop: Stops the player and leaves the voice channel.
* volume: The volume of the stream. Max 1, min 0.
* audiotype: The audio type of the stream.
  * 'arbitrary': For mp3 audio **(default)**
  * 'ogg/opus': For ogg audio
  * 'webm/opus': For webm audio
  * 'opus': For opus audio
  * 'raw': For raw audio

```javascript
const discordaudio = require('discordaudio');

const broadcast = new discordaudio.Broadcast(`https://somecoolsite.com/somecoolstream.mp3`, {
    noListeners: 'play',
    volume: 1,
    audiotype: 'arbitrary'
});
```

### Pausing a broadcast

You can easily pause a broadcast by using the pause function.

```javascript
...
broadcast.pause();
```

### Resuming a broadcast

You can easily resume a broadcast by using the resume function.

```javascript
...
broadcast.resume();
```

### Destroy a broadcast

You can destroy a broadcast by using the destroy function. You should first unsubscribe the broadcast of all connections before using this function.

```javascript
...
broadcast.destroy();
```

### Changing the broadcast volume

You can change the volume of the broadcast by using the volume function. The volume has to be at least 0 and can maximum be 1.

```javascript
...
broadcast.volume(1);
```

### Event listeners

The Broadcast class also has 2 events:

* play: When the broadcast starts playing.
* end: When the broadcast ended.

Both events return the stream that is playing or has ended.

```javascript
...
broadcast.on('play', stream => console.log(`Started playing ${stream}`));

broadcast.on('end', stream => console.log(`The stream ${stream} ended`));
```

{% hint style="warning" %}
**Recommended** is that if you want to play another stream you first destroy the broadcast and then play a new stream. Otherwise the old resource still will be available and can cause memory leaks.
{% endhint %}

### Playing a broadcast

You can play a broadcast by using the [Connection](https://zyno-studio.gitbook.io/discord-audio/classes/broken-reference) class. The Connection class also has [some events](https://zyno-studio.gitbook.io/discord-audio/classes/broken-reference) which are being used for the Broadcast.

```javascript
const {Connection, Broadcast} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS, discord.Intents.FLAGS.GUILD_MESSAGES, discord.Intents.FLAGS.GUILD_VOICE_STATES]});

const config = {
    token: 'Your-Secret-Token',
    prefix: '-'
};

const broadcast = new Broadcast(`https://somecoolsite.com/somecoolstream.mp3`, {
    noListeners: 'play',
    volume: 1,
    audiotype: 'arbitrary'
});

const connections = new Map();

client.once('ready', () => console.log(`${client.user.username} is online`);

client.on('messageCreate', message => {
    if(message.author.bot || message.channel.type === `DM`) return;
    
    if(!message.content.startsWith(config.prefix)) return;
    
    let args = message.content.substring(config.prefix.length).split(" ");
    
    switch(args[0].toLowerCase()){
        case 'connect':
            if(connections.get(message.guild.id)) return message.channel.send({content: `The bot is already connected to a voice channel!`});
            if(!message.member.voice.channel)) return message.channel.send({content: `Please join a voice channel in order to connect the bot to a voice channel`});
            const connection = new Connection(message.member.voice.channel, {
                selfDeaf: true,
                selfMute: false
            });
            connection.subscribe(broadcast).then(() => {
                message.channel.send(`Connected to ${message.member.voice.channel.name}`)
                connections.set(message.guild.id, connection);
            }).catch(err => {
                console.log(err);
                message.channel.send({content: `There was an error while playing the broadcast!`});
            });
            break;
    }
});

client.login(config.token);
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zyno-studio.gitbook.io/discord-audio/classes/broadcast.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
