简体   繁体   中英

Create a random audio sound generator

Thanks for replying.

I am trying to make a program when I press a button once, two random sounds will play. I can get the random sounds to play if I press the button, but I am looking that if I press the button, the random sound will play differently each time.

I can paste the sounds together to hear them in the sequence I want them, but I would like swift to generate the sounds.

I thought of the AVqueplayer to make it as a playlist. I was thinking this can be like a pair of dice in an analogy. For example, if I were to throw the dice down, the random sounds will occur.

I am still a newbie, and tried to figure this out on my own, because it seemed so simple, but I am out of options now.

Here is what I got so far. This will play a random sound when I press the button each time.

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVAudioPlayer = AVAudioPlayer()

    var sounds = ["sound1", "sound2", "sound3"]

    override func viewDidLoad() {
        super.viewDidLoad()  

    }

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        if event!.subtype == UIEventSubtype.motionShake {


            let randomNumber = Int(arc4random_uniform(UInt32(sounds.count)))
            let fileLocation = Bundle.main.path(forResource: sounds[randomNumber], ofType: "mp3")
            var error: NSError? = nil
            do { try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation!))
            player.play()
            } catch {}           
        }
    }

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

Using the same code, and adding a second random number with matching file location will allow the sounds to play back to back, both being random:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVAudioPlayer = AVAudioPlayer()

    var sounds = ["sound1", "sound2", "sound3"]

    override func viewDidLoad() {
        super.viewDidLoad()  

    }

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        if event!.subtype == UIEventSubtype.motionShake {


            let randomNumber1 = Int(arc4random_uniform(UInt32(sounds.count)))
            let randomNumber2 = Int(arc4random_uniform(UInt32(sounds.count)))
            let fileLocation1 = Bundle.main.path(forResource: sounds[randomNumber1], ofType: "mp3")
            let fileLocation2 = Bundle.main.path(forResource: sounds[randomNumber2], ofType: "mp3")
            //var error: NSError? = nil
            do {
                try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation1!))
                player.play()
                try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileLocation2!))
                player.play()
            } catch {}           
        }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM