2016-02-04 3 views
0
import CoreMotion  
var ButtonAudio4URL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Swing", ofType: "wav")!) 
var ButtonAudioPlayer4 = AVAudioPlayer() 
do { 
     try ButtonAudioPlayer4 = AVAudioPlayer(contentsOfURL: ButtonAudio4URL) 
     ButtonAudioPlayer4.prepareToPlay() 
    } catch { 
     print("audioPlayer failure") 
    } 
func play() { 
    ButtonAudioPlayer4.currentTime = 0 
    ButtonAudioPlayer4.play() 
} 
func play2() { 
    ButtonAudioPlayer4.currentTime = 0 
    ButtonAudioPlayer4.play() 
} 
func play3() { 
    ButtonAudioPlayer4.currentTime = 0 
    ButtonAudioPlayer4.play() 
} 
func stop() { 
    ButtonAudioPlayer4.currentTime = 0 
    ButtonAudioPlayer4.stop() 
} 
func stop2() { 
    ButtonAudioPlayer4.currentTime = 0 
    ButtonAudioPlayer4.stop() 
} 
func stop3() { 
    ButtonAudioPlayer4.currentTime = 0 
    ButtonAudioPlayer4.stop() 
} 
lastDirection = 0 //Idle accel 
    threshold = 1.0 //Minimum positive(right) accel 
    nthreshold = -1.0 //Minimum negative(left) accel 

    if motionManager.accelerometerAvailable { 
     let queue = NSOperationQueue() 
     motionManager.startAccelerometerUpdatesToQueue(queue, withHandler: { 
      data, error in 
      guard let data = data else{ 
       return 
      } 

    // Get the acceleration 
    let xAccel = data.acceleration.x //X accel 
    let yAccel = data.acceleration.y //Y accel 
    let zAccel = data.acceleration.z //Z accel 
    let xPositive = xAccel > 0 //Positive(right) x accel 
    let xNegative = xAccel < 0 //Negative(left) x accel 
    let yPositive = yAccel > 0 //Positive(up) y accel 
    let yNegative = yAccel < 0 //Negative(down) y accel 
    let zPositive = zAccel > 0 //Positive(front) z accel 
    let zNegative = zAccel < 0 //Negative(back) z accel 

    // Run if the acceleration is higher than theshold 
    if abs(xAccel) > self.threshold { 

     //If moved right 
     dispatch_async(dispatch_get_main_queue()) { 
      if self.lastDirection != 1 && xPositive { 
       self.lastDirection = 1 
       print("Up") 
       self.play() 
     } else if self.lastDirection !=  -1 && !xPositive { 
      self.lastDirection = -1 
      print("Down") 
      self.play() 
     } 
     } 
    } 

    // Run if the acceleration is higher than ntheshold 
    if abs(xAccel) < self.nthreshold { 

     //If moved left 
     dispatch_async(dispatch_get_main_queue()) { 
      if self.lastDirection != 1 && xNegative { 
       self.lastDirection = 1 
       print("Up") 
       self.play2() 
     } else if self.lastDirection !=  -1 && !xNegative { 
      self.lastDirection = -1 
      print("Down") 
      self.play2() 
     } 
     } 
    } 

    // Run if the acceleration is higher than theshold 
    if abs(yAccel) > self.threshold { 

     //If moved up 
     dispatch_async(dispatch_get_main_queue()) { 
      if self.lastDirection != 1 && yPositive { 
       self.lastDirection = 1 
       print("Up") 
       self.play() 
     } else if self.lastDirection !=  -1 && !yPositive { 
      self.lastDirection = -1 
      print("Down") 
      self.play() 
     } 
     } 
    } 

    // Run if the acceleration is higher than ntheshold 
    if abs(yAccel) < self.nthreshold { 

     //If moved left 
     dispatch_async(dispatch_get_main_queue()) { 
      if self.lastDirection != 1 && yNegative { 
       self.lastDirection = 1 
       print("Up") 
       self.play2() 
     } else if self.lastDirection !=  -1 && !yNegative { 
      self.lastDirection = -1 
      print("Down") 
      self.play2() 
    } 
    } 
    } 

    // Run if the acceleration is higher than theshold 
    if abs(zAccel) > self.threshold { 

    //If moved front 
    dispatch_async(dispatch_get_main_queue()) { 
    if self.lastDirection != 1 && zPositive { 
    self.lastDirection = 1 
    print("Up") 
    self.play() 
    } else if self.lastDirection !=  -1 && !zPositive { 
    self.lastDirection = -1 
    print("Down") 
    self.play() 
    } 
    } 
    } 

    // Run if the acceleration is higher than theshold 
    if abs(zAccel) < self.nthreshold { 

    //If moved back 
    dispatch_async(dispatch_get_main_queue()) { 
    if self.lastDirection != 1 && zNegative { 
    self.lastDirection = 1 
    print("Up") 
    self.play2() 
    } else if self.lastDirection !=  -1 && !zNegative { 
    self.lastDirection = -1 
    print("Down") 
    self.play2() 
    } 
    } 
    } 

    }) 

    } 

Bonjour, J'ai construit une application iOS avec Xcode 7.2, Swift 2.0, pour i0S 9,2 Je voudrais ajouter une fonction où lorsque les ondes utilisateur leur appareil dans l'air, un son être joué. Mon problème est que, bien que le son joue, il s'arrête soudainement, et est rejoué lorsqu'il est déplacé à nouveau dans le processus de lecture du son. Je voudrais juste que le son soit joué une fois, sans chevauchement ni arrêt. J'ai inclus tout le code lié à CoreMotion, et exclu tous les autres. Merci.Intégrer audio et accéléromètre pour Swift 2.0? (L'utilisation coremotion)

Répondre

1

Si je comprends votre but, vous voudrez probablement avoir une barrière booléenne pour empêcher le jeu en double.

Ex:


    var isPlaying1 = false 

    func play() { 
     if self.isPlaying1 == true { 
      return 
     } 
     ButtonAudioPlayer4.currentTime = 0 
     ButtonAudioPlayer4.play() 
     self.isPlaying1 = true 
    } 

    func stop() { 
     if self.isPlaying1 == false { 
      return 
     } 
     ButtonAudioPlayer4.currentTime = 0 
     ButtonAudioPlayer4.stop() 
     self.isPlaying1 = false 
    } 

+0

Merci beaucoup. Je ne sais pas si cela fonctionne, mais je vais le tester bientôt, une fois que j'aurai le temps. –

+1

Bonne idée, mais vous n'avez pas besoin de créer un nouveau booléen, AVAudioPlayer possède déjà une propriété bool 'playing'. – Moritz

+0

@EricD Comment pourrais-je implémenter ceci? Ça semble intéressant. –