# I am trying to load in a sound file to my p5 sound project, but they all come out distorted or my screen goes black

**URL:** <https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121>\
**Category:** Libraries\
**Created:** [November 30, 2018, 6:06pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121 "2018-11-30T18:06:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![thesavgplatypus](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@thesavgplatypus](https://discourse.processing.org/u/thesavgplatypus)\
**Post date:** [November 30, 2018, 6:06pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/1 "2018-11-30T18:06:11Z")

</div>

I have tried .ogg, .m4a, .mp3, everything. any suggestions?

---

<div class="post-metadata">

**Author:** ![Pr0tonX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pr0tonx/32/2590_2.png) [@Pr0tonX](https://discourse.processing.org/u/Pr0tonX)\
**Post date:** [November 30, 2018, 8:59pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/2 "2018-11-30T20:59:28Z")

</div>

Give your code, and we will probably can help you…Without anything, we can’t know what’s wrong.

First, did you store yours audio files in a correct folder and give the path to find it in your code ?  
Did you load p5 Sounds librairies ?  
Did you load yours song in a Preload function ?

---

<div class="post-metadata">

**Author:** ![Uncle\_Jonno](https://avatars.discourse-cdn.com/v4/letter/u/c67d28/32.png) [@Uncle\_Jonno](https://discourse.processing.org/u/Uncle_Jonno)\
**Post date:** [December 1, 2018, 1:12am UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/3 "2018-12-01T01:12:15Z")

</div>

Which browser are you using?

Chrome won’t let you access files on your computer, but Firefox will.

---

<div class="post-metadata">

**Author:** ![thesavgplatypus](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@thesavgplatypus](https://discourse.processing.org/u/thesavgplatypus)\
**Post date:** [December 1, 2018, 2:16am UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/4 "2018-12-01T02:16:19Z")

</div>

```auto
var img;
var sound;
var ellipsex = 200
var ellipsey = 330
var swordX = -909999
var swordY = 260
var cooldownx = 0
var obst1X = 160
var obst1Y = 25
var rip = -9999

function preload() {
  soundFormats('m4a','mp3','ogg')
  sound = loadSound('assets/untitled_1087.mp3');
}

function setup() {
  createCanvas(400, 400);
  img = loadImage("assets/genji.png");
  ellipseMode(CENTER)
  rectMode(CENTER)
  imageMode(CENTER)
}

function draw() {
  background(255,51,153);
  obst1Y = obst1Y + 2.5
  textSize(20);
  noStroke()
  fill('pink')
  fill('blue');
  //player
  //ellipse(ellipsex,ellipsey,70,70);
  //genji
  image(img,ellipsex,ellipsey);
  img.resize(200,100)
  //sword
  fill(0)
  rect(swordX,swordY,60,10)
  //cooldown
  text('COOLDOWNS:',0,20)
  fill(0,204,51)
  rect(cooldownx,40,120,10)
  //obstacles
  fill(153, 0, 153)
  rect(obst1X,obst1Y,75,75)
  // endgame rectangle
  rect(rip,0,10000,10000)
  //if ellipse is offscreen
  if (ellipsex < -35) {
    ellipsex = width + 35
  }
  if (ellipsex > width + 35) {
    ellipsex = -35
  }
  //sword appears
  if (mouseIsPressed == true) {
   if (mouseButton == LEFT) {
     if (cooldownx > -55) {
    swordX = ellipsex + 5
       sound.setVolume(0.1)
       sound.play()
       sound.rate(1)
     }
     if (cooldownx < -55) {
       swordX = -9999
     }
     cooldownx = cooldownx -1
   }
  }
  if (mouseIsPressed == false) {
    frameRate(60)
    swordX = -99999
    if (cooldownx < 0) {
      cooldownx = cooldownx + 0.5
    }
  }
  //player movement
  if (key==='a') {
    ellipsex = ellipsex - 2
  }
  if (key==='d') {
    ellipsex = ellipsex + 2
  }
  //if the obstacle hits the player
  if (ellipsex > obst1X - 37.5 && ellipsey + 35 < obst1Y + 37.5 && ellipsex + 35 < obst1X + 37.5 && ellipsey > obst1Y - 37.5) {
    ellipsey = obst1Y
    rip = 0
  }
  // if the sword hits the obstacle
  if (swordY < obst1Y + 25 && swordY > obst1Y - 25 && swordX > obst1X - 25 && swordX < obst1X + 25) {
    obst1Y = -9999
  }
  // if deflect is used
}

```

yes. it’s in prototype though. I have a local server, I have loaded it into the sketch, so it should work.

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [December 1, 2018, 3:16pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/5 "2018-12-01T15:16:09Z")

</div>

You are getting the distortion due to calling `sound.play` inside of draw … it essentially wants to play that sound 30-60 times per second. If you really need to call it inside of draw, wrap it in a conditional checking if the sound is not already playing, if so, play sound.

```auto
if (!sound.isPlaying()) {
  sound.play();
}

```

---

<div class="post-metadata">

**Author:** ![thesavgplatypus](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@thesavgplatypus](https://discourse.processing.org/u/thesavgplatypus)\
**Post date:** [December 14, 2018, 5:54pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/6 "2018-12-14T17:54:20Z")

</div>

thank you! that is solved. however, I want **another sound** to stop looping after it plays once.

```auto
  if (rip > -50) {
    if (defeat.isPlaying() == false) {
      defeat.setVolume(1)
      defeat.play()
      defeat.rate(1)
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 14, 2018, 7:37pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/7 "2018-12-14T19:37:05Z")

</div>

I assume that you want sound Y to stop playing as soon as sound X finishes.  
Stopping a sound when another finishes shouldn’t be too hard. Let me try some pseudocode…

```auto
//Outside of draw()...
var soundYShouldStop = false;

function draw(){
  ...
  //...Whereever you put soundX.play(); function...
  soundX.play();
  soundYShouldStop = true; //Add this.
  //.....

  //At one point in draw() so that this runs every frame
  if(soundX.isPlaying() == false & soundYShouldStop == true){ //After soundX has finished playing, stop soundY.
    soundY.stop();
    soundYShouldStop = false;
  }
```

---

<div class="post-metadata">

**Author:** ![thesavgplatypus](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@thesavgplatypus](https://discourse.processing.org/u/thesavgplatypus)\
**Post date:** [December 14, 2018, 7:58pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/8 "2018-12-14T19:58:21Z")

</div>

Hi - I actually want to know whether I can stop soundX after playing once. since I have to say if (soundX.isPlaying()==false) because draw will play it 60 times per second, how do I get it to stop after one time through?

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 14, 2018, 8:13pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/9 "2018-12-14T20:13:01Z")

</div>

Oh. Simple! Let me try doing it with your code related to the `defeat` sound.  
Basically, as soon as you want the sound to play, you let it know about it once by using multiple boolean values.

```auto

boolean defeatAllowedToPlay = true; //Let it play the first time when it wants to

void draw() {
...
  if (rip > -50) {
    if (defeat.isPlaying() == false && defeatAllowedToPlay == true) {
      defeat.setVolume(1);
      defeat.play();
      defeat.rate(1);
      defeatAllowedToPlay = false;
    }
  } else { //i.e. if rip <= -50
    defeatAllowedToPlay = true;
  }
...

```

As soon as the `rip>-50` condition to actually play the sound is met, it plays it once and instantly disallows it to play again.  
Only as soon as that condition is not met, we allow the sound to play again - which it wouldn’t as that condition is not met, i.e. it would only allow playing the sound only as soon as `rip>-50` is met again.

In other words, you can use the `defeatAllowedToPlay` variable to know if `rip>-50` was true or not in the previous `draw()` cycle, to know if it ever played the sound or not.

---

<div class="post-metadata">

**Author:** ![thesavgplatypus](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@thesavgplatypus](https://discourse.processing.org/u/thesavgplatypus)\
**Post date:** [December 17, 2018, 6:51pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/10 "2018-12-17T18:51:16Z")

</div>

I tried this. The sound didn’t play at all! I think it’s because the sound is longer than one draw cycle.

---

<div class="post-metadata">

**Author:** ![fullmotion](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fullmotion/32/11300_2.png) [@fullmotion](https://discourse.processing.org/u/fullmotion)\
**Post date:** [December 4, 2020, 5:55pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/11 "2020-12-04T17:55:30Z")

</div>

do we always have to upload the sound file into p5.js sketch files?  
What’s the best way to upload big file? larger than 22 mb.

---

<div class="post-metadata">

**Author:** ![Annushka123](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@Annushka123](https://discourse.processing.org/u/Annushka123)\
**Post date:** [March 5, 2021, 5:50pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/12 "2021-03-05T17:50:33Z")

</div>

Hi @slow_izzm ,  
I was wondering if you could suggest a function for sound.play other than draw or mousePressed? I’m having a similar issue and I want the sound to play while mouseX is hovering in certain parts of the sketch but the sound id awful coming from within the draw function.

Many thanks,  
Anna

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [March 26, 2021, 1:57pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/14 "2021-03-26T13:57:29Z")

</div>

Same answer as above … you can invoke sound.Play() from any function or method but you still need to check if the sound is already playing so it doesn’t play another instance of it.

When it comes to triggering the sound on/while hovering then you just need a bool that keeps track of the hover state …

[https://editor.p5js.org/slow\_izzm/sketches/QDJ3v8fxg](https://editor.p5js.org/slow_izzm/sketches/QDJ3v8fxg)

---

<div class="post-metadata">

**Author:** ![Annushka123](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@Annushka123](https://discourse.processing.org/u/Annushka123)\
**Post date:** [March 26, 2021, 2:30pm UTC](https://discourse.processing.org/t/i-am-trying-to-load-in-a-sound-file-to-my-p5-sound-project-but-they-all-come-out-distorted-or-my-screen-goes-black/6121/15 "2021-03-26T14:30:30Z")

</div>

wow. thank you! that was super helpful.
