How to stop audio playing twice when tapped on android devices?

Hi Guys,

I'm trying to make a simple drum tap thing, and it works well in Safari and on iPhone. However, on both of my friends Android phones, one tap on the screen plays the hit twice.

It's got me stumped. Any tips for me?

var song, analyzer;

function preload() {
  soundFormats('wav');
  song = loadSound('warble.wav');
}

function setup() {
  createCanvas(500, 1146);
  fullscreen(false);

  // create a new Amplitude analyzer
  analyzer = new p5.Amplitude();

  // Patch the input to an volume analyzer
  analyzer.setInput(song);
}


function mouseClicked() {
  if (song.isPlaying()) {
    //sample.pause();
    song.playMode("restart");
    song.play();
    //do nothing!
  } else {
    song.play();
}
}


function draw() {

  background(255);

  // Get the average (root mean square) amplitude
  var rms = analyzer.getLevel();


  //rectMode(CENTER);  // Set rectMode to CENTER
  fill(0,150,220);
  ellipse(width/2, height/2, 100+rms*2500, 100+rms*2500);

  fill(255);


 ellipse(width/2, height/2, 50+rms*1200, 50+rms*1200);



  // Draw an ellipse with size based on volume



}

Answers

  • edited October 2016

    Nice use of restart!

    Odd -- i can't test but the error sounds almost as if on Android the MouseClicked is getting called by both touchStarted() and again by touchEnded(). Have you tried changing the sketch from mouseClicked to touchStarted and testing that on touchscreen?

Sign In or Register to comment.