---
name: "addMusicEventHandler"
type: command
status: ok
groups: ["Event Handlers", "Sounds"]
arma3_since: "0.50"
locality: {"argument": null, "effect": "local", "server_exec": false}
source: https://community.bistudio.com/wiki/addMusicEventHandler
revid: 370195
updated: 2024-11-06T12:01:24Z
---

# addMusicEventHandler

*Command · Event Handlers, Sounds · Arma 3 v0.50*

Locality: effect **local**

## Description

Adds [playMusic](../commands/playMusic.md) event handler. Returns id of the handler or -1 when failed. Like [addMissionEventHandler](../commands/addMissionEventHandler.md), the music event handler is attached to the mission.
See [Arma 3: Event Handlers#Music Event Handlers](../topics/Event_Handlers.md#music-event-handlers).

## Syntax

```sqf
addMusicEventHandler [type, function]
```

**Parameters:**

- `type`: [String](../topics/String.md) - "MusicStart" or "MusicStop"
- `function`: [String](../topics/String.md) or [Code](../topics/Code.md) - passed params array is [musicClassname, eventHandlerId] where:

  ```sqf
  params ["_musicClassname", "_eventHandlerId", "_currentPosition", "_totalLength"];
  ```

  - musicClassName: [String](../topics/String.md) - [CfgMusic](../topics/Description.ext.md#music) class name of the music that started
  - eventHandlerId: [Number](../topics/Number.md) - event handler id (same as returned by [addMusicEventHandler](../commands/addMusicEventHandler.md) usage)
  - Arma 3 v2.18 currentPosition: [Number](../topics/Number.md) - current playback position in 0..1 range (see [getMusicPlayedTime](../commands/getMusicPlayedTime.md))
  - Arma 3 v2.18 totalLength: [Number](../topics/Number.md) - track total length in seconds

**Returns:** [Number](../topics/Number.md) - event handler id

## Examples

### Example 1

```sqf
private _ehID = addMusicEventHandler ["MusicStart", { hint str _this }];
```

### Example 2

```sqf
private _ehID = addMusicEventHandler ["MusicStop", { hint str _this }];
```

## See also

[removeMusicEventHandler](../commands/removeMusicEventHandler.md) · [playMusic](../commands/playMusic.md) · [removeAllMusicEventHandlers](../commands/removeAllMusicEventHandlers.md) · [getEventHandlerInfo](../commands/getEventHandlerInfo.md)

## Community notes

**Druid** — 2013-03-04

There are these two types at this time:
- **MusicStart** - It is triggered when the music is started (command [playMusic](../commands/playMusic.md)). In variable _this is stored class name from [CfgMusic](../topics/Description.ext.md#music).
- **MusicStop** - It is triggered when the music is ended (command [playMusic](../commands/playMusic.md)). In variable _this is stored class name from [CfgMusic](../topics/Description.ext.md#music).

<sub>Source: [BI Community Wiki — addMusicEventHandler](https://community.bistudio.com/wiki/addMusicEventHandler) (revision 370195).</sub>
