# One background music during a loop

**URL:** <https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803>\
**Category:** Builder\
**Created:** [November 13, 2018, 2:08am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803 "2018-11-13T02:08:54Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 13, 2018, 4:05am UTC](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803/2 "2018-11-13T04:05:00Z")

</div>

You need to use some tiny code snippets here to do this, as all Builder stimuli are designed to start and stop within a routine, while you want one that will span multiple iterations over a routine.

Insert a code component on the routine where the sound will play. In the _ **"Begin experiment"** _ tab, insert code something like this to create the sound object in advance:

```
background_sound = sound.Sound('your_sound_file.wav')

```

In the _ **"Begin routine"** _ tab, put something like this, so that the sound starts playing, but only on the first iteration:

```auto
if your_loop_name.thisN == 0:
    background_sound.play()

```

I guess you might also need to stop it after the 90th trial, so put something like this in the _ **"End routine"** _ tab:

```auto
if your_loop_name.thisN == 89:
    background_sound.stop()

```

---

_[View the full topic](https://discourse.psychopy.org/t/one-background-music-during-a-loop/5803)._
