2015-09-21 100 views
0

我寫了一個Iphone Swift應用程序,它使用AVAudioPlayer隨機播放一系列聲音 - 現在是流行音,喇叭聲和鑼。當你點擊播放按鈕時,它會起作用,除了....Swift中的流量控制 - AVAudioPlayer

但是,當我點擊停止按鈕時沒有任何反應 - 它沒有響應(如果我只有一個聲音,停止工作)。我相信這是由於我的流量控制。如果我沒有放入'while soundPlayer.playing == true {}',代碼會「飛」通過聲音,而不是等待它完成。

如何修改代碼使聲音在播放下一個聲音之前完成?還允許停止按鈕功能?請參閱下面的代碼和屏幕截圖。 (其實堆棧溢出不會讓我張貼圖片,因爲我太新)


// 
// ViewController.swift 
// InsultSchool2 Swift 
// 
// Created by JR on 12/3/14. 
// Copyright (c) 2014 JR. All rights reserved. 
// 

import UIKit 
import AVFoundation 

//--------------------------------------------------- 

var soundPlayer:AVAudioPlayer = AVAudioPlayer() 

// used to try to do some kind of flow control. May not be needed if better way is found. 
var stopFlag:Bool = false 

//----------------------------------------------------- 

class ViewController: UIViewController { 

    @IBOutlet weak var valueSliderTime: UISlider! 
    @IBOutlet weak var valueLabelTime: UILabel! 
    @IBOutlet weak var valueStepperVolume: UIStepper! 
    @IBOutlet weak var valueLabelVolume: UILabel! 

//------------------------------------------------------ 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     //Displays an initial Volume value in volume label on load 
     //Convert to an int. Otherwise you get a weird value when getting close to zero 
     //Multiply by 10 so the int works. Otherwise you would int'ing a number between 0.0 and 1.0. 
     // "\()" is a shorthand to convert whatever to a string 
     valueLabelVolume.text = "\(Int(valueStepperVolume.value * 10))" 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


//------------------------------------------------------ 


    @IBAction func buttonStop(sender: UIBarButtonItem) { 
     NSLog("Enter Button Stop") 
     //?1 If a sound is not playing and stop is hit, then it crashes 
     //?2 the stop button does not work with the whlle loop below 
     soundPlayer.stop() 
    } 

    @IBAction func sliderTime(sender: UISlider) { 
     valueLabelTime.text = "\(Int(valueSliderTime.value))" 
    } 

    @IBAction func stepperVolume(sender: UIStepper) { 
     //Converted to an int. Otherwise you get a weird value when getting close to zero 
     valueLabelVolume.text = "\(Int(valueStepperVolume.value * 10))" 
    } 

    @IBAction func buttonPlay(sender: UIBarButtonItem) { 
     NSLog("Enter Button Start") 

     var soundArray:[String] = ["Sound0", "Sound1", "Sound2"] 

     // Randomizes a number to indicate which random sound to play in the array 
     /* Number is from 0 to number in the(). Don't add one or 0 will never play. Go one more than the numbers in the array. For example if you have 3 items in the array go to 3. THis will go from 0 to 2 (ie., 3 items)*/ 
     // Reference----- var soundRandomNumber:Int = Int(arc4random_uniform(3)) 
     var soundRandomNumber:Int 
     soundRandomNumber = Int(arc4random_uniform(3)) 


     //Creates a random number to wait between sounds based on the slider value. 
     //arc4random requires a UInt32 (Unsigned is a positive number). 
     //_uniform is slightly more random than without the Uniform 
     //The documentation says to use Int otherwise. 
     println(Int(valueSliderTime.value)) 
     NSLog("valueSliderTime.value") 
     var waitTimeRandomNumber = Int(arc4random_uniform(UInt32(valueSliderTime.value))) 
     println(waitTimeRandomNumber) 
     NSLog("waitTimeRandomNumber") 

     // Constructs a string with the random number for the URL 
     //var soundFile:String = soundArray[soundRandomNumber] 
     var soundFile:String 
     soundFile = soundArray[soundRandomNumber] 

     //Reference---- var soundURL = NSBundle.mainBundle().URLForResource(soundFile, withExtension:"mp3") 

     var soundURL:NSURL! 
     soundURL = NSBundle.mainBundle().URLForResource(soundFile, withExtension:"mp3") 
     soundPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil) 


     //?3 How do I set up a loop or control that works until the stop button is pressed? 
      while stopFlag == false{ 
       NSLog("inside while") 
       println(stopFlag) 


       //?4 Is the below correct? The actual volume does not seem to change though the .volume does 
       soundPlayer.volume = Float(valueStepperVolume.value) 
       println(Float(valueStepperVolume.value)) 
       NSLog("Float(valueStepperVolume.value)") 
       println(soundPlayer.volume) 
       NSLog("soundPlayer.volume") 

       soundRandomNumber = Int(arc4random_uniform(3)) 
       soundFile = soundArray[soundRandomNumber] 
       soundURL = NSBundle.mainBundle().URLForResource(soundFile, withExtension:"mp3") 
       soundPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil) 
       soundPlayer.prepareToPlay() 
       soundPlayer.play() 


       //?5 How do I make the player not blow through the sound and wait until is finished 
       while soundPlayer.playing == true { 
       } 


       //?6 How can i make a random timer that waits for a random time before relooping? 
       waitTimeRandomNumber = Int(arc4random_uniform(UInt32(valueSliderTime.value))) 

      }// End of while loop 


     } //ends playButton IBAction 

//?7 How to allow this app to play over other music in another player 
} 

回答