APWidgets multiple instances of a single sound

edited May 2016 in Android Mode

I am trying to create a laser sound effect for my game. Everything works great, except a new instance of the sound won't play until the previous one has finished. I was wondering how to make an ArrayList of sounds, if possible, so the sound will properly play with the creation of each laser.

I am currently using apwidgets as my sound library, and I am going off this example of code: https://code.google.com/p/apwidgets/wiki/APMediaPlayer_example

Tagged:

Answers

  • shameless self bump

  • Can you provide the code for what you have so far?

  • edited January 2014

    Here is a snippet of code from a larger project.

    import apwidgets.*;
    
    PMediaPlayer player;
    
    void setup() {
      player = new PMediaPlayer(this); //create new APMediaPlayer
      player.setMediaFile("laser_shoot1.wav"); //set the file (files are in data folder)
      player.start(); //start play back
      player.setLooping(true); //restart playback end reached
      player.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
    }
    
    //--------Laser Class---------------
    
    class Laser {
       Laser() {
         player.start();
       }
    }
    
  • I think you should create a separate PMediaPlayer for each Laser. Then you can create it in Laser's constructor and start looping it, removing the need for an ArrayList of PMediaPlayers... only an ArrayList of Lasers.

  • edited January 2014

    I'm getting an unexpected token error now, at the line indicated, at "new".

    class Laser {
      float radius, x, y, speed, velocityX, velocityY, direction, targetX, targetY, distance;
      float timer; 
      boolean destroyed, orange, red, purple, yellow, cyan, white;  
      int c;
    
      Laser(float _playerX, float _playerY, int _c) {
        radius = 21/2;
        speed = 12;
        x = _playerX;
        y = _playerY;
        c = _c;
        targetX = mouseX;
        targetY = mouseY;
        direction = (atan2(targetY-y, targetX-x));
        velocityX = cos(direction)*speed;
        velocityY = sin(direction)*speed;
    
        PMediaPlayer player;
    
    
        player = new PMediaPlayer(this); //create new APMediaPlayer ****Error Here****
        player.setMediaFile("laser_shoot1.wav"); //set the file (files are in data folder)
        player.setLooping(false); //restart playback end reached
        player.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
        player.start();
      }
    
    
      void draw() {
        update();
        display();
      }
    
      void update() {
    
        timer--;
    
        velocityX = cos(direction)*speed;
        velocityY = sin(direction)*speed;
    
        x += velocityX;
        y += velocityY;
    
        if (x > screenWidth - 20 || x < -20 || y > height + 20 || y < -20 ) {
          destroyed = true;
        }
      }
    
      void display() {
        imageMode(CENTER);
        if (c == 1) {
          fill(222, 126, 47);
          orange = true;
        }
        if (c == 2) {
          fill(255, 0, 0);
          red = true;
        }
        if (c == 3) {
          fill(141, 21, 175);
          purple = true;
        }
        if (c == 4) {
          fill(245, 250, 2); 
          yellow = true;
        }
        if (c == 5) {
          fill(3, 250, 173);
          cyan = true;
        }
        if (c == 6) {
          fill(255); 
          white = true;
        }
        ellipse(x, y, radius*2, radius*2); //Make this a laser
      }
    }
    
  • edited January 2014

    If you meant line #22, perhaps you should take a look at this recent post below: <):)
    http://forum.processing.org/two/discussion/2239/minim-in-a-new-tab#Item_3

Sign In or Register to comment.